Switching branches
Published 18 April 2016
There are two ways to switch branches:
- drop and recreate your database in the destination branch
This is the simplest method. The only disadvantage is that you'll lose transactional data. If you need this data for testing, you'll have to repopulate your databases. - resync your schema to match the destination branch
Use this method if you need to preserve the contents of your database, and if there are only small differences between the branches.
Drop and recreate your database
The simplest way to switch branches is to drop and recreate your database. This resets your environment so you can deploy from the new branch. To do this:
In Visual Studio, close your solution.
- Take a backup of the database contained in your solution.
- From your local SQL Server instance, drop the database.
- In version control, select the branch you want to switch to.
- In Visual Studio, open the solution.
- To deploy the database, click Start.
You can also follow these steps to test whether your databases can be reproduced from source files.
Revert pending changes to re-sync your schema
If you need to preserve the contents of your database, and there are only small difference between branches, resync the schema to match the destination branch before you deploy:
- In version control, select the branch you want to switch to.
- In Visual Studio, open the solution.
- Click View > ReadyRoll
- Within the ReadyRoll tool-window, click Refresh.
The differences between the two branches are displayed. - Select the objects in the list, then right-click on them and click Revert selected object(s).
The pending object list refreshes and shows objects as identical. - To deploy the databases, click Start.
If there are multiple databases in your solution, you'll need to repeat steps 4 and 5 for each database. To switch between database projects, use the drop-down list in the ReadyRoll tool-window.
What differences may cause the resync to fail?
Resyncing your schema will work as long as there aren't substantial differences between the two branches. For example, if branch A contains a migration that dropped a not-null column, clicking Revert selected object(s) on branch B may fail. SQL Server can't recreate the column containing the data.
To prevent this problem in future, you could start taking regular backups of your databases. Then you can restore your development environment to a previous known state before deploying your solution.