Skip to main content

Databases

What is a Database Migration?

Definition

A database migration is a versioned script that changes a schema, so every environment reaches the same structure by applying the same steps in the same order.

Also known as: Schema migration

Database Migration in more detail

Application code is versioned in Git, and a database that is modified by hand is not — which is how staging and production drift apart until a deploy fails in the one environment that matters. Migrations fix that by making each schema change a file: create this table, add that column, backfill these rows. The tool records which files have run in a metadata table, so applying them is repeatable and a fresh database can be built from nothing by replaying the whole history.

Most frameworks generate a pair of directions, an up that applies the change and a down that reverses it. Down migrations are useful in development and much less trustworthy in production, because dropping a column that has been written to since is data loss, not a rollback. The safer production discipline is that migrations only ever move forward, and a mistake is corrected by a new migration.

Zero-downtime deployment forces one more rule: for a while, old and new application code run against the same database. A migration must therefore be compatible with both, which is the expand-and-contract pattern — add the new column, deploy code that writes to both, backfill, deploy code that reads the new one, and only then drop the old. Renaming a column in a single step, by contrast, breaks every instance that has not been replaced yet. On large tables also watch the lock: an unqualified ALTER can block writes for the duration, so use the concurrent or online variants your engine provides and backfill in batches.

Database Migration example

db/migrate/
  20260301090000_create_orders.sql
  20260304143000_add_status_to_orders.sql
  20260311101500_backfill_order_status.sql

-- 20260304143000_add_status_to_orders.sql
-- up
ALTER TABLE orders ADD COLUMN status TEXT;      -- nullable: no rewrite
CREATE INDEX CONCURRENTLY idx_orders_status ON orders (status);
-- down
DROP INDEX IF EXISTS idx_orders_status;
ALTER TABLE orders DROP COLUMN status;

Expand and contract — a rename with zero downtime
  1. add  full_name  (leave  name  in place)
  2. deploy code writing BOTH columns
  3. backfill full_name in batches
  4. deploy code reading full_name
  5. a later migration drops  name

  Renaming in one step breaks every instance still running
  the previous release.

Database Migration — common questions

What is the difference between a migration and a seed?
A migration changes structure — tables, columns, indexes, constraints. A seed inserts data, usually reference data or sample records for development. Keeping them separate means you can rebuild a schema without importing anyone's test data.
Should migrations be reversible?
In development, yes, it is convenient. In production, treat forward-only as the rule: a down migration that drops a column destroys data written since it ran, so correcting a mistake with a new migration is safer than rolling back.
How do I add a NOT NULL column without downtime?
In steps: add it nullable, deploy code that writes it, backfill existing rows in batches, then add the constraint. Adding NOT NULL with a default in one statement can rewrite or lock a large table, depending on the engine and version.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary