Publishing DacPacs in Visual Studio 2013

蹲街弑〆低调 提交于 2019-12-12 01:23:38

问题


I have an SSDT database project in Visual Studio 2013. This is used as the "answer sheet" when publishing database updates to a database in the other environments. I recently came across Jamie Thompson's blog article on DacPacs, where he writes a great summary on what DacPacs are, and how to use them.

Now, say I have the following scenario:

  1. The SSDT project in VS2013, which is version 1.0.33
  2. A database in my Dev environment, which is version 1.0.32
  3. A database in my S-test environment, whic is version 1.0.31

According to Jamie, publishing databases changes using DacPacs is idempotent, i.e. I can publish the DacPac from the SSDT project in bullet 1 to the database in bullet 3, and it will get all the changes done to the database in both version 1.0.32 and 1.033 since the DacPac contains information about the entire DB schema (which then also should include changes done in version 1.0.32).

Is this a correct understanding of how publishing a DacPac works?


回答1:


Yes, once you defined your model in a DACPAC in a declarative way, you can then deploy your model to any target environment with whatever version of you database. The engine will automatically generate the proper change scripts according to the target.

You can deploy (publish) your model from Visual Studio or from command line using the SqlPackage.exe utility. Here an example of a PowerShell script that use SqlPackage.exe and a Publish Profile file. You can choose to publish directly or generate the change script (set the $action variable). The DACPAC file and the Publish Profile file have to be in the same folder of the ps file. A log file will be generated:

$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
    }
}


来源:https://stackoverflow.com/questions/31334151/publishing-dacpacs-in-visual-studio-2013

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!