Skip to content
DatabaseChapter 33 / 57Advanced7 min read

Safe Migrations

Database migrations best practices. Write idempotent, non destructive and reversible migrations, and review drops, renames and backfills by hand.

On this page

1. What is this practice?

A migration is a file that moves the database schema from one state to another, like adding a new column. These files live in version control so that every environment (local, staging, production) is built the same way, in the same order.

2. Why does it matter?

Real outages caused by migrations:

  • A rename took the app down: name was renamed to full_name. The new code took two minutes to deploy, and during those two minutes the old code was still looking for name. Every request returned 500.
  • A lock stopped the site: the type of a column on a big table was changed. Postgres rewrote the whole table and held a lock, and all writes stopped for 10 minutes.
  • One query, all the data: someone ran update users set plan = 'free' on the production console and forgot the where. Every paying customer became free.
  • Ran twice and blew up: a migration failed halfway, and when it was run again it got stuck on table already exists, and the deploy was blocked.
Casual migrationsSafe migrations
Errors when run againif not exists, safe to run again
Rename and drop in one stepExpand and contract, across several deploys
No way to go backA written down for every up
Hand written SQL on productionFrom CI or a deploy step, after a backup
Editing a merged migrationWriting a new migration
App code creates tables on requestSchema lives only in migration files

3. When to follow it

LevelHow importantWhy
Learning projectLearn to use migration filesCreating tables by hand builds a bad habit
Production codeRequired, with every conditionProduction data does not come back
Senior levelPlan and review destructive changesDecide expand contract, backfills and rollback

When to learn it: the first time you need to add a column to a table in a deployed app. Read Database Naming & Design first.

4. Bad vs good code

Bad: destructive, not idempotent, no down

sql
alter table users rename column name to full_name;
alter table users drop column legacy_role;
alter table orders alter column total type numeric(12, 2);
create type plan_tier as enum ('free', 'pro');

The rename immediately breaks the old code. The drop deletes data forever. The type change locks a big table. Running it again makes create type fail. There is no file to go back.

This is also bad, schema inside app code:

ts
export async function saveLead(lead: Lead) {
  await sql`create table if not exists leads (id serial primary key, email text)`;
  await sql`insert into leads (email) values (${lead.email})`;
}

Now the schema lives in two places, migrations and code, and the two will never match.

Good: additive, idempotent, with a down

File migrations/20261101093000_add_plan_tier_to_users.sql:

sql
do $$
begin
  create type plan_tier as enum ('free', 'pro');
exception
  when duplicate_object then null;
end
$$;

alter table users add column if not exists plan_tier plan_tier not null default 'free';

create index if not exists users_plan_tier_idx on users (plan_tier);

File migrations/20261101093000_add_plan_tier_to_users.down.sql:

sql
drop index if exists users_plan_tier_idx;
alter table users drop column if exists plan_tier;
drop type if exists plan_tier;

Run this up migration twice and check that the second run does nothing. This simple test proves idempotency. The down file is for development and staging. Running a down in production is also a deliberate decision, because data is lost.

The right way to rename: expand and contract

Renaming a column with no downtime
Deploy 1: add the new column full_name (additive)
Deploy 2: code writes to both columns, reads from the old one
Backfill script: copy old rows into full_name, with a test
Deploy 3: code reads and writes only full_name
Deploy 4: after human review, drop the old column

What is the difference?

ThingBadGood
Running againErrorif not exists, guarded do block
DataDrop and rename immediatelyOnly add, drop later by a human
RollbackNoneA .down.sql file
NameSays nothingTimestamp and the name of the job
Where the schema livesCode and migrations bothOnly migrations

5. How to apply it

Three questions before every migration:

QuestionIf the answer is "no"
Does it avoid deleting or changing existing data?It is destructive, a human reviews and applies it
Is it safe to run twice?Add if not exists and guarded blocks
Is its down path written?Write a down file, or write down why it is not possible

Only if all three answers are "yes" can it go through the automatic pipeline. If even one is "no", the file stays in the PR and a human applies it by hand after a backup.

Other rules:

  • One migration, one change, and the name says the job: add_plan_tier_to_users, not create_alter_users.
  • Never edit a merged migration. If you made a mistake, write a new migration.
  • Migrations run from CI or a deploy step, after a health check and a backup. App code does not create schema on request.
  • A dry run on the pull request (like supabase db push --dry-run) so the review knows what will run.
  • Staging and production must never share one database.
  • On a big Postgres table, build the index with create index concurrently so writes do not stop.

6. Exceptions and common mistakes

On a local database that is only yours, it is perfectly fine to reset and run the migrations from scratch. On a brand new table with no data yet, drop or rename is less dangerous, but still do it through a file and review so other environments match.

Mistake: Running SQL directly on the production console

Why? No review, no backup, no record, and one missing where clause changes the whole table.

Better: Put every change in a migration or script file, run it after PR review and after taking a backup.

Mistake: Renaming a column in a single deploy

Why? During the deploy the old code looks for the old name and every request fails.

Better: Expand and contract: add the new column, write to both, backfill, then remove the old one.

Mistake: Changing a merged migration file

Why? Environments where it already ran never get the new version, and the schema drifts.

Better: Always write a new migration that fixes the earlier mistake.

Mistake: Hiding a backfill inside a migration

Why? Updating millions of rows holds a long lock, and if it fails, half the data is changed and nothing was tested.

Better: Put the backfill in a separate script, in batches, with a test, and under human supervision.

7. What to learn before and after

Final quick revision

CheckQuestion
IdempotentIs it safe even when run twice?
Non destructiveIs any data deleted or changed?
ReversibleIs the down path written?
DestructiveWere drop, rename, type change and backfill reviewed and applied by a human?
ProductionDid the change go through CI, after a backup, with no console?
EditWas any merged migration touched?