如何捕获DacSevices.Deploy输出?

Dav*_*ers 15 sql-server dacpac

所以我设法通过Octopus部署了我们的DACPAC架构.我正在使用与.Net对象交互的Deploy.ps1脚本,就像文章描述的那样.

我想通过在我们的Octopus日志中包含从sqlcmd获得的"标准输出"来使部署过程更加透明.我正在寻找生成的架构修改消息以及我们的开发人员在前/后脚本中添加的任何自定义迁移迁移消息.

我能想到的唯一解决方法是首先使用DACPAC服务生成脚本,然后使用sqlcmd.exe运行它.有任何想法吗?

Dav*_*ers 25

找到解决方案,发布以防其他人遇到此问题.您只需订阅DacService的Message事件即可.

C#样本:

var services = new Microsoft.SqlServer.Dac.DacServices("data source=machinename;Database=ComicBookGuy;Trusted_connection=true");

var package = Microsoft.SqlServer.Dac.DacPackage.Load(@"C:\Database.dacpac");

var options = new Microsoft.SqlServer.Dac.DacDeployOptions();
options.DropObjectsNotInSource = true;
options.SqlCommandVariableValues.Add("LoginName", "SomeFakeLogin");
options.SqlCommandVariableValues.Add("LoginPassword", "foobar!");

services.Message += (object sender, Microsoft.SqlServer.Dac.DacMessageEventArgs eventArgs) => Console.WriteLine(eventArgs.Message.Message);

services.Deploy(package, "ComicBookGuy", true, options);
Run Code Online (Sandbox Code Playgroud)

Powershell样本(由Octopus Tentacle执行):

# This script is run by Octopus on the tentacle
$localDirectory = (Get-Location).Path
$tagetServer = $OctopusParameters["SQL.TargetServer"]
$databaseName = "ComicBookGuy"

Add-Type -path "$localDirectory\lib\Microsoft.SqlServer.Dac.dll"

$dacServices = New-Object Microsoft.SqlServer.Dac.DacServices ("data source=" + $tagetServer + ";Database=" + $databaseName + "; Trusted_connection=true")
$dacpacFile = "$localDirectory\Content\Unity.Quotes.Database.dacpac"

$dacPackage = [Microsoft.SqlServer.Dac.DacPackage]::Load($dacpacFile)

$options = New-Object Microsoft.SqlServer.Dac.DacDeployOptions
$options.SqlCommandVariableValues.Add("LoginName", $OctopusParameters["SQL.LoginName"])
$options.SqlCommandVariableValues.Add("LoginPassword", $OctopusParameters["SQL.LoginPassword"])
$options.DropObjectsNotInSource = $true

Register-ObjectEvent -InputObject $dacServices -EventName "Message" -Action { Write-Host $EventArgs.Message.Message } | out-null

$dacServices.Deploy($dacPackage, $databaseName, $true, $options)
Run Code Online (Sandbox Code Playgroud)

在powershell版本中,我无法获得方便的"Add_EventName"样式的事件通知,因此我不得不使用笨重的cmdlet.咩.