Skip to content

Migrations

The Tech Strategy Tool uses EF Core migrations to manage the PostgreSQL database schema. Migrations are the sole source of truth for the schema.

  • Migrations live in src/TechStrat.Infrastructure/Persistence/Migrations/
  • In Development mode, migrations are auto-applied on startup via db.Database.MigrateAsync()
  • In Production, migrations should be applied explicitly before deploying

When you change EF Core entity models or the TechStratDbContext, create a new migration:

Terminal window
dotnet ef migrations add <MigrationName> \
--project src/TechStrat.Infrastructure \
--startup-project src/TechStrat.Api \
--output-dir Persistence/Migrations

Use descriptive PascalCase names that describe the change:

  • AddJiraKeyToInitiative
  • CreateSessionsTable
  • AddTargetIdIndex
  1. Modify the entity model in Infrastructure/Persistence/
  2. Run the migration command above
  3. Review the generated migration file
  4. Test by resetting the database: docker compose -f docker/docker-compose.yml down -v && docker compose -f docker/docker-compose.yml up -d
  5. Start the app and verify the migration applies cleanly

In Development mode (ASPNETCORE_ENVIRONMENT=Development), migrations are applied automatically when the application starts:

await db.Database.MigrateAsync();

No manual intervention is needed.

Apply migrations before deploying the new application version:

Terminal window
dotnet ef database update \
--project src/TechStrat.Infrastructure \
--startup-project src/TechStrat.Api

Or generate a SQL script for review:

Terminal window
dotnet ef migrations script \
--project src/TechStrat.Infrastructure \
--startup-project src/TechStrat.Api \
--output migration.sql

Install the EF Core CLI tools globally:

Terminal window
dotnet tool install --global dotnet-ef

Update to the latest version:

Terminal window
dotnet tool update --global dotnet-ef

Verify installation:

Terminal window
dotnet ef --version
Terminal window
dotnet ef migrations list \
--project src/TechStrat.Infrastructure \
--startup-project src/TechStrat.Api

Remove the last migration (if not applied)

Section titled “Remove the last migration (if not applied)”
Terminal window
dotnet ef migrations remove \
--project src/TechStrat.Infrastructure \
--startup-project src/TechStrat.Api

The simplest way to reset is via Docker:

Terminal window
docker compose -f docker/docker-compose.yml down -v
docker compose -f docker/docker-compose.yml up -d

This destroys all data and creates an empty database. The next application startup will apply all migrations automatically.

EF Core entity models are in src/TechStrat.Infrastructure/Persistence/:

EntityTableDescription
EventEntityeventsAppend-only event log
CheckpointEntitycheckpointsDocument snapshots
UserEntityusersUser accounts
SessionEntitysessionsLogin sessions
TableColumnType
eventssequence_numberUnique
eventstarget_idNon-unique
checkpointssequence_numberUnique
usersusernameUnique
sessionsuser_idNon-unique

Ensure you are specifying the correct --project and --startup-project paths.

The EF Core CLI builds the project before generating migrations. Fix any build errors first.

Ensure ASPNETCORE_ENVIRONMENT is set to Development. Auto-migration only runs in Development mode. For other environments, apply migrations manually with dotnet ef database update.