There are several ways to achieve this, the first approach is to check the environment in deploying a post script, for example ..
if @@servername = 'dev_server' begin insert data here end
A slightly cleaner version should have different script files for each environment and import them through: r import sqlcmd script so you can:
- PostDeploy.sql
- DevServer.sql
- QAServer.sql
then
if @@servername = 'dev_server' begin :r DevServer.sql end if @@servername = 'qa_server' begin :r QAServer.sql end
You will need to make sure that the paths to the .sql files are correct, and you will copy them using dacpac.
You do not need to use @@ servername, you can use sqlcmd variables and pass them for each environment, which again is a little cleaner than the names of hard-coded servers.
The second approach is to configure dacpac to modify the post delpoy script with your specific environment, this is my preference and works best as part of the CI assembly, my process:
- Registration Changes
- Build Server creates dacpac
- The assembly accepts dacpac, copies the files dev, qa, prod, etc.
- The assembly replaces the post-deployment script in each with a specific env script
I am calling the scripts PostDeploy.dev.sql, PostDeploy.Qa.sql, etc. and set the Build action to "None" or they are added as "Script, Not in Build".
To replace the post-deployment script, you just need to use the .net packaging API or for some examples take a look at my Dir2Dac demo that does this and much more:
https://github.com/GoEddie/Dir2Dac
more specific:
https://github.com/GoEddie/Dir2Dac/blob/master/src/Dir2Dac/DacCreator.cs
var part = package.CreatePart(new Uri("/postdeploy.sql", UriKind.Relative), "text/plain"); using (var reader = new StreamReader(_postDeployScript)) { reader.BaseStream.CopyTo(part.GetStream(FileMode.OpenOrCreate, FileAccess.ReadWrite)); }