SQLPackage Post Deployment Script not running

霸气de小男生 提交于 2019-12-11 10:38:07

问题


I'm using Visual Studio 2013 with TFS, over SQL-Server 2012. Specifically I'm using SQL-Server Data Tools within VS for our SQL development, rather than within SQL-Server Management Studio.

In short, the Post-Deployment SQL script appears not to be executed by SQLPackage.

The specifics are: I have a VS SQL Database Project 'ADMIR', which has been developing over the last few weeks and gradually becoming more sophisticated both in terms of SQL and it's deployment. The database includes some dynamically generated tables and therefore I've introduced SQL Command Variables to reference two databases (one is the actual ADMIR database, the other a DB on the same Server). Therefore I've added Database References to dacpacs for these two DB's.

The ADMIR Project will Build fine.

There is also a Post-Deployment SQL script, and prior to the introduction of SQL Command Variables, this would also run fine and initialize various table data.

However, my understanding is that SQL Command Variables won't work with the VS/TFS Publish action, in that the Post-Deployment script is no longer being executed.

This is ok, as ultimately I want to script/automate the Publish/Deploy of the ADMIR Project and so I've moved to using SQLPackage to perform the Publish.

Again, this appears to work fine - My SQL Package batch command will execute and log errors/output (as applicable) and Publish my Build dacpac to the target Server successfully. However, it seems to completely ignore the Post-Deployment script. In addition, the output reports that the SQL Command Variables are not set.

These are the various scripts and configuration settings:

SQLPackage batch command:

::Publish the ADMIR database using the ADMIR.DEV.Publish.xml Profile.
"C:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\SqlPackage.exe" 
    /Action:Publish 
    /Profile:"C:\Source Control\ADMIR\ReleaseControl\ADMIR.DEV.publish.xml"
    /SourceFile:"C:\Source Control\ADMIR\Build\ADMIR.dacpac"
    2> "C:\Source Control\ADMIR\ReleaseControl\Publish_Error.txt" 
    > "C:\Source Control\ADMIR\ReleaseControl\Publish_Output.txt"

ADMIR.DEV.publish.xml Publishing Profile:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <IncludeCompositeObjects>True</IncludeCompositeObjects>
    <TargetDatabaseName>ADMIR</TargetDatabaseName>
    <DeployScriptFileName>ADMIR.sql</DeployScriptFileName>
    <TargetConnectionString>Data Source=FOOBAR;Integrated Security=True;Pooling=False</TargetConnectionString>
    <ProfileVersionNumber>1</ProfileVersionNumber>
  </PropertyGroup>
  <ItemGroup>
    <SqlCmdVariable Include="ADMIRDatabaseName">
      <Value>ADMIR</Value>
    </SqlCmdVariable>
    <SqlCmdVariable Include="XYZDatabaseName">
      <Value>XYZDEV</Value>
    </SqlCmdVariable>
  </ItemGroup>
</Project>

Script.PostDeploy.sql (first few lines):

/* ADMIR Post-Deployment Script Template */
PRINT N'Post Deploy Script started:'+CAST(GETDATE() AS NVARCHAR(20))+N'.'
PRINT N'SQLCMD Variable $ADMIRDBName:'+[$(ADMIRDatabaseName)]+N'.'
PRINT N'SQLCMD Variable $XYZDBName:'+[$(XYZDatabaseName)]+N'.'

/* Purge existing data as we'll repopulate with what is required for a fresh deploy */
PRINT N'Purging existing table data...'
DELETE FROM [ADMIR].[Engine].[ProcessingStatus]
DELETE FROM [ADMIR].[Engine].[DataField]
DELETE FROM [ADMIR].[Engine].[DataKeyField]
...
...

When running a Build, I can see the ADMIR_Create.sql script does contain all the Post-Deploy statements. i.e. I can see the above lines of code in this file.

** ADMIR.sqlproj file contains the Post-Deploy scripts and the SQL Command Variables:**

<ItemGroup>
    ...
    ...
    <PostDeploy Include="ReleaseControl\Script.PostDeploy.sql" />
</ItemGroup>
...
...
<ItemGroup>
<SqlCmdVariable Include="ADMIRDatabaseName">
  <DefaultValue>ADMIR</DefaultValue>
  <Value>$(SqlCmdVar__7)</Value>
</SqlCmdVariable>
<SqlCmdVariable Include="XYZDatabaseName">
  <DefaultValue>XYZDEV</DefaultValue>
  <Value>$(SqlCmdVar__6)</Value>
</SqlCmdVariable>

Publish log, Publish_Output.txt:

Publishing to database 'ADMIR' on server 'FOOBAR'.
Initializing deployment (Start)
*** The following SqlCmd variables are not defined in the target scripts: ADMIRDatabaseName XYZDatabaseName.
Initializing deployment (Complete)
Analyzing deployment plan (Start)
Analyzing deployment plan (Complete)
Updating database (Start)
Update complete.
Updating database (Complete)
Successfully published database.

So:

  1. Why is the Post-Deployment script not being executed? I'm not seeing any logging information relating to it.

  2. Why does the SQLPackage output log state that the SQL Command Variables are not defined, when they are in the Publish profile?

  3. Possibly related to 2. Is the .sqlproj file correct in have the values of the SQL Command Variables as variables themselves e.g. $(SqlCmdVar__7)? where __6 & __7 coming from?

Thanks for any assistance!


回答1:


As per my comment, the issue logged in MSDN Connect, appears to be cause. i.e. an SQLCMD variable with the same name as the dacpac for the database is resulting in the script not being executed. When I remove this reference, and change the related SQLCMD variables to literal values for the DB Name, then the project builds and the post-deploy script is executed as required.




回答2:


I found that a new .sqlproj file created in Visual Studio 2017 did not include "default" Predeployment.sql or PostDeployment.sql files, so I added one manually.

Publishing did not execute the PostDeployment.sql because the file's Build Action was set to None. Changing the Build Action to PostDeploy fixed the issue.



来源:https://stackoverflow.com/questions/35298743/sqlpackage-post-deployment-script-not-running

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