Db migration safety

From Hainrixz/claude-db · View interactive version

Db migration safety is an AI agent skill from the GitHub repository Hainrixz/claude-db. Migration-safety lint (M22) — the static contract the migration-safety auditor (MSA) and db-migration-writer enforce on a candidate migration BEFORE it runs. Checks reversibility (down migration present / expand-contract), lock level (does it take ACCESS EXCLUSIVE on a hot table), table rewrite (ful

How to use this skill

Skill file

<!-- Copyright (c) 2026 Enrique Rocha (@soyenriquerocha) · tododeia.com · id er-f37b7121 -->

db-migration-safety (M22) — the migration lint contract

M22 lints a candidate migration as a static artifact before execution. It is the contract the

migration-safety auditor and the db-migration-writer agent both honor. Feeds **Performance &

Scale** (Almacenamiento/operability category). A destructive irreversible migration is the sev-5 cap on

the performance axis.

What it checks (the lint contract)

  1. Reversibility — a usable down/rollback exists, or the change follows expand-contract

(add-new → backfill → switch → drop-old in separate deploys). A one-shot destructive step with no

down is the highest-severity finding here.

  1. Lock level — does the statement take a blocking lock on a hot table? In Postgres: adding a column

with a volatile default (pre-11), ALTER COLUMN TYPE, adding a CHECK/FK without NOT VALID,

non-CONCURRENTLY index creation all take ACCESS EXCLUSIVE / block writes. Prefer

CREATE INDEX CONCURRENTLY, ADD CONSTRAINT ... NOT VALID then VALIDATE CONSTRAINT.

  1. Table rewrite — changes that force a full-table rewrite (certain ALTER COLUMN TYPE, adding a

NOT NULL column with a volatile default on old engines) → slow + long lock on large tables.

  1. Destructive ops — DROP COLUMN, DROP TABLE, TRUNCATE, DROP NOT NULL reversals, dropping a

column still read by deployed code. Destructive + irreversible = sev 5 (caps performance).

  1. NOT NULL without default added to a populated table → fails or locks; require a backfill plan.
  2. Enum mutation — the durable concern: enum values cannot be removed or reordered (removal is

unsupported; renaming/reordering breaks dependents), so a changing set should be a lookup table,

not a native enum. ALTER TYPE ... ADD VALUE is append-only and runs in a transaction on PG12+

(only older PG forbade it mid-txn). Flag enum changes; suggest lookup tables.

  1. Schema drift — the declarative artifact (schema.prisma / structure.sql / snapshot) disagrees with

the migration history (a column exists in one but not the other) → directional warn.

Tier-0 static checks

Parse migration SQL / ORM migration files (scripts/parse-schema.mjs, parse-orm-python.py). Scan for:

DROP TABLE|DROP COLUMN|TRUNCATE; ALTER COLUMN .* TYPE; ADD COLUMN .* NOT NULL without DEFAULT;

CREATE INDEX lacking CONCURRENTLY; ADD CONSTRAINT (FK/CHECK) lacking NOT VALID;

ALTER TYPE .* ADD VALUE / enum value removal or reorder; absence of a down/rollback block; drift between the

authoritative artifact (references/detection-signals.md precedence) and the latest migration.

Tier-1 verification query

reltuples FROM pg_class WHERE relname='<t>';`

throwaway copy / shadow DB. Use verification.method: dry_run_migration when a shadow DB is available;

otherwise migration_lint on the static SQL. When live size is needed to decide severity but no DB is

reachable → needs_api, never a silent pass.

Findings

Emit per schema/finding.schema.json. Example ids:

still referenced (fail, severity 5, axis performance, fixable proposed → expand-contract plan).

axis performance, fixable auto → add CONCURRENTLY).

(fail, severity 4, axis performance, fixable proposed).

suggest a lookup table (warn, severity 3, axis performance).

confidence directional).

Each finding: evidence.observed quotes the offending migration statement verbatim (secrets redacted);

verification.reproduce is a runnable command above using $DATABASE_URL; verification.method is

migration_lint or dry_run_migration; fix_preview carries the safer rewrite (e.g. CONCURRENTLY,

NOT VALID + VALIDATE, an expand-contract sequence); expected_impact carries

{axis, confidence, magnitude, rationale} — banded, never a naked %.

Honesty

unknown-size table is directional (medium), not an established sev-5. Don't claim a downtime

duration in seconds; magnitude banded only.

Reversible or expand-contract changes are warns. Drift is directional and never caps.

behind explicit user accept.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.