TFS 2015 vNext build从文件夹aganst databese执行SQL脚本

时间:2016-05-19 14:21:41

标签: tfsbuild tfs2015

我需要创建集成检查构建,该构建还检查更新skripts在针对数据库执行时是否不会抛出错误(microsoft sql server 2008 r2)
我无法弄清楚如何执行从特定文件夹到测试数据库的所有SQL文件?
有任何建议吗?

在旧的TFS 2010中,有人已经为此创建了库,并在MSbuild中进行了这样的配置。

<Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
    <UsingTask TaskName="PP.Builder.VersionUpdateTask" AssemblyFile="PP.Builder.dll"/>
    <UsingTask TaskName="PP.DbUpdater.Library.DbBuildTask" AssemblyFile="PP.DbUpdater.Library.dll"/>
    .....
    .....
  <Target Name="AfterGet">
     <Exec Command="$(TF) get /noprompt /all $(AssemblyInfoFile)"/>
  </Target>

  <Target Name="AfterCompile">
     <DbBuildTask DbServer="TestServer\sql2008r2" DbBackupPath="c:\beckUp backups" DbName="empty" ScriptsPath="$(SolutionRoot)\Proj\dev\PP.Veyron.Db" UpdateVersionNumber="false" UpdateVersionNumberYesterday="false" TestOnly="true" SkipDbBackup="false"/>
    </Target>
</Project>

如何将此转移到TFS2015 vNext构建系统?

1 个答案:

答案 0 :(得分:5)

Powershell脚本可用于执行SQL脚本。因此,您可以在构建模板中添加Powershell脚本步骤,该脚本应如下所示:

#Provide SQLServerName
$SQLServer ="MySQLSErver\InstanceName"
#Provide Database Name 
$DatabaseName ="Test"
#Scripts Folder Path
$FolderPath ="C:\MyScripts\Test\"

#Loop through the .sql files and run them
foreach ($filename in get-childitem -path $FolderPath -filter "*.sql")
{
invoke-sqlcmd –ServerInstance $SQLServer -Database $DatabaseName -InputFile $filename.fullname
#Print file name which is executed
$filename 
} 

请参阅博客:http://www.techbrothersit.com/2015/08/how-to-execute-sql-files-from-directory.html