Database Migration Testing: Protect Data Through Every Release

Database changes are among the riskiest parts of a release because application code and stored data evolve together. A migration can pass on a small developer database and still fail on production volume, lock critical tables, corrupt edge case records, or make rollback impossible. Treat migrations as production code with explicit compatibility, integrity, and recovery tests.

Test the transition, not only the final schema

A migration test should validate the starting state, the transformation, and the resulting state. Testing only that the new column exists does not prove that existing records were converted correctly or that old application versions can continue operating during a rolling deployment.

Build fixtures that represent realistic historical data, including nulls, duplicate values, unusual encodings, large fields, and records created by older application versions. Keep a small fast fixture set for pull requests and a larger volume dataset for scheduled tests.

Use expand and contract for compatibility

For changes that must coexist with old and new application versions, prefer an expand and contract sequence. First add the new schema in a backward compatible way. Deploy code that can read the old and new representations. Backfill data in controlled batches. Switch reads and writes after validation. Remove the old schema only when no active code depends on it.

This approach reduces the risk of a deployment requiring application code and schema changes to happen atomically. Test each stage as a separate state transition, including the period when two application versions may run simultaneously.

Verify data integrity with invariants

Define invariants before writing the migration. Examples include row counts within expected bounds, uniqueness constraints, referential integrity, non null requirements, preserved monetary totals, and valid status transitions. Compare old and new representations using deterministic queries or checksums where appropriate.

For transformations, test boundary values and idempotency. Running a safe backfill twice should not duplicate or damage data. If a migration cannot be rerun, document how partial execution is detected and resumed safely.

Test production scale and locking behaviour

A migration that completes in seconds on a local database may scan millions of rows in production. Measure execution time and lock duration against a dataset representative of production size. Check index creation behaviour, transaction boundaries, replication lag, disk usage, and connection pool pressure.

Prefer bounded batches for large backfills, with checkpoints and observable progress. Test interruption and restart. A migration should not silently leave half transformed data with no reliable recovery procedure.

Validate application compatibility

Test the application against each schema state that can exist during deployment. This includes old code with the expanded schema, new code before the backfill is complete, and new code after the migration is complete. Verify reads, writes, background jobs, reporting queries, and administrative tools.

Pay particular attention to asynchronous consumers and scheduled jobs. They may continue running old code after the web application has been updated. Contract tests can help verify the boundaries between services that depend on shared data structures. See the microservice contract testing guide.

Plan rollback and forward recovery

A database rollback is not always the reverse of a migration. Once data has been transformed or new writes have occurred, reverting the schema may destroy information. For each change, decide whether rollback is genuinely safe or whether forward recovery is the correct strategy.

Test the recovery procedure, not just the happy path. Simulate a failed deployment after the schema expansion, a partially completed backfill, and a failed constraint validation. Confirm that operators can identify the current state and continue without guessing. Backups are essential, but restoring a large database may take longer than the service can tolerate.

Add migrations to the delivery pipeline

A robust pipeline should validate migration syntax, apply migrations to a clean database, upgrade from representative older schema versions, run integrity assertions, and execute application compatibility tests. For risky changes, run a volume test and inspect the query plan or lock behaviour in a production like environment.

Store migration logs and duration as build artifacts. Require review from the team that owns the data. Keep schema changes small and observable so failures can be attributed to a specific transition.

A practical migration checklist

Before release, confirm that the migration has a tested starting state, explicit invariants, a volume estimate, a deployment sequence, and an owner. During rollout, monitor duration, locks, replication lag, error rates, and application compatibility. After rollout, verify data counts and business level totals before removing the old schema.

Include migration health in the broader automation debt and reliability review. A technical debt review can reveal missing database fixtures, fragile setup scripts, and undocumented dependencies that make migrations harder to test.

QA AUTOMATION HEALTH CHECK

Know what to improve first.

Get an independent review of test reliability, automation architecture, and CI with prioritized findings and a practical 30 day plan.

Explore the review →