Migration Architecture
Migrations are managed by the Migrator class library located inutil/Migrator/.
Understanding the Migrator
The core migration logic is inutil/Migrator/DbMigrator.cs:
src/Migrator/DbMigrator.cs
Key Features
- Automatic Database Creation - Creates the database if it doesn’t exist
- Retry Logic - Handles SQL Server upgrade mode automatically
- Transaction Support - Wraps migrations in transactions by default
- Dry Run Mode - Preview migrations without applying them
- Multiple Providers - SQL Server, MySQL, PostgreSQL support
Migration Folders
Migrations are organized in theutil/Migrator/DbScripts/ directory:
- DbScripts/ - Main migration scripts (SQL Server)
- DbScripts_transition/ - Transitional scripts for major changes
- DbScripts_finalization/ - Post-migration cleanup scripts
- MySql/ - MySQL-specific migrations
- Postgres/ - PostgreSQL-specific migrations
Running Migrations
Local Development
Production Deployment
In production environments, migrations typically run:- During Application Startup - Via
DatabaseMigrationHostedServicein the Admin service:
src/Admin/HostedServices/DatabaseMigrationHostedService.cs
- As Part of CI/CD - Before deploying application containers
Creating New Migrations
Determine migration type
Migrations are named with a timestamp prefix:Example:
2026-03-10-14-30_AddUserPreferences.sqlCreate the migration file
Create a new
.sql file in util/Migrator/DbScripts/:Always make migrations idempotent - they should handle cases where they’ve been partially applied.
Set the file as an Embedded Resource
Edit The wildcard pattern typically already includes all
util/Migrator/Migrator.csproj to include the new script:.sql files.Migration Best Practices
Make Migrations Idempotent
Always check if changes exist before applying:Use Transactions Appropriately
Most migrations run in transactions by default. For long-running operations, consider:Handle Large Data Migrations
For migrations affecting millions of rows:Maintain Backward Compatibility
When removing columns:- Step 1: Deploy code that no longer uses the column
- Step 2: After deployment, create migration to drop the column
Document Complex Migrations
Migration Execution Order
Migrations execute in this order:- DbScripts/ - Main schema changes
- DbScripts_transition/ - Transitional logic
- DbScripts_finalization/ - Cleanup and finalization
Troubleshooting
Migration Failed Mid-Execution
If a migration fails:-
Check the migration journal:
- Manually rollback changes if needed
- Fix the migration script
- The migrator will retry on next run
Script Upgrade Mode Error
The migrator automatically retries with 20-second delays. If persistent:Multiple Database Providers Out of Sync
Ensure all provider migrations are equivalent:Related Files
util/Migrator/DbMigrator.cs:15- Core migration logicutil/Migrator/SqlServerDbMigrator.cs:1- SQL Server-specific implementationsrc/Admin/HostedServices/DatabaseMigrationHostedService.cs:1- Production migration service
See Also
- DbUp Documentation
- Contributing Guide - Submit migration changes
- Testing Guide - Test migrations with integration tests