schema-migrations
Skill1 starsChanged 2 months agoMITView on GitHub
Evolve a live database schema across Postgres, MySQL and SQLite without downtime or data loss - expand-contract rewrites, backfill versus constrain, rollback and forward-fix rules, and the tool-specific commands for Alembic, Flyway, Prisma, golang-migrate, Django, Rails, Knex and Liquibase. Use when someone asks to add/rename/drop a column or table, when a deploy is blocked on a migration, when a migration failed halfway in production, or when an index or constraint locks a hot table. Triggers on "migration", "alter table", "add column", "schema change", "rollback", "zero downtime", "DDL lock".
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 3 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 62
volatile: default (now(), gen_random_uuid()) rewrites the entire table.
- Very specific: line 136
If the new column is NOT NULL from the start, the dual-write app keeps it filled and the backfill is a safety net for rows written by …
- Distinctive: line 177
Run the migration against production first (it is backward compatible with the old app by construction), then deploy the app.
- Very specific: line 182
Verify with a read-only query (SELECT count(*) ...
- Distinctive: line 184
Only then drop the old column.
- Distinctive: line 186
If a migration fails halfway, do not edit the file.
- Very specific: line 217
After creating it, check the plan actually uses it (see postgres-query-tuning).
- Very specific: line 225
Read references/tool-workflows.md for the exact CLI for Alembic, Flyway, Prisma, golang-migrate, Django, Rails, Knex, and Liquibase, …
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Schema Migrations
Change a live schema without a maintenance window and without losing data. The output is a set of migration files, a written rollback, and a rehearsal against production-sized data.
Non-negotiable rules
- Never edit a migration that has been applied anywhere but your laptop. Checksums will not match and every environment diverges. Roll forward with a new migration; the ledger is append-only.
- Every migration ships with a rollback, or a documented forward-fix. An irreversible migration (dropped column, dropped table, narrowed type) must say so in its header and name the forward-fix that replaces the rollback.
- Assume the deploy and the migration are two different events. Code must work with both the old and the new schema during the window between them, or you have to sequence them deliberately.
- One logical change per migration. A migration that adds a column and backfills and drops the old column cannot be bisected or partially rolled back.
Workflow
- 1. Classify the change: additive / constraint / type change / removal
- 2. Check the lock it takes, per engine (table below)
- 3. Write expand → migrate → contract as separate deploys
- 4. Write the migration file and its rollback
- 5. Rehearse on a production-sized copy, timed
- 6. Deploy expand, backfill, then contract — with a verified checkpoint between
Step 1 — classify the change
| Change | Online? | Default approach |
| Add nullable column | Yes, all three engines | Single migration |
| Add column with constant default | PG 11+ yes; MySQL 8.0 instant for some cases; SQLite no | Split: add nullable, backfill, then set default |
Add NOT NULL | No — scans and validates | NOT VALID + VALIDATE, then SET NOT NULL |
| Add foreign key | No by default | NOT VALID, then VALIDATE CONSTRAINT |
| Add check constraint | No by default | NOT VALID, then VALIDATE CONSTRAINT |
| Add index | PG: CONCURRENTLY; MySQL 8.0: online DDL; SQLite: build a new table | Separate migration, never inside a transaction with other DDL |
| Rename column/table | No | Expand-contract with two columns; never rename in place |
| Change column type | Rewrites the table (PG 11+ narrows in place, but breaks large values) | New column, backfill, swap, drop old |
| Drop column/table | Yes but unrecoverable | Separate deploy, after the data is provably unused |
Step 2 — what each engine actually does
PostgreSQL
ACCESS EXCLUSIVEis required for mostALTER TABLEforms. It does not block reads, but it blocks every subsequent read while it waits behind the lock queue — one slowALTERcan stall a whole database.- Adding a nullable column: metadata only, instant.
- Adding a column with a constant default: instant since PG 11. A
volatile default (
now(),gen_random_uuid()) rewrites the entire table. Expand with no default, backfill, thenSET DEFAULT. CREATE INDEX CONCURRENTLY: no write lock, two table scans, cannot run inside a transaction block, and can leave anINVALIDindex behind. Retry withDROP INDEX CONCURRENTLYfirst.- Set
lock_timeouton every connection that runs DDL so a queued lock fails fast instead of stacking up:SET lock_timeout = '3s'; SET statement_timeout = '0'; -- DDL legitimately runs long; lock wait must notERROR: canceling statement due to lock timeoutmeans your transaction would have blocked others; retry, do not raise the timeout and wait. - Long locks make a queue in
pg_locks; queries behind them do not get an error, they just stop moving. Checkpg_stat_activitybefore blaming the app.
MySQL 8.0
ALGORITHM=INPLACEavoids a copy but still takes a metadata lock at start and end.ALGORITHM=COPYrewrites the table and blocks writes for the whole operation.LOCK=NONEpermits concurrent reads and writes; unsupported operations fail fast with a clear error rather than silently copying. Always try the strict form so a surprise rewrite cannot happen:ALTER TABLE orders ADD COLUMN risk_score INT, ALGORITHM=INPLACE, LOCK=NONE;pt-online-schema-changeandgh-ostdo copy-based changes without blocking, at the cost of triggers, replication lag, and a trigger on the table.- Foreign keys are never
LOCK=NONE-able when adding to an existing table; plan a maintenance window or add the constraint after backfill.
SQLite
- Almost all
ALTER TABLEis unsupported.ALTER TABLE ... RENAME COLUMN,ADD COLUMN, andDROP COLUMNexist; everything else requires the create-new-copy-rename dance. ADD COLUMN NOT NULL DEFAULT 'x'is allowed only if the default is constant, and it is O(1).- To add a constraint or change a type you must build
new_table, copy, drop, rename.PRAGMA foreign_keysmust beON(it is off by default) for the whole process to be safe. - DDL takes a writer lock.
SQLITE_BUSY/database is lockedduring a migration means a live connection is reading; setPRAGMA journal_mode=WALand abusy_timeoutin every client.
Step 3 — expand-contract
For anything that rewrites, narrows, renames, or constrains a large table, ship three changes across at least three deploys.
deploy 1 EXPAND add new_col (nullable, no default)
deploy 2 MIGRATE backfill in batches, then dual-write from the app
deploy 3 VERIFY count mismatches; only now enforce constraints
deploy 4 CONTRACT drop the old column, add NOT NULL, tighten the typeThe application must tolerate the middle state: it reads coalesce(new_col,
old_col) and writes both. That single fact is what makes expand-contract
work — once you dual-write, you can stop caring about the backfill and just
wait for the natural turnover of rows.
Batched backfill, each batch its own transaction with a pause, so the table never stops serving traffic:
-- run in a loop, committing per batch
UPDATE t SET new_col = old_col
WHERE id IN (SELECT id FROM t WHERE new_col IS NULL LIMIT 5000);
-- then sleep briefly, e.g. 100ms, before the next batchIf the new column is NOT NULL from the start, the dual-write app keeps it
filled and the backfill is a safety net for rows written by anything else.
Step 4 — constrain last
Backfill first, then add the constraint unvalidated, then validate:
ALTER TABLE orders ADD CONSTRAINT orders_email_fk
FOREIGN KEY (customer_id) REFERENCES customers(id) NOT VALID;
ALTER TABLE orders VALIDATE CONSTRAINT orders_email_fk;NOT VALID skips the existing-row scan and takes only
SHARE ROW EXCLUSIVE, so it is near-instant. VALIDATE scans but permits
concurrent reads and writes. A validated constraint is a promise the database
will now enforce; if validation fails you get the offending rows named, fix
the data, re-validate. Same pattern in Flyway/Liquibase via
<constraints nullable="true"> and a separate validate step.
Step 5 — rehearse
A migration is not done until it has run against a copy of production at production row count, with wall-clock time recorded. Check specifically:
- How long does the lock hold? (It should be milliseconds, not seconds.)
- Does the backfill finish before the next deploy? If not, that is a separate resumable job, not a migration.
- Does it run inside a transaction that also does DML? That is how Postgres deadlocks and how bloat starts.
- Is the rollback written and tested by running it, not by reading it?
- What happens if the app version still running is the old one? It must work.
- Does the new code fail if the migration has not run yet? It should fail with a clear error, not corrupt anything.
Step 6 — the deploy itself
Prescriptive, because this is where mistakes are expensive:
- Take the migration artifact (immutable, checksummed) and the app artifact.
- Run the migration against production first (it is backward compatible with the old app by construction), then deploy the app. If the order is reversed, the expand is not additive and the old app breaks.
- Watch the lock queue and error rate for the duration. The backfill runs separately, throttled, and is resumable.
- Verify with a read-only query (
SELECT count(*) ... WHERE new_col IS NULLmust be 0) before the contract deploy. - Only then drop the old column. That drop is the point of no return.
If a migration fails halfway, do not edit the file. Diagnose, add a new migration that repairs the state, and mark the failed one in the ledger so the tool does not retry it blindly.
Gotchas
- "You have N unapplied migrations" after a fresh clone usually means a
branch merge reordered files, and the tool resolves it by timestamp or by
lexicographic name. Rename with a numeric prefix (
0007_...) or adopt the tool's revision id, nevergit mergemigration folders. - Checksum mismatches (
Alembic: Migration file ... has been modified,Flyway: checksum mismatch) mean someone edited an applied migration. Neverrepair/stampto silence it in production — find the environment that is actually behind and reconcile deliberately. - Squashing migrations for a fast test setup changes their checksums and
breaks every long-lived environment. Give tests their own build path
(
alembic upgrade headagainst a fresh database) rather than squashing. DROP COLUMNexecuted withIF EXISTSin production can silently succeed on the wrong database. Always echocurrent_database()into migration output and refuse to run outside the expected environment.- Foreign keys make every write on the child check the parent. A
last-minute
NOT VALIDFK can be a real throughput regression, not free correctness. - A unique index creation can fail on data you thought was unique. Deduplicate before the migration ships, not in the migration.
SET NULLon a column with a dependent view or aDEFAULTthat another migration assumes is a silent contract break for old app versions.- Postgres: a long transaction in the same session as DDL holds the old
snapshot, so the
ALTERwaits even though nothing conflicts. Commit before you migrate. - Adding an index does not speed up a query the planner cannot prove.
After creating it, check the plan actually uses it (see
postgres-query-tuning). CREATE INDEXinside a Flyway/Liquibase migration is transactional and therefore cannot useCONCURRENTLY. Configure the tool for non-transactional DDL rather than silently taking the blocking path.
Tool notes
Read references/tool-workflows.md for the exact CLI for Alembic, Flyway,
Prisma, golang-migrate, Django, Rails, Knex, and Liquibase, including
autogenerate pitfalls, CONCURRENTLY configuration, and how each tool handles
failed migrations and checksums.
Files
references/tool-workflows.md— per-tool commands, autogenerate gotchas, non-transactional DDL config, backfill and rollback patterns.
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.

