migration-patterns
Skill2 starsChanged 6 months agoMITView on GitHub
Safe schema-migration patterns for systems under live traffic — expand/contract, backfill, double-write, shadow-read, online DDL. Use when authoring, reviewing, or sequencing a migration that can't take a maintenance window.
We split this skill into the rules it gives an agent
14 rules. 0 are common, 10 are distinctive, and 4 are very specific to this project.
0
Common
Most of the 4 edited versions we read keep these.
10
Distinctive
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 34
Never reverse this order.
- Very specific: line 38
Never ALTER TABLE ... RENAME COLUMN while code is live.
- Distinctive: line 41
Add a new column with the target type.
- Distinctive: line 45
Stop writing the old.
- Very specific: line 51
Never just CREATE INDEX on a hot table without CONCURRENTLY / online algorithm.
- Distinctive: line 54
Add the column first, without the constraint.
- Very specific: line 56
Add the constraint NOT VALID (Postgres) so new rows are checked.
- Distinctive: line 60
Create the new partitioned table alongside.
- Very specific: line 72
Avoid UPDATE ... WHERE condition on the whole table in one shot.
- Distinctive: line 87
When writers must update both old and new:
- Distinctive: line 90
If cross-store, use an outbox table — write to outbox in the transaction, publish async.
- Distinctive: line 97
Run both queries (old and new), compare results, log mismatches.
- Distinctive: line 99
Only cut over when mismatch rate ≈ 0 for a sustained window.
- Distinctive: line 103
Every migration has a reverse.
“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Migration patterns
Assume: the service is running, writes are arriving, you cannot take a maintenance window. Most production migrations live here.
The universal rule
Never couple a schema change to a code change in the same deploy. They fail independently, and you need each to be reversible independently.
Expand → Migrate → Contract
The safe three-phase dance for any non-trivial change:
- Expand — add the new shape alongside the old. No caller depends on it yet.
- Migrate — move data to the new shape. Switch readers. Switch writers. Backfill anything left.
- Contract — drop the old shape once nothing reads or writes it for a cooling-off period.
Each phase is a separate deploy. Each is independently revertable.
Common change patterns
Add a column
- Safe:
ALTER TABLE ... ADD COLUMN ... NULL. No lock (or brief, depending on engine). - Danger:
NOT NULLwith no default on a large table — full-table rewrite, long lock. Use a default (cheap if metadata-only in your engine) or expand/migrate/contract: add nullable → backfill → add NOT NULL constraint.
Drop a column
- Ship code that stops reading it. Deploy. Wait a release.
- Ship code that stops writing it. Deploy. Wait a release.
- Drop the column.
- Never reverse this order.
Rename a column
- Effectively: add new → dual-write → backfill → switch reads → stop writing old → drop old.
- Never
ALTER TABLE ... RENAME COLUMNwhile code is live. Callers break.
Change column type
- Add a new column with the target type.
- Dual-write (writers write both).
- Backfill the new column.
- Switch readers to the new column.
- Stop writing the old.
- Drop the old.
Add an index on a large table
- Postgres:
CREATE INDEX CONCURRENTLY— no table lock. Monitor for failure. - MySQL: online DDL since 5.6 for most index adds. Check
ALGORITHM=INPLACE, LOCK=NONE. - Never just
CREATE INDEXon a hot table withoutCONCURRENTLY/ online algorithm.
Add a foreign key
- Add the column first, without the constraint.
- Backfill valid values.
- Add the constraint
NOT VALID(Postgres) so new rows are checked. VALIDATE CONSTRAINTlater during low traffic.
Partitioning
- Create the new partitioned table alongside.
- Shadow-write to both.
- Backfill.
- Switch reads.
- Drop the old.
Backfill patterns
- Batch in chunks by primary key range. Size the chunk so a single chunk finishes in ~1s.
- Sleep between chunks. Leave room for user traffic and replication lag.
- Checkpoint progress in a table or file; a restart should resume, not re-scan.
- Track replica lag on Postgres/MySQL. If lag climbs, slow down.
- Avoid
UPDATE ... WHERE conditionon the whole table in one shot. It will bite.
Sketch:
-- in a loop, with checkpointing and sleep
UPDATE users
SET email_lower = LOWER(email)
WHERE id > $last_id
AND id <= $last_id + 10000
AND email_lower IS NULL;Dual-write safely
When writers must update both old and new:
- Apply the write to both inside the same transaction if they're in the same DB.
- If cross-store, use an outbox table — write to outbox in the transaction, publish async.
- Reject writes where the dual-write fails, unless the business accepts loss on the new path during rollout.
Shadow-read for confidence
Before switching reads:
- Run both queries (old and new), compare results, log mismatches.
- Fix mismatches in the backfill, not in the read path.
- Only cut over when mismatch rate ≈ 0 for a sustained window.
Reversibility
Every migration has a reverse. Write both up and down migrations. For destructive ops (drops, renames), the reverse might be "restore from backup" — state that explicitly, and get a snapshot before you run it.
Checklist before you run it
- Ran on a restored copy of prod-sized data? Measured duration.
- Identified locking behavior on this engine, for this size.
- Backup / snapshot present and verified.
- Rollback path documented.
- Oncall notified. Deploy freeze if needed.
- Feature flag / flagged query gate, so readers/writers can switch without redeploy.
Anti-patterns
- Migrations mixed into feature PRs. Can't revert independently.
DELETE FROM ... WHERE ...to "clean up" at scale — use batched archival instead.CREATE INDEXwithoutCONCURRENTLYon a live Postgres table — locks writes.SELECT *in backfill queries — read only what you need.- Running a migration in the ORM's auto-migrate mode in production. Use explicit migration files, versioned and reviewed.
Other versions4 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 4
- bybren-llc/safe-agentic-workflow★ 405same name
- duyet/clickhouse-monitoring★ 303same name
- dilolabs/nosia★ 214same name
- adyba/pi-safe-workflow★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository10 other files it gives its agents.
- a11y-checklistSkill
- api-designSkill
- context-engineeringSkill
- debug-playbookSkill
- observability-checklistSkill
- perf-checklistSkill
- pr-review-rubricSkill
- security-checklistSkill
- test-patternsSkill
- token-economySkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

