SQL Change Automation projects use a migration script driven approach to deployment (as opposed to the model driven approach of SSDT projects), where you script your database changes up-front and deploy the same set of scripts in a pre-defined order through each of your application environments. This results in consistent and repeatable deployments through to Production.

The trade-off is the up-front work required to get your scripts right during the development phase. SQL Change Automation provides a tool-window to assist with the authoring and re-authoring of migrations, allowing you to work online to make your database edits, and then import your changes to generate your migrations.

Adding a migration to your SQL Change Automation project

If you’ve already got some scripts written, or if you need to deploy scripts provided by a vendor (eg. ASP.NET Membership Provider object creation scripts), simply create a new project and then right-click the project node in Solution Explorer and select Add... Script. This will add an empty migration to the project, into which you can paste your script content.

The new script will be added to the project with the following auto-generated filename:

[nextNumber]_[date]-[time]_[username].sql

For example, 001_20180613-1525_A.Developer.sql.

If your script contains SQL statements that have already been executed against the target database, click Mark as Deployed to prevent the script from being executed again, otherwise click Deploy Project.

Conditional script execution (Applies to package-based deployments only)

By default, all migrations within your project are executed at deployment time unconditionally. However if you would like a given migration to be executed in some circumstances and not others, then you can include an execution conditional within the header of your script to be evaluated at deployment time. This is particularly useful if you have scripts that should only be run in specific environments, such as a set of users that should only be created in the Development environment. The condition should be written in the syntax of a T-SQL "IF" statement condition, for example:

-- <Migration Id="{UniqueIdentifier}" Condition="@@SERVERNAME LIKE 'DEV%' OR '$(Environment)'='DEV' " />


Only if the condition evaluates to TRUE during the first pass will the script be executed; if the condition is not met, SQL Change Automation will bypass the script and mark it as deployed. Even if the condition later evaluates to TRUE, the script will not be executed during a subsequent deployment (unless the appropriate record is deliberately removed from the   [__MigrationLog] table first).


The Condition attribute is only evaluated when deploying with the package scripts that SQL Change Automation produces at build time.
It will be ignored altogether when the database is deployed within Visual Studio and Powershell (or any other method that involves deploying the patch scripts produced with MSBuild, Powershell cmdlets or the add-ons) meaning that all migrations will be executed regardless of whether the condition is met.


Guidelines for working with migration scripts


If you would like to have your migration executed with each modification to the script file, try using Programmable Objects (useful for deploying replaceable objects such as stored procedures, functions and views).

Alternatively, if you would like to perform an action each time you deploy the project, try using a Pre or Post-Deployment script instead. These can be used for creating the database if it doesn't exist, altering database settings, or for static data.