Conditional logic in PostDeployment.sql script using SQLCMD Conditional logic in PostDeployment.sql script using SQLCMD database database

Conditional logic in PostDeployment.sql script using SQLCMD


UPDATE

I've now discovered that the if/else syntax above doesn't work for me because some of my linked scripts require a GO statement. Essentially the :r just imports the scripts inline, so this becomes invalid sytax.

If you need a GO statement in the linked scripts (as I do) then there isn't any easy way around this, I ended up creating several post deployment scripts and then changing my project to overwrite the main post depeployment script at build time depending on the build configuration. This is now doing what I need, but it seems like there should be an easier way!

For anyone needing the same thing - I found this post useful

So in my project I have the following post deployment files:

  • Script.PostDeployment.sql (empty file which will be replaced)
  • Default.Script.PostDeployment.sql (links to scripts needed for standard data config)
  • Configuration1.Script.PostDeployment.sql (links to scripts needed for a specific data config)

I then added the following to the end of the project file (right click to unload and then right click edit):

  <Target Name="BeforeBuild">      <Message Text="Copy files task running for configuration: $(Configuration)" Importance="high" />      <Copy Condition=" '$(Configuration)' == 'Release' " SourceFiles="Scripts\Post-Deployment\Default.Script.PostDeployment.sql" DestinationFiles="Scripts\Post-Deployment\Script.PostDeployment.sql" OverwriteReadOnlyFiles="true" />      <Copy Condition=" '$(Configuration)' == 'Debug' " SourceFiles="Scripts\Post-Deployment\Default.Script.PostDeployment.sql" DestinationFiles="Scripts\Post-Deployment\Script.PostDeployment.sql" OverwriteReadOnlyFiles="true" />      <Copy Condition=" '$(Configuration)' == 'Configuration1' " SourceFiles="Scripts\Post-Deployment\Configuration1.Script.PostDeployment.sql" DestinationFiles="Scripts\Post-Deployment\Script.PostDeployment.sql" OverwriteReadOnlyFiles="true" />  </Target>

Finally, you will need to setup matching build configurations in the solution.

Also, for anyone trying other work arounds, I also tried the following without any luck:

  1. Creating a post build event to copy the files instead of having to hack the project file XML. i couldn't get this to work because I couldn't form the correct path to the post deployment script file. This connect issue describes the problem

  2. Using variables for the script path to pass to the :r command. But I came across several errors with this approach.


I managed to work around the problem using the noexec method.

So, instead of this:

IF ('$(ConfigSetting)' = 'Configuration1') BEGIN    print 'inserting specific configuration'     :r .\Configuration1\Data.sql END

I reversed the conditional and set NOEXEC ON to skip over the imported statement(s) thusly:

IF ('$(ConfigSetting)' <> 'Configuration1')    SET NOEXEC ON:r .\Configuration1\Data.sqlSET NOEXEC OFF

Make sure you turn it back off if you want to execute any subsequent statements.


Here's how I am handling conditional deployment within the post deployment process to deploy test data for the Debug but not Release configuration.

First, in solution explorer, open the project properties folder, and right-click to add a new SqlCmd.variables file.

Name the file Debug.sqlcmdvars.

Within the file, add your custom variables, and then add a final variable called $(BuildConfiguration), and set the value to Debug.

Repeat the process to create a Release.sqlcmdvars, setting the $(BuildConfiguration) to Release.

Now, configure your configurations:Open up the project properties page to the Deploy tab.On the top dropdown, set the configuration to be Debug.On the bottom dropdown, (Sql command variables), set the file to Properties\Debug.sqlcmdvars.

Repeat for Release as:On the top dropdown, set the configuration to be Release.On the bottom dropdown, (Sql command variables), set the file to Properties\Release.sqlcmdvars.

Now, within your Script.PostDeployment.sql file, you can specify conditional logic such as:

IF 'Debug' = '$(BuildConfiguration)'BEGINPRINT '***** Creating Test Data for Debug configuration *****';:r .\TestData\TestData.sqlEND

In solution explorer, right click on the top level solution and open Configuration Manager. You can specify which configuration is active for your build.You can also specify the configuration on the MSBUILD.EXE command line.

There you go- now your developer builds have test data, but not your release build!