Skip to content

Database migrations

Jay Malhotra edited this page Apr 19, 2024 · 6 revisions

Whenever a change to the database model is made, it is necessary to generate a migration using the EF Core tools. This allows for the database schema to be updated based on the new model.

Creating a migration

Run the following command to create a migration called name:

dotnet ef migrations add name

Or you can explicitly specify the path to the database project to both -s and -p.

Read more at https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli

Naming convention

Migrations should be named after the feature they concern, e.g. they are applied against, and there is then a suffixed number to differentiate subsequent migrations in a version. Note that the EF Core tools will automatically prefix a timestamp. For example:

20231204191101_WeaponPassives.cs
20231210194604_DailyMissions.cs

Applying a migration

If the PostgresOptions.DisableAutoMigration configuration value is not set to true via appsettings or env variables, then migrations are applied automatically on startup. In other words, you don't have to worry about applying migrations manually in the development environment.

If you wish to deploy to production, you may find this resource from Microsoft about managing migrations to be useful. The recommended way is with SQL scripts that are carefully reviewed to ensure no data loss occurs, and of course with plenty of backups. The main deployment uses auto-migration and has not had any issues so far, however.

Clone this wiki locally