Working with migration scripts
Published 18 February 2016
What are migration scripts?
To deploy changes from version control, the SQL Compare engine generates a deployment script. This is based on the differences between the state in version control and the target database. Migration scripts in version control can define how the SQL Compare engine generates specific sections of this deployment script.
Migration scripts are necessary to avoid data loss when making certain schema changes. To achieve this, the migration script intervenes to make data changes occur at the right point of the deployment.
In most cases, you only need to write SQL for the data changes in the migration script. Schema changes are committed separately and deployed as normal.
To learn more, see Migration script examples.
Creating a migration script
To create a new migration script:
- From the Object Explorer, select the database you want to add a migration script to.
- From the toolbar, select SQL Source Control.
The SQL Source Control window opens. Go to the Migrations tab.
Select the type of migration script, depending on your development process and the changes you're making:
- In the Name field, enter a name for the script.
- In the editor window, write SQL to make the required changes.
Note: Static data changes in migration scripts won't be deployed. - Click Save & Close.
Commit the changes to version control.
Always commit a new migration script immediately after saving it. Making changes to your database schema between saving and committing migration scripts can cause errors during deployment.
When you deploy this revision from version control, or use Get latest in SQL Source Control on another machine, the migration script will run as part of the deployment. For more information, see How migration scripts work.
Existing migration scripts
You can edit or delete existing migration scripts from the Migrations tab in SQL Source Control.
To edit or delete an existing migration script:
- From the Object Explorer, select a database with migration scripts.
- From the toolbar, select SQL Source Control.
The SQL Source Control window opens. Go to the Migrations tab.
Expand Existing migration scripts.
Migration scripts on the remote repository are listed.In the Actions column, click View / Edit next to a migration script.
Edit the script to make required changes.
Click Save & Close.
- Go to the Commit tab and commit the updated migration script.
Once committed, the updated migration script is used in all future deployments.
Deploying migration scripts
SQL Compare 11 or later can generate deployment scripts with migrations using the following source/target types:
Source:
- Scripts folder
- Source control
Target:
- Live database
- Scripts folder
We recommend using SQL Compare to deploy changes to production, as you have the opportunity to review the deployment script before it's deployed. For a full walkthrough of the deployment process using SQL Compare, see Migration script examples.
It is possible to use the Get latest function in SQL Source Control to deploy these changes, however we don't recommend linking your production database directly to source control.
Dependencies
When you create a migration script that includes uncommitted schema changes, SQL Source Control automatically includes any dependencies. Deselecting any of these dependencies during the deployment stage will cause the deployment to fail.
Static data
Migration scripts do not work with static data. See Static data and migrations.