Write a migration and its rollback
Produces the forward change and the way back, with the locking risk stated.
Write a database migration for this change. Change: [WHAT SHOULD BE DIFFERENT] Database: [POSTGRES / MYSQL / SQLITE / OTHER] · approximate rows in affected tables: [NUMBER] Deploy style: [ROLLING / MAINTENANCE WINDOW] Give me: 1. The forward migration. 2. The rollback. If the change is genuinely irreversible, say so plainly rather than writing a rollback that loses data quietly. 3. **What it locks and roughly for how long** at the row count I gave you. 4. Whether the currently-running code survives it. On a rolling deploy the old version runs alongside the new one — dropping or renaming a column breaks it immediately. 5. If it needs splitting across releases to be safe, write out the releases. Do not produce a migration whose safety depends on there being no traffic unless I said there is a maintenance window.
Highlighted parts are placeholders. Replace them before you send.
Worked example
The input is invented for this page. The output shows what the prompt asks for with that input.
Example input (invented)
Change: Orders need a currency. Today every order is in euros and nothing stores that. Add a three-letter currency code to each order, required, with existing orders set to EUR. Next quarter we start taking orders in US dollars.
Database: Postgres 16
Approximate rows in affected tables: 3.2 million in orders
Deploy style: Rolling. Old and new versions of the app run side by side for about ten minutes.
Example output
1. Forward migration
SET lock_timeout = '3s';
ALTER TABLE orders
ADD COLUMN currency char(3) NOT NULL DEFAULT 'EUR';
ALTER TABLE orders
ADD CONSTRAINT orders_currency_format CHECK (currency ~ '^[A-Z]{3}$') NOT VALID;
ALTER TABLE orders VALIDATE CONSTRAINT orders_currency_format;
2. Rollback
ALTER TABLE orders DROP CONSTRAINT orders_currency_format;
ALTER TABLE orders DROP COLUMN currency;
This is safe only while every order is in euros. Once one dollar order exists, dropping the column deletes the only record that it was in dollars. Check first, and if the count is not zero, roll back the code and leave the column:
SELECT count(*) FROM orders WHERE currency <> 'EUR';
3. Locks
ADD COLUMNwith a constant default does not rewrite the table on Postgres 11 or later. It takes an exclusive lock for about a millisecond. The risk is waiting: behind a long-running query, it queues and every query onordersqueues behind it.lock_timeoutmakes it fail after 3 seconds instead, and you retry.VALIDATEmakes one scan over all 3.2 million rows to check the constraint. Its lock still allows reads and writes, so nothing waits on it. Time it on a copy of production before you run it there.
4. Does the running code survive? Yes. The old version inserts without a currency and gets EUR, which is true for every order it creates.
5. Releases. The trouble starts with the first dollar order. During a rolling deploy the old version would read it and show the amount as euros. So:
- Release 1: this migration, plus code that reads and displays
currency. Still euros only. - Release 2, after release 1 runs everywhere: allow other currencies.
More Data prompts
3 of the 5 other prompts filed under Data.
Write the SQL and explain what it will do
Produces the query plus how it will actually execute at your row counts.
Design a schema from a description
Turns a domain in prose into tables, keys and the constraints that protect them.
Explain and fix a spreadsheet formula
Understand a formula you inherited, then repair it.
Data tools and skills
1 tool and 3 skills are filed under Data, the same category as this prompt.
Julius AI
Chat with a spreadsheet and get analysis plus charts.
Data cleaner
Profiles a messy dataset and proposes fixes before changing anything.
Data table analyst
Reads a table and reports what changed, with the caveats left in.
Migration safety checker
Finds the database migrations that will lock a table or break running code.