Back to all terms
ServerNode 1Node 2Infrastructure
Infraintermediate

Database Migration in CI/CD

Automate database schema changes as versioned migrations integrated into the CI/CD pipeline with rollback safety.

Also known as: database migrations, schema migrations, migration pipeline, automated migrations, DB migrations in CI

Description

Database migration in CI/CD is the practice of managing database schema changes as versioned, code-reviewed migration files that are automatically applied as part of the deployment pipeline. Each migration file contains an 'up' operation (applying the change) and a 'down' operation (reverting it), and migrations are tracked in a dedicated table to ensure each runs exactly once and in order. This replaces manual SQL execution with a repeatable, auditable process.

Migrations must be designed for zero-downtime compatibility when used with rolling or blue-green deployments. This means following an expand-contract pattern: first, add new columns/tables without removing old ones (expand), deploy application code that uses the new schema, then in a subsequent release remove the old columns/tables (contract). Column renames become add-new, backfill, deploy-dual-write, migrate-reads, drop-old sequences. Indexes should be created with CONCURRENTLY (PostgreSQL) to avoid table locks.

In the CI/CD pipeline, migrations typically run as a pre-deployment step in a dedicated job that connects to the target database with migration-specific credentials (least privilege -- ALTER, CREATE, but not DROP in most cases). The pipeline should include migration dry-run validation, backup verification, and automatic rollback triggers. For large tables, data migrations should be separated from schema migrations and run as background jobs to avoid long-running transactions that block other operations.

Prompt Snippet

Integrate Drizzle (or Prisma) migrations into the CI/CD pipeline as a dedicated pre-deployment GitHub Actions job. Generate migration files via drizzle-kit generate, require them in code review, and apply via drizzle-kit migrate with a connection string from CI secrets. Enforce the expand-contract pattern: PR linting must reject ALTER TABLE ... DROP COLUMN and ALTER TABLE ... RENAME in the same PR as application code changes. Create indexes with CREATE INDEX CONCURRENTLY and set lock_timeout=5s and statement_timeout=30s for migration sessions. Run migrations in a transaction with automatic rollback on failure, and verify migration status in the post-deploy health check.

Tags

databasemigrationsci-cdschemadeploymentautomation