skillsync

migration-safety-check

Checks a SQL database migration for locking, data loss and rollback risks before it ships, and rewrites it to run safely on a live database. Use when the user is writing or reviewing a database migration or schema change.

Ask your agent

What it does

A risk verdict per statement, the locks it takes, and a safer step-by-step version with a rollback.

SKILL.md
---
name: migration-safety-check
description: "Checks a SQL database migration for locking, data loss and rollback risks before it ships, and rewrites it to run safely on a live database. Use when the user is writing or reviewing a database migration or schema change."
---

# Migration safety check

Review or write a schema migration so it can run against a live, busy database without downtime or
data loss. Written for PostgreSQL; call out where MySQL differs if the user is on MySQL. Go through
checklist.md for every statement; examples/safe-add-column.sql shows the expand-and-contract pattern.

## Steps

1. **List each statement** and the lock it takes. Anything taking an `ACCESS EXCLUSIVE` lock on a
   large or hot table is a risk, even if it's fast, because it queues behind long transactions and
   blocks everything behind it.
2. **Check for data loss**: dropped columns or tables, type changes that truncate, `NOT NULL` added
   to columns with nulls, unique constraints over duplicate data.
3. **Check app compatibility**: the old version of the app will run against the new schema during
   the deploy. Renames and drops must wait until no running code uses the old name.
4. **Rewrite risky steps** using the safe patterns in checklist.md (add nullable then backfill,
   `CREATE INDEX CONCURRENTLY`, `NOT VALID` then `VALIDATE CONSTRAINT`, batched backfills).
5. **Set guards**: `SET lock_timeout = '5s'` and a statement timeout at the top, so a blocked
   migration fails fast instead of taking the site down.
6. **Write the rollback** for each step, and say which steps can't be rolled back.

## Output

A table of statement → lock → risk (low/medium/high) → why, then the safe migration as ordered,
separately deployable steps, then the rollback.
3 files
  • SKILL.md1.7 KB
  • checklist.md1.3 KB
  • examples/safe-add-column.sql0.8 KB