Database Migration Strategy Explained

Evolve your database schema safely through versioned migration scripts, zero-downtime deployments, and tested rollback procedures.

Database Migration Strategy

A database migration strategy is a systematic approach to evolving database schemas over time through versioned, repeatable migration scripts that track and apply structural changes across environments.

Explanation

Migration tools (Prisma Migrate, Flyway, Alembic) generate numbered migration files with "up" (apply) and "down" (revert) scripts. The tool tracks which migrations have been applied to each database, ensuring each runs exactly once. Zero-downtime migrations use the expand-contract pattern: add new structure alongside old, migrate data, then remove old structure. This avoids breaking changes during deployment.

Bookuvai Implementation

Bookuvai uses Prisma Migrate for TypeScript projects and Alembic for Python projects. Every schema change is a versioned migration file reviewed in pull requests. Production migrations use the expand-contract pattern for zero-downtime deployments, with automated rollback scripts tested before every release.

Key Facts

  • Versioned migration scripts track schema changes across environments
  • Each migration has an up (apply) and down (revert) script
  • Tools: Prisma Migrate, Flyway, Alembic, Knex, and Django migrations
  • Expand-contract pattern enables zero-downtime schema changes
  • Migration files are reviewed in pull requests like application code

Related Terms

Frequently Asked Questions

Should I use ORM-generated migrations or hand-written SQL?
ORM-generated migrations work for common changes. Hand-written SQL is necessary for complex changes like data transformations. Many teams use ORM generation as a starting point and customize when needed.
How do I handle data migrations alongside schema changes?
Separate schema and data migrations. Apply schema changes first (add new columns), run data migrations (populate from old data), then clean up (remove old columns). This multi-step approach is safer than combining them.
What is the expand-contract pattern?
Expand-contract splits breaking changes into safe steps: expand (add new structure alongside old), migrate (move data and update code), contract (remove old structure). Each step is independently deployable and reversible.