0

我在 Visual Studio 2013 中有一个 SSDT 数据库项目。在将数据库更新发布到其他环境中的数据库时,它用作“答题纸”。我最近看到了Jamie Thompson 关于 DacPac 的博客文章,他在其中写了一篇关于 DacPac 是什么以及如何使用它们的精彩总结。

现在,假设我有以下情况:

  1. VS2013中的SSDT项目,版本为1.0.33
  2. 我的开发环境中的一个数据库,版本为 1.0.32
  3. 我的 S 测试环境中的数据库,版本为 1.0.31

根据 Jamie 的说法,使用 DacPac 发布数据库更改是幂等的,即我可以将项目符号 1 中的 SSDT 项目中的 DacPac 发布到项目符号 3 中的数据库,它将在版本 1.0.32 和版本中对数据库进行所有更改1.033,因为 DacPac 包含有关整个DB 模式的信息(然后还应包括在 1.0.32 版中所做的更改)。

这是对发布 DacPac 工作原理的正确理解吗?

4

1 回答 1

2

是的,一旦您以声明方式在 DACPAC 中定义了模型,您就可以使用任何版本的数据库将模型部署到任何目标环境。引擎会根据目标自动生成合适的变更脚本。

您可以使用 SqlPackage.exe 实用程序从 Visual Studio 或命令行部署(发布)模型。这是一个使用 SqlPackage.exe 和发布配置文件的 PowerShell 脚本示例。您可以选择直接发布或生成更改脚本(设置 $action 变量)。DACPAC 文件和发布配置文件必须位于 ps 文件的同一文件夹中。将生成一个日志文件:

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

####################################
$action                 = 'Publish' #Only generate script: 'Script'; Publish directly: 'Publish'

$databaseName       = 'Test'
$serverName         = 'localhost'
$dacpacPath         = Join-Path $scriptPath '.\Test\bin\Debug\Test.dacpac'
$publishProfilePath = Join-Path $scriptPath '.\Test\Scripts\Publish\Test.publish.xml'


$outputChangeScriptPath = Join-Path $scriptPath 'TestDeploymentScript.sql'

$logPath = Join-Path $scriptPath 'TestDeployment.log'
####################################



$sqlPackageExe = 'C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin\SqlPackage.exe'

if ($action.ToUpper() -eq 'SCRIPT')
{

    Write-Host '********************************' | Tee-Object -File "$logPath"
    Write-Host '*  Database Objects Scripting  *' | Tee-Object -File "$logPath"
    Write-Host '********************************' | Tee-Object -File "$logPath"

    $args = "/Action:Script /TargetDatabaseName:$databaseName /TargetServerName:$serverName " +
            "/SourceFile:""$dacpacPath"" /Profile:""$publishProfilePath"" /OutputPath:""$outputChangeScriptPath"" "

    $command = "& ""{0}"" {1}" -F $sqlPackageExe, $args

    Invoke-Expression $command | Tee-Object -File "$logPath"

    if($LASTEXITCODE -ne 0)
    {
        $commandExitCode = $LASTEXITCODE 
        $Error[0] | Tee-Object -File $outputChangeScriptPath
        return $commandExitCode
    }

}

if ($action.ToUpper() -eq 'PUBLISH')
{
    # DWH
    Write-Host '*********************************' | Tee-Object -File "$logPath"
    Write-Host '*  Database Objects Deployment  *' | Tee-Object -File "$logPath"
    Write-Host '*********************************' | Tee-Object -File "$logPath"

    $args = "/Action:Publish /TargetDatabaseName:$databaseName /TargetServerName:$serverName " +
            "/SourceFile:""$dacpacPath"" /Profile:""$publishProfilePath"" "

    $command = "& ""{0}"" {1}" -F $sqlPackageExe, $args

    Invoke-Expression $command | Tee-Object -File "$logPath"

    if($LASTEXITCODE -ne 0)
    {
        $commandExitCode = $LASTEXITCODE 
        $Error[0] | Tee-Object -File $outputChangeScriptPath
        return $commandExitCode
    }
}
于 2015-07-10T22:08:11.800 回答