Appearance
How To Manage Database Schema Changes
Goal: Version database schema changes alongside the code that requires them. Make both deploy and rollback easier.
Steps
- Accomplish the goal Ensure SQL solution folder exists
- Create two SQL scripts for each feature or story you're working on:
{story}-UP.sql
and{story}-DOWN.sql
. - Write the one schema change into the
-UP.sql
script. Don't try to get your entire final schema in one step; just do one change. - Apply the
-UP.sql
script to your database. - Manually verify the resulting schema is correct.
- Implement as much of the code change as you can with just the amount of schema change you've implemented so far.
- Use the
-UP.sql
script as a guide to write the-DOWN.sql
script. - Apply the
-DOWN.sql
script to your database. - Manually verify that you are back to the original schema.
- Commit code and scripts to source control.
- Does your
-UP.sql
script get to the whole final schema you want?- No:
- Repeat from step 2. You will modify/extend the same script files.
- Done!
Ensure SQL solution folder exists
- In your solution folder, create a physical directory named
SQL
. - In Visual Studio, add the newly created
SQL
directory as a Solution Folder. - Done!