Skip to content
mentoor

Topic 1 of 5 · 5 min read

Destructive and locking database migrations

Goal: Find the step in an AI-written migration that loses data or locks a large table and rewrite it as a safe expand-and-contract sequence.

A migration is the one kind of code you cannot roll back by redeploying: review it for lost data and held locks before you review anything else.

Check yourself · 1 of 3

Which statement is destructive for running code even though it looks harmless?

Choose one answer

Why it matters

A bad function is fixed by the next deploy. A bad migration may delete rows for good, or freeze every query on a busy table while it runs. Agents such as Claude Code, Cursor or Copilot write migrations that work on an empty development database. Production is where they hurt.

What to check, in order

  1. Does anything get destroyed? Look for DROP COLUMN, DROP TABLE, TRUNCATE, a column type change, or a rename. A rename is a drop plus an add as far as running code is concerned.
  2. Does it lock? Adding an index without the concurrent option, adding a column with a volatile default, or adding NOT NULL to a big table can block reads and writes while the database scans or rewrites it. Tools such as strong_migrations list these cases and the safe form of each.
  3. Does old code still work? During a deploy, old and new app versions run at the same time. If the migration removes a column the old version reads, requests fail mid-deploy.
  4. Is the data moved? A new column with no backfill is empty for every existing row. Backfill in small batches, not one giant UPDATE.
  5. Is there a way back? A down step that drops the new column loses whatever was written to it. Decide what rollback means before you merge.

Expand and contract

Split one risky change into several safe ones, shipped in separate deploys. This is the Parallel Change pattern.

  1. Expand: add the new column, nullable, with no default.
  2. Migrate: the app writes to both columns; a batched job copies old values.
  3. Switch: the app reads from the new column only.
  4. Contract: after one release with no reads of the old column, drop it.

Before and after

-- Agent: "rename users.fullname to users.display_name"
ALTER TABLE users RENAME COLUMN fullname TO display_name;

The old app version still selects fullname and starts failing the moment this runs. The safe version adds display_name, backfills it in batches, switches reads, and drops fullname in a later release.

What goes wrong

  • The agent tests on a small seed database, so a table rewrite that takes minutes in production looks instant.
  • The prompt said "clean up unused columns" and the agent guessed which columns are unused. Search the code and the logs; do not trust the guess.
  • The migration is correct but the backfill and the code change are in the same deploy, so old servers break.

Examples

A rename that breaks the old version

An agent writes ALTER TABLE orders RENAME COLUMN total TO amount_cents; and updates the code. During the rolling deploy, servers on the old release still query total and return errors for several minutes. The fix is to add amount_cents, write both, backfill, switch reads, and drop total one release later.

The unsafe index

CREATE INDEX idx_events_user ON events (user_id);

On PostgreSQL this blocks writes to events while it builds. The safe form is CREATE INDEX CONCURRENTLY, which the agent omitted because it worked on the empty dev database.

Key points

  • Treat DROP, TRUNCATE, type changes and renames as data loss until proven otherwise.
  • Check for locks: concurrent indexes, defaults and NOT NULL on large tables need a safe form.
  • Old and new app versions run side by side during a deploy, so a migration must work for both.
  • Split risky changes into expand, migrate, switch and contract, each in its own deploy.
  • Ask what the rollback does to data written since the migration ran.

Common mistakes

  • Running the migration only on an empty or tiny development database and calling it safe.
  • Reading the up step and never the down step.
  • Putting the schema change, the backfill and the code switch in one deploy.
  • Accepting the agent's claim that a column is unused without searching code and queries.

Check yourself · 2 of 3

What is the safe first step when replacing a column?

Choose one answer

Check yourself · 3 of 3

An agent's migration adds a column with NOT NULL to a very large live table. What should you ask first?

Choose one answer

Practice items, not counted towards your mastery.

Start here