Migrations
note
We recommend reading Evolutionary Database Design first, since it has a major impact in how we write migrations.
Repositories
We use the Repository pattern with the MSSQL repositories being written using Dapper. Each repository method in turn calls a Stored Procedure, which primarily fetches data from Views.
Changing the database
As we follow Evolutionary Database Design each change needs to be split into two parts. A backwards compatible transition phase, and a non-backwards compatible.
Best Practices
When writing a migration script there are a couple of best practices we follow. Please check the T-SQL Code Style for more details.
Backwards Compatible
- Modify the source
.sqlfiles insrc/Sql/dbo. - Write a migration script, and place it in
util/Migrator/DbScripts. Each script must be prefixed with the current date.
Non-backwards Compatible
- Copy the relevant
.sqlfiles fromsrc/Sql/dbotosrc/Sql/dbo_future. - Remove the backwards compatibility which is no longer needed.
- Write a new Migration and place it in
src/Migrator/DbScripts_future, name itYYYY-0M-FutureMigration.sql.- Typically migrations are designed to be run in sequence. However since the migrations in DbScripts_future can be run out of order, care must be taken to ensure they remain compatible with the changes to DbScripts. In order to achieve this we only keep a single migration, which executes all backwards incompatible schema changes.