safe-migrations
/safe-migrations
Change a schema that holds data without downtime or loss, with expand and contract steps, short locks, batched backfills, a rollback plan and a tested backup. Use it for any migration that renames, removes or retypes a column, adds a constraint, indexes a large table or runs on production.
- Author
- jelvodev ↗
- Repository
- yaap ↗
- Licence
- MIT
- Last repository change
- 13.11.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 13.11.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Change a schema that holds data without downtime or loss, with expand and contract steps, short locks, batched backfills, a rollback plan and a tested backup. Use it for any migration that renames, removes or retypes a column, adds a constraint, indexes a large table or runs on production.
Original author description
Change a schema that holds data without downtime or loss, with expand and contract steps, short locks, batched backfills, a rollback plan and a tested backup. Use it for any migration that renames, removes or retypes a column, adds a constraint, indexes a large table or runs on production.
How it works
- 1. Before you write it
- 2. Expand and contract
- 3. Keep locks short
- 4. Patterns for PostgreSQL
- 5. Backfill in batches
- 6. Plan the way back
- 7. Test it
- Report
SKILL.md preview
---
name: safe-migrations
description: Change a schema that holds data without downtime or loss, with expand and contract steps, short locks,
batched backfills, a rollback plan and a tested backup. Use it for any migration that renames, removes or retypes a
column, adds a constraint, indexes a large table or runs on production.
license: MIT
metadata:
sources: agency-agents/engineering/engineering-database-reliability-engineer.md, wshobson/plugins/database-migrations/commands/sql-migrations.md
---
# Safe migrations
A migration on a table with data is a change to a running system. Old and new code run side by side during a
deploy, a lock can block every request, and some changes cannot be undone. Plan each migration so any single step
can stop or roll back without losing data.
## 1. Before you write it
- **Know the size.** How many rows, how busy is the table, which queries use it? A change that is instant on a test
database can take minutes on production.
- **Know the backup.** Is there a recent backup, and has a restore from it been tested? A backup that was never
restored is a hope, not a plan. For a risky change, take a fresh one and say where it is.
- **Know the way back on the code side.** YAAP checkpoints each change by itself. Undo a failed attempt with git
(`git restore <file>` through `run_command`), or ask the user to use *Restore to before this*.
## 2. Expand and contract
Never change a column in place in one step while code depends on it. Split the change across releases:
1. **Expand.** Add the new column, table or index next to the old one. Nothing reads it yet.
2. **Write both.** Deploy code that writes old and new.
3. **Backfill.** Copy existing data to the new form, in batches.
4. **Read new.** Deploy code that reads the new form; keep writing both for one release.
5. **Contract.** Once no running code uses the old form, stop writing it and drop it, in a later release.
A rename is an expand and contract: add `email_address`, write both, backfill, read the new one, drop `email`. Each
step is deployable and reversible on its own.
## 3. Keep locks short
Most schema changes take a lock that blocks reads or writes while it waits. Set a budget, so a migration that cannot
get its lock fails fast instead of queueing every request behind it:
```sql
SET lock_timeout = '5s';
SET statement_timeout = '15min';
```
Retry a migration that hit the lock timeout at a quieter moment; do not raise the timeout until it passes.
## 4. Patterns for PostgreSQL
**Add a column.** Nullable, or with a constant default (PostgreSQL 11 and later), is instant. A volatile default
(`random()`, `clock_timestamp()`) rewrites the table.
**Make a column NOT NULL** without a long scan under lock:
```sql
ALTER TABLE orders ADD CONSTRAINT orders_currency_not_null CHECK (currency IS NOT NULL) NOT VALID;
ALTER TABLE orders VALIDATE CONSTRAINT orders_currency_not_null;
ALTER TABLE orders ALTER COLUMN currency SET NOT NULL;
ALTER TABLE orders DROP CONSTRAINT orders_currency_not_null;
```
`VALIDATE` scans without blocking writes, and from PostgreSQL 12 `SET NOT NULL` uses the validated check instead of
scanning again.
**Add a foreign key** the same way: `ADD CONSTRAINT … NOT VALID`, then `VALIDATE CONSTRAINT` in a separate step.
**Build an index** with `CREATE INDEX CONCURRENTLY`. It cannot run inside a transaction, so the migration tool must
run that step outside one (in Alembic, inside `with op.get_context().autocommit_block():`). If it fails, it leaves an
invalid index; drop it and try again.
**Change a column's type** with expand and contract (a new column, a backfill, a switch), unless the change is known
to need no rewrite, such as making a `varchar` longer.
## 5. Backfill in batches
One `UPDATE` over millions of rows holds locks for its whole run and bloats the table. Update a bounded batch, commit,
repeat until no rows are left:
```sql
UPDATE orders
SET currency = 'EUR'
WHERE id IN (
SELECT id FROM orders
WHERE currency IS NULL
ORDER BY id
LIMIT 5000
);
```
Run it from a script or job that loops, logs progress, and can stop and resume. Keep the backfill out of the schema
migration, so a deploy does not wait for it.
## 6. Plan the way back
Write the rollback with the migration: a down migration, or the forward fix you would ship instead. Say which steps
cannot be undone (dropping a column, deleting rows, a lossy type change) and do those last, only after the release
that stopped using the data has run without trouble. Before a destructive step, check that the data is no longer read
and that the backup has it.
## 7. Test it
- Run the migration up, down and up again on a fresh database in the tests; check that the migrated schema matches the
models.
- Run it once on a copy with realistic data volume and time it, when the table is large.
- With Alembic, review every autogenerated migration by hand; it misses renames and some type changes, and can drop
what it does not recognise. Apply it with `uv run alembic upgrade head`.
## Report
```
Change: <what changes in the schema and why>
Steps: <expand, backfill, switch, contract, with the release each belongs to>
Locks: <what each step locks and for how long, with the timeouts>
Rollback: <per step; and which steps are irreversible>
Backup: <where, and when a restore was last tested>
Tested: <commands and results; data volume used>
```
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · jelvodev/yaap | 100 | MIT |
| vibe-md/vibe.md | 100 | MIT |
| nuttakulsv/safe-migrations | 100 | MIT |
| thiientv/godmode | 90 | MIT |
Database snapshot: 13.11.2026 · package version 44b8f385e81a (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Similar skills
- 90db-migration
Writes safe, zero-downtime schema and data migrations (Flyway, Mongo backfills, online index builds) using expand/contract, batched idempotent backfills and rollback plans. Use to add/rename/retype a field or column, backfill data, or \"migrate without downtime\". - 100database-migrations
Safe, reversible database migration patterns: forward-only production changes, expand-contract zero-downtime renames, concurrent indexes, batched backfills, and per-tool workflows for PostgreSQL, Prisma, and Drizzle. Use when writing a schema or data migration, adding a column… - 100database-migrations
Plans and writes zero-downtime database schema migrations (expand/contract, safe indexes, batched backfills, rollback). Use when adding, renaming or dropping columns or tables, changing types or constraints, adding indexes, or backfilling data on a live database. - 90database-migrations
Change schemas safely with expand-contract, online DDL, batched backfills, and rollback awareness. Use when altering a production schema or when a migration risks locking or downtime. - 85zero-downtime-schema
Change a production database schema without downtime or data loss: expand and contract, backward-compatible steps across deployments, batched backfills, online index operations, renames through new columns, dual writes when needed, and a rollback plan per step; independent of… - 100database-migrations
Safe, reversible database migration patterns: forward-only production changes, expand-contract zero-downtime renames, concurrent indexes, batched backfills, and per-tool workflows for PostgreSQL, Prisma, Drizzle, Kysely, Django, and golang-migrate. Use when writing a schema or…