db-migration-safety
Skill9 starsChanged 5 months agoApache-2.0View on GitHub
Reviews and writes database migrations for safety — lock contention, blocking DDL on large tables, data-loss/destructive operations, missing indexes, and rollback plans. Use before running any schema change against a real or production database.
- Deletes or force-pushes
1 other project uses a version of this.
Adopted by
Most recently taken up by
- KhaledSaeed18/dotclaudetheir own version, first seen Jul 4, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- KhaledSaeed18/dotclaude★ 5same 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 repository147 other files it gives its agents.
- second-brain/AGENTS.mdAGENTS.md
- second-brain/CLAUDE.mdCLAUDE.md
- agent-tool-mcp-builderSkill
- api-design-reviewSkill
- async-concurrency-correctnessSkill
- audit-accessibility-wcagSkill
- audit-license-complianceSkill
- audit-technical-seoSkill
- auth-jwt-sessionSkill
- author-codemodSkill
- brainstorm-designSkill
- build-audit-loggingSkill
- build-cdc-streaming-pipelineSkill
- build-cli-toolSkill
- build-data-tableSkill
- build-etl-pipelineSkill
- build-form-validationSkill
- build-native-mobile-uiSkill
- build-office-docsSkill
- build-offline-first-syncSkill
- build-react-componentSkill
- build-realtime-channelSkill
- build-spreadsheetSkill
- build-vector-searchSkill
- caching-strategySkill
- cicd-pipeline-authorSkill
- cloud-cost-optimizeSkill
- code-commentsSkill
- code-reviewSkill
- compose-local-dev-stackSkill
- configure-bundler-buildSkill
- configure-dns-tlsSkill
- configure-reverse-proxy-lbSkill
- configure-security-headers-cspSkill
- contract-testingSkill
- datetime-timezone-correctnessSkill
- debug-ci-pipeline-failureSkill
- debug-flaky-testsSkill
- debug-frontend-browserSkill
- debug-root-causeSkill
- defend-llm-prompt-injectionSkill
- deliver-webhooksSkill
- dependency-upgradeSkill
- deploy-releaseSkill
- design-api-paginationSkill
- design-authorization-modelSkill
- design-backup-dr-recoverySkill
- design-event-sourcing-cqrsSkill
- design-multi-tenancySkill
- design-protobuf-grpc-serviceSkill
- design-relational-schemaSkill
- design-search-index-infraSkill
- design-state-machineSkill
- design-token-systemSkill
- diff-table-paritySkill
- distributed-locks-leasesSkill
- dockerfile-optimizeSkill
- encrypt-sensitive-dataSkill
- error-messageSkill
- estimate-workSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 27
Get the row count: before judging lock risk: SELECT reltuples::bigint FROM pg_class WHERE relname = '<table>'.
- Distinctive: line 67
If you cannot verify lock duration on real data volume, do NOT run against production — escalate for a maintenance window or a tested …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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
When to Use
Trigger this skill before writing or running ANY of these against a real/production DB:
- New migration file, or editing an existing one
ALTER TABLE,CREATE INDEX,DROP,ADD CONSTRAINT, type changes,SET NOT NULL- Backfilling / updating data across a large table (≥ ~100k rows, or unknown size)
- Renaming a column/table, or changing a column's type
- Any change reviewed by another engineer touching schema
Skip only for: brand-new empty tables, dev-only throwaway DBs, or pure read/query work with no DDL.
Steps
- Classify every statement as
additive(safe),lock-risky, ordestructive:- additive:
ADD COLUMN(nullable, no default OR constant default on PG11+),CREATE INDEX CONCURRENTLY, new table, add nullable FK without validation - lock-risky:
CREATE INDEX(non-concurrent),ADD COLUMN ... NOT NULLwith volatile default,ALTER COLUMN TYPE,ADD CONSTRAINT(validating),SET NOT NULLon existing column - destructive:
DROP TABLE/COLUMN,TRUNCATE, type narrowing (e.g.bigint→int,text→varchar(n)),DROP CONSTRAINTrelied on by app Print this classification before doing anything else.
- additive:
- Get the row count before judging lock risk:
SELECT reltuples::bigint FROM pg_class WHERE relname = '<table>';(fast estimate). Treat "unknown" as large. Lock duration scales with rows. - Kill blocking index builds — replace
CREATE INDEXwithCREATE INDEX CONCURRENTLY(Postgres) /ALGORITHM=INPLACE, LOCK=NONE(MySQL 8). CONCURRENTLY cannot run inside a transaction block, so it must be its own migration file with transaction wrapping disabled (e.g.disable_ddl_transaction!/ noBEGIN). - Batch every backfill — never one
UPDATEover the whole table (holds row locks + bloats WAL). Loop in chunks (1k–10k rows) keyed on PK, commit per batch, optional sleep between. Run the backfill as a SEPARATE step from the DDL that depends on it. - Apply expand–contract for rename / type change (3 deploys, never rename in place):
- Expand: add the new column/type. Deploy app that writes BOTH old + new.
- Migrate: backfill new column in batches; switch reads to new column.
- Contract: drop the old column in a LATER migration, after the new code is fully deployed and stable.
- Make NOT NULL safe — don't
SET NOT NULLdirectly on a big table (full scan under lock). Instead:ADD CONSTRAINT ... CHECK (col IS NOT NULL) NOT VALID, thenVALIDATE CONSTRAINT(lighter lock), then optionally promote. - Write the rollback — every migration needs a working
down/ inverse. If the op is irreversible (dropped column = lost data), say so explicitly and require an explicit confirmation + backup before running. - Dry-run on a copy — run the migration against a clone/staging snapshot of prod data (real volume), time it, and check it completes without long locks. Report measured duration.
Common Errors / Gotchas
CREATE INDEX CONCURRENTLYinside a transaction → fails or silently degrades to a normal locking build. Each CONCURRENTLY index needs its own non-transactional migration. A failed CONCURRENTLY build leaves anINVALIDindex — drop it before retrying.ADD COLUMN ... DEFAULT <volatile>rewrites the whole table under anACCESS EXCLUSIVElock. Constant defaults are metadata-only on PG11+/MySQL8 and are safe;now(),gen_random_uuid(), sequences are NOT.ALTER TABLEtakesACCESS EXCLUSIVEwhich queues behind AND blocks all reads/writes — even a fastALTERblocks the table while waiting for a slow query to finish (lock queue pile-up). Set a shortlock_timeout(e.g.SET lock_timeout = '3s') and retry, so the migration fails fast instead of freezing the table.- Adding a foreign key validates the whole referencing table under lock. Use
ADD CONSTRAINT ... NOT VALIDthenVALIDATE CONSTRAINTin a second step. - Type narrowing = silent data loss / errors (
bigint→intoverflows,varchar(n)truncates,timestamp→datedrops time). Block these unless data is proven to fit and a backup exists. - Dropping a column the running app still references → errors until the new deploy lands. Drop only AFTER the code that stopped using it is fully deployed (contract phase).
- Renaming a column in one shot breaks the currently-running app version during deploy. Always expand–contract.
- Long single-statement backfill holds locks + inflates replication lag; replicas fall behind and reads time out.
Verify
A migration is safe to run only when ALL hold:
- Every statement classified; no
destructiveop runs without explicit confirmation + a backup taken - No non-concurrent index build on a non-trivial table; CONCURRENTLY indexes are in their own non-transactional file
- All backfills are batched with per-batch commits, separate from dependent DDL
- Rename/type-change uses expand–contract across separate deploys
- A working
down/rollback exists, or irreversibility is flagged and confirmed lock_timeoutset so the migration fails fast instead of hanging the table- Dry-run on prod-sized copy completed; measured duration and lock impact reported
If you cannot verify lock duration on real data volume, do NOT run against production — escalate for a maintenance window or a tested staging run first.
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.

