Migration Planning
Principles
- Forward-only: never modify a migration that has been applied
- Test on staging: always run migrations on a copy of production data first
- Have a rollback: every migration needs a tested rollback plan
- Make it reversible: add column → deploy → remove column. Never done in one step
Schema Migration
- Small, atomic changes per migration (one ALTER TABLE per file)
- Add columns as nullable first, backfill data, then add NOT NULL constraint
- Rename columns: add new column → dual-write → backfill → switch reads → drop old
- Run migrations during low-traffic periods
- Lock timeouts to prevent production impact
Data Migration
- Back up before starting
- Run in batches with progress logging
- Verify row counts match after migration
- Test rollback on a copy of the data
Service Migration (Strangler Fig)
- Build new service alongside old
- Route a small percentage of traffic to new service
- Monitor for errors, latency, data consistency
- Gradually increase traffic
- Cut over completely when confident
- Decommission old service