✓ Recommended by FindUtils

Database Migration Best Practices

Safe database migration strategies for zero-downtime deployments, rollbacks, and large-scale schema changes.

Claude CodeCursorGitHub CopilotWindsurfClineCodex / OpenAIGemini CLI
Updated 2026-04-05
CLAUDE.md
# Database Migration Best Practices

You are an expert in database migrations, schema evolution, and zero-downtime deployments.

Zero-Downtime Migrations:
- Never rename or drop columns in a single deploy; use expand-contract pattern
- Expand: add new column, backfill data, deploy code reading both columns
- Contract: remove old column after all code uses the new one
- Add new columns as nullable or with defaults; never add NOT NULL without default
- Create indexes CONCURRENTLY in PostgreSQL to avoid table locks

Migration Safety:
- Always test migrations against production-sized datasets
- Include both up and down migrations for rollback capability
- Run migrations in transactions where supported (PostgreSQL yes, MySQL DDL no)
- Set statement timeouts to prevent long-running DDL from blocking
- Never run data migrations and schema migrations in the same file

Large Table Migrations:
- Use pt-online-schema-change (MySQL) or pg_repack (PostgreSQL) for ALTER on large tables
- Backfill data in batches with rate limiting (1000 rows per batch, 100ms delay)
- Use triggers or CDC to keep old and new columns in sync during migration
- Monitor replication lag during backfill operations
- Schedule heavy migrations during low-traffic windows

Rollback Strategy:
- Every migration must be reversible or have a documented manual rollback
- Test rollback in staging before applying to production
- Keep the previous application version deployable for 24 hours post-migration
- Store migration checksums to detect tampering
- Use feature flags to decouple code deploys from schema changes

Tooling:
- Use framework migration tools: prisma migrate, knex migrate, alembic, flyway
- Version migrations sequentially with timestamps (20260405120000_add_users)
- Run pending migrations automatically in CI/CD pipeline
- Keep a migration log with author, date, and purpose

Add to your project root CLAUDE.md file, or append to an existing one.

Tags

migrationszero-downtimeschema-evolutionrollbackdeploy