Skip to content

Commit 717e06c

Browse files
committed
Improve database management documentation
1 parent c23dd27 commit 717e06c

File tree

7 files changed

+126
-112
lines changed

7 files changed

+126
-112
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
label: "Database Migrations"
2+
position: 2

docs/contributing/database-migrations/edd.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ will not be run until the start of the next deploy when they are moved into `DbS
265265
After this deploy, to prep for the next release, all migrations in `DbScripts_transition` are moved
266266
to `DbScripts` and then all migrations in `DbScripts_finalization` are moved to `DbScripts`,
267267
conserving their execution order for a clean install. For the current branching strategy, PRs will
268-
be open against `main` when `rc` is cut to prep for this release. This PR automation will also
269-
handle renaming the migration file and updating any reference of `[dbo_finalization]` to `[dbo]`.
268+
be open against `main` when `rc` is cut to prep for this release.
270269

271270
The next deploy will pick up the newly added migrations in `DbScripts` and set the previously
272271
repeatable _Transition_ migrations to no longer be repeatable, execute the _Finalization_
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Entity Framework
6+
7+
:::info
8+
9+
For instructions on how to apply database migrations, please refer to the
10+
[Getting Started](../../getting-started/server/database/ef/index.mdx) documentation.
11+
12+
:::
13+
14+
If you alter the database schema, you must create an EF migration script to ensure that EF databases
15+
keep pace with these changes. Developers must do this and include the migrations with their PR.
16+
17+
To create these scripts, you must first update your data model in `Core/Entities` as desired. This
18+
will be used to generate the migrations for each of our EF targets. Additionally, for table changes
19+
it is strongly recommended to define or update an `IEntityTypeConfiguration<T>` to accurately
20+
represent any constraints needed on the data model.
21+
22+
Once the model is updated, navigate to the `dev` directory in the `server` repo and execute the
23+
`ef_migrate.ps1` PowerShell command. You should provide a name for the migration as the only
24+
parameter:
25+
26+
```bash
27+
pwsh ef_migrate.ps1 [NAME_OF_MIGRATION]
28+
```
29+
30+
This will generate the migrations, which should then be included in your PR.

docs/contributing/database-migrations/index.md

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.

docs/getting-started/server/database/ef/index.mdx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ each.
4040

4141
Our EF implementations currently support Postgres, MySQL, and SQLite3.
4242

43-
## Setting Up EF Databases
43+
## Creating the database
4444

45-
The workflow here is broadly the same as with the normal MSSQL implementation: set up the docker
45+
The workflow here is broadly the same as with the normal MSSQL implementation: set up the Docker
4646
container, configure user secrets, and run migrations against their relating databases in
4747
chronological order.
4848

@@ -139,7 +139,7 @@ that the changes take effect.
139139

140140
:::
141141

142-
### Migrations
142+
## Updating the database
143143

144144
<Tabs
145145
groupId="provider"
@@ -185,7 +185,7 @@ You can also run migrations for all database providers at once using
185185
pwsh migrate.ps1 -all
186186
```
187187

188-
### Optional: Verify
188+
### Verifying changes
189189

190190
If you would like to verify that everything worked correctly:
191191

@@ -194,7 +194,7 @@ If you would like to verify that everything worked correctly:
194194
- Note: this requires a configured MSSQL database. You may also need to set up other EF providers
195195
for tests to pass.
196196

197-
## Testing EF Changes
197+
## Testing changes
198198

199199
In your `server/dev/secrets.json` file find or add this block of secrets in the root of the json
200200
structure:
@@ -230,3 +230,8 @@ existing databases if you have not already. If these settings are not present at
230230
With connection strings applied to your projects: ensure your databases are all migrated using
231231
`pwsh server/dev/migrate.ps1 --all`. Then you can run EF tests from the
232232
`test/Infrastructure.IntegrationTest` folder using `dotnet test`.
233+
234+
## Modifying the database
235+
236+
The process for modifying the database is described in
237+
[Migrations](./../../../../contributing/database-migrations/ef.md).

docs/getting-started/server/database/mssql/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ of your database.
2323
## Modifying the database
2424

2525
The process for modifying the database is described in
26-
[Migrations](./../../../../contributing/database-migrations/).
26+
[Migrations](./../../../../contributing/database-migrations/mssql.md).

0 commit comments

Comments
 (0)