Key points
- A migration that runs instantly on your laptop can block a large production table for minutes.
- Split it: add the column, backfill in batches, then add the constraint.
- Ask for explicit
ALGORITHMandLOCKclauses so MySQL fails loudly instead of locking quietly.
Ask an AI assistant to “add a currency column to orders and fill it in” and you will usually get a tidy-looking migration. It runs in a second against a dev database holding 200 orders. But the assistant knows nothing about the size of your tables, your traffic or how you deploy. On our projects, reviewing generated migrations has become a step of its own, with a dedicated checklist.
What the assistant typically writes
A common result, perfectly valid SQL:
-- Proposed by the assistant, in a single migration
ALTER TABLE orders ADD COLUMN currency CHAR(3) NOT NULL DEFAULT 'EUR';
UPDATE orders o JOIN shops s ON s.id = o.shop_id SET o.currency = s.currency;
ALTER TABLE orders MODIFY total BIGINT NOT NULL;
-- down
ALTER TABLE orders DROP COLUMN currency;
There are three issues hiding in it. The UPDATE rewrites the whole table in one transaction, locking a huge number of rows and leaving replicas lagging. Changing the type of total rebuilds the entire table and blocks writes while it does. And the down migration drops the column: roll back a few hours later and every currency recorded in between is gone.
Split it into separately deployable steps
We nearly always break this kind of change into three parts: add a nullable column, backfill it, then enforce the constraint.
-- Migration 1: instant, no table copy (MySQL 8)
ALTER TABLE orders ADD COLUMN currency CHAR(3) NULL, ALGORITHM=INSTANT;
-- Migration 2 (another deployment): rebuilds the table, schedule it off-peak
ALTER TABLE orders MODIFY currency CHAR(3) NOT NULL DEFAULT 'EUR';
Spelling out ALGORITHM=INSTANT or LOCK=NONE has a real benefit: if the operation cannot run that way, MySQL refuses instead of silently picking a blocking method. It is a cheap safety net.
Between the two migrations, the application must cope with an empty currency, for example by falling back to the shop’s default.
Backfill in batches, outside the migration
The backfill belongs in a dedicated script or command, working through ID ranges:
$stmt = $pdo->prepare(
'UPDATE orders o JOIN shops s ON s.id = o.shop_id
SET o.currency = s.currency
WHERE o.currency IS NULL AND o.id > :from AND o.id <= :to'
);
$max = (int) $pdo->query('SELECT MAX(id) FROM orders')->fetchColumn();
for ($from = 0; $from < $max; $from += 5000) {
$stmt->execute(['from' => $from, 'to' => $from + 5000]);
usleep(200_000); // let replication and live traffic breathe
}
It is safe to rerun: the currency IS NULL condition skips rows already done.
Our review checklist
- How many rows does the production table hold? The assistant cannot know, so put the order of magnitude in your prompt.
- Does the statement rebuild the table? Changing a type, nullability or character set usually does.
- Does the down migration destroy data?
- Does the code deployed before and after work with both schemas?
- Was it tested on a full-size copy of the database rather than fixtures?
FAQ
Should we stop letting AI write migrations?
No. It saves time on syntax and sometimes remembers an index you forgot. Give it the context (table size, MySQL version, zero-downtime deploys) and review it against the list above.
What about PostgreSQL?
Same reasoning, different blocking operations. Check the docs for your version, for instance index creation with CONCURRENTLY.
Evolving a database that cannot go offline? We design and review migrations for zero-downtime releases.



