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.
Overview
Section titled “Overview”- 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
Creating a Migration
Section titled “Creating a Migration”When you change EF Core entity models or the TechStratDbContext, create a new migration:
dotnet ef migrations add <MigrationName> \ --project src/TechStrat.Infrastructure \ --startup-project src/TechStrat.Api \ --output-dir Persistence/MigrationsNaming conventions
Section titled “Naming conventions”Use descriptive PascalCase names that describe the change:
AddJiraKeyToInitiativeCreateSessionsTableAddTargetIdIndex
Example workflow
Section titled “Example workflow”- Modify the entity model in
Infrastructure/Persistence/ - Run the migration command above
- Review the generated migration file
- Test by resetting the database:
docker compose -f docker/docker-compose.yml down -v && docker compose -f docker/docker-compose.yml up -d - Start the app and verify the migration applies cleanly
Applying Migrations
Section titled “Applying Migrations”Development (automatic)
Section titled “Development (automatic)”In Development mode (ASPNETCORE_ENVIRONMENT=Development), migrations are applied automatically when the application starts:
await db.Database.MigrateAsync();No manual intervention is needed.
Production (manual)
Section titled “Production (manual)”Apply migrations before deploying the new application version:
dotnet ef database update \ --project src/TechStrat.Infrastructure \ --startup-project src/TechStrat.ApiOr generate a SQL script for review:
dotnet ef migrations script \ --project src/TechStrat.Infrastructure \ --startup-project src/TechStrat.Api \ --output migration.sqlEF Core CLI Setup
Section titled “EF Core CLI Setup”Install the EF Core CLI tools globally:
dotnet tool install --global dotnet-efUpdate to the latest version:
dotnet tool update --global dotnet-efVerify installation:
dotnet ef --versionCommon Operations
Section titled “Common Operations”List migrations
Section titled “List migrations”dotnet ef migrations list \ --project src/TechStrat.Infrastructure \ --startup-project src/TechStrat.ApiRemove the last migration (if not applied)
Section titled “Remove the last migration (if not applied)”dotnet ef migrations remove \ --project src/TechStrat.Infrastructure \ --startup-project src/TechStrat.ApiReset the database
Section titled “Reset the database”The simplest way to reset is via Docker:
docker compose -f docker/docker-compose.yml down -vdocker compose -f docker/docker-compose.yml up -dThis destroys all data and creates an empty database. The next application startup will apply all migrations automatically.
Entity Models
Section titled “Entity Models”EF Core entity models are in src/TechStrat.Infrastructure/Persistence/:
| Entity | Table | Description |
|---|---|---|
EventEntity | events | Append-only event log |
CheckpointEntity | checkpoints | Document snapshots |
UserEntity | users | User accounts |
SessionEntity | sessions | Login sessions |
Key indexes
Section titled “Key indexes”| Table | Column | Type |
|---|---|---|
events | sequence_number | Unique |
events | target_id | Non-unique |
checkpoints | sequence_number | Unique |
users | username | Unique |
sessions | user_id | Non-unique |
Troubleshooting
Section titled “Troubleshooting””No migrations were found”
Section titled “”No migrations were found””Ensure you are specifying the correct --project and --startup-project paths.
”Build failed”
Section titled “”Build failed””The EF Core CLI builds the project before generating migrations. Fix any build errors first.
Migration doesn’t apply on startup
Section titled “Migration doesn’t apply on startup”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.