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?
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
- 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. - Does it lock? Adding an index without the concurrent option, adding a column with a volatile default, or adding
NOT NULLto a big table can block reads and writes while the database scans or rewrites it. Tools such asstrong_migrationslist these cases and the safe form of each. - 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.
- Is the data moved? A new column with no backfill is empty for every existing row. Backfill in small batches, not one giant
UPDATE. - Is there a way back? A
downstep 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.
- Expand: add the new column, nullable, with no default.
- Migrate: the app writes to both columns; a batched job copies old values.
- Switch: the app reads from the new column only.
- 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
upstep and never thedownstep. - 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?
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?
Practice items, not counted towards your mastery.
Start here
- strong_migrations: catch unsafe migrations in development (opens in a new tab)ToolFree
A catalogue of unsafe migration operations with the safe alternative for each.
- Parallel Change (martinfowler.com) (opens in a new tab)ArticleFree
The expand, migrate, contract pattern behind safe schema changes.
- terraform plan command (HashiCorp docs) (opens in a new tab)DocsFree
What plan output means and how to save and review a plan before apply.