|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# MSSQL |
| 6 | + |
| 7 | +:::info |
| 8 | + |
| 9 | +For instructions on how to apply database migrations, please refer to the |
| 10 | +[Getting Started](../../getting-started/server/database/mssql/index.md) documentation. |
| 11 | + |
| 12 | +::: |
| 13 | + |
| 14 | +:::tip |
| 15 | + |
| 16 | +We recommend reading [T-SQL Code Style](../code-style/sql.md) since it has a major impact in how we |
| 17 | +write migrations. |
| 18 | + |
| 19 | +::: |
| 20 | + |
| 21 | +## SQL database project |
| 22 | + |
| 23 | +The separate database definitions in `src/Sql/.../dbo` serve as a "master" reference for the |
| 24 | +intended and final state of the database at that time. This is crucial because the state of database |
| 25 | +definitions at the current moment may differ from when a migration was added in the past. These |
| 26 | +definitions act as a lint and validation step to ensure that migrations work as expected, and the |
| 27 | +separation helps maintain clarity and accuracy in database schema management and synchronization |
| 28 | +processes. |
| 29 | + |
| 30 | +Additionally, a |
| 31 | +[SQL database project](https://learn.microsoft.com/en-us/azure-data-studio/extensions/sql-database-project-extension-sdk-style-projects) |
| 32 | +is in place; however, instead of using the auto-generated migrations from |
| 33 | +[DAC](https://learn.microsoft.com/en-us/sql/relational-databases/data-tier-applications/data-tier-applications?view=sql-server-ver16), |
| 34 | +we manually write migrations. This approach is chosen to enhance performance and prevent accidental |
| 35 | +data loss, which is why we have both a `sqlproj` and standalone migrations. |
| 36 | + |
| 37 | +## Modifying the database |
| 38 | + |
| 39 | +In accordance with the tenets of [Evolutionary Database Design](./edd.mdx) every change must be |
| 40 | +considered as split into two parts: |
| 41 | + |
| 42 | +1. A backwards-compatible transition migration |
| 43 | +2. A non-backwards-compatible final migration |
| 44 | + |
| 45 | +It is likely that a change does not require a non-backwards-compatible end phase e.g. all changes |
| 46 | +may be backwards-compatible in their final form; in that case, only one phase of changes is |
| 47 | +required. With the use of beta testing, partial roll-outs, [feature flags](../feature-flags.md), |
| 48 | +etc. the often-chosen path is to spread a change across several major releases with a calculated |
| 49 | +future state that can perform a "cleanup" migration that is backwards-compatible but still |
| 50 | +represents an overall-_incompatible_ change beyond the boundaries of what we need for individual |
| 51 | +release safety. |
| 52 | + |
| 53 | +### Backwards compatible migration |
| 54 | + |
| 55 | +1. Modify the source `.sql` files in `src/Sql/dbo`. |
| 56 | +2. Write a migration script, and place it in `util/Migrator/DbScripts`. Each script must be prefixed |
| 57 | + with the current date. |
| 58 | + |
| 59 | +Please take care to ensure: |
| 60 | + |
| 61 | +- any existing stored procedure accepts the same input parameters and that new parameters have |
| 62 | + nullable defaults |
| 63 | +- when a column is renamed the existing stored procedures first check (coalesce) the new location |
| 64 | + before falling back to the old location |
| 65 | +- continued updating of the old data columns since in case of a rollback no data should be lost |
| 66 | + |
| 67 | +### Non-backwards compatible migration |
| 68 | + |
| 69 | +These changes should be written from the perspective of "all data has been migrated" and any old |
| 70 | +stored procedures that were kept around for backwards compatibility should be removed. Any logic for |
| 71 | +syncing old and new data should also be removed in this step. |
| 72 | + |
| 73 | +1. Remove the backwards compatibility that is no longer needed. |
| 74 | +2. Write a new Migration and place it in `src/Migrator/DbScripts_finalization`. Name it |
| 75 | + `YYYY-0M-FinalizationMigration.sql`. |
| 76 | + - Typically migrations are designed to be run in sequence. However since the migrations in |
| 77 | + `DbScripts_finalization` can be run out of order, care must be taken to ensure they remain |
| 78 | + compatible with the changes to `DbScripts`. In order to achieve this we only keep a single |
| 79 | + migration, which executes all backwards incompatible schema changes. |
| 80 | + |
| 81 | +Upon execution any finalization scripts will be [automatically moved](./edd.mdx#Online-environments) |
| 82 | +for proper history. |
0 commit comments