agentleFS
antonrischantonrisch/db-skills/skills/write-safe-migrations/SKILL.md

write-safe-migrations

Skill9 starsChanged 10 months agoMITView on GitHub

Plans and executes safe database migrations with low-downtime patterns, verification, and rollback. Use when changing schema, backfilling data, adding constraints, or creating indexes in production.

The file

Show its rules and text

We split this skill into the rules it gives an agent

13 rules. About 0 are common, 12 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈12

    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
  1. Distinctive:

    Default approach: expand → migrate → contract.

    line 10
  2. Distinctive:

    when rollback must be possible without restoring from backup

    line 18
  3. Distinctive:

    add new nullable column

    line 32
  4. Distinctive:

    add constraint / set not null (engine-specific)

    line 35
  5. Distinctive:

    remove old column in a later deploy

    line 36
  6. Distinctive:

    add constraint in a way that avoids long blocking where possible

    line 46
  7. Distinctive:

    write the migration plan and rollback plan

    line 52
  8. Distinctive:

    run on staging with production-like data volume if possible

    line 56
  9. Distinctive:

    run correctness queries and app-level smoke checks

    line 60
  10. Distinctive:

    remove old paths only after adoption is complete

    line 64
  11. Very specific:

    prefer additive changes first (new columns/tables/indexes) to keep rollback easy

    line 150
  12. Distinctive:

    avoid destructive changes in the same deploy (drop/rename) unless you can restore quickly

    line 151
  13. Distinctive:

    keep old read path working until you prove new path is correct

    line 152

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Write Safe Migrations

Quick Start

Default approach: expand → migrate → contract.

Goal: reduce lock time, keep rollback easy, and prove correctness with checks.

When to use this skill

  • any production schema change (especially large tables)
  • adding columns/indexes/constraints, renames, backfills, or data moves
  • when rollback must be possible without restoring from backup

Before you start (inputs)

  • database engine + version
  • migration goal (what changes, why)
  • table size + write rate + peak hours
  • lock tolerance (seconds) and downtime tolerance
  • app deploy constraints (can you do multiple deploys?)

Common safe patterns

add column (nullable) + backfill + enforce

  1. add new nullable column
  2. deploy app writing both old + new (or only new with fallback)
  3. backfill in batches
  4. add constraint / set not null (engine-specific)
  5. remove old column in a later deploy

add index safely

  • postgres: create index concurrently
  • mysql: use online ddl when supported; validate it is online for your engine/version

add foreign key / check constraint safely

  • validate data first (no orphans, no invalid values)
  • add constraint in a way that avoids long blocking where possible
  • validate separately if engine supports it

Workflow (default)

  1. design
    • write the migration plan and rollback plan
  2. prechecks
    • confirm table sizes, existing indexes, constraint violations
  3. dry run
    • run on staging with production-like data volume if possible
  4. execute
    • apply schema change, then data movement in batches
  5. verify
    • run correctness queries and app-level smoke checks
  6. monitor
    • lock waits, replication lag, error rates
  7. contract
    • remove old paths only after adoption is complete

PostgreSQL templates

add column:

ALTER TABLE public.your_table
ADD COLUMN new_col text;

backfill in batches (example using id ranges):

UPDATE public.your_table
SET new_col = <expression>
WHERE id >= :min_id AND id < :max_id
  AND new_col IS NULL;

add index safely:

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_your_table_new_col
ON public.your_table (new_col);

set not null (do this only after backfill + verification):

ALTER TABLE public.your_table
ALTER COLUMN new_col SET NOT NULL;

MySQL templates

add column:

ALTER TABLE your_table
ADD COLUMN new_col VARCHAR(255) NULL;

backfill in batches (example):

UPDATE your_table
SET new_col = <expression>
WHERE id BETWEEN ? AND ?
  AND new_col IS NULL;

add index:

CREATE INDEX idx_your_table_new_col ON your_table (new_col);

Verification queries (examples)

null rate after backfill:

SELECT COUNT(*) AS null_count
FROM your_table
WHERE new_col IS NULL;

row count consistency (when moving data):

SELECT COUNT(*) FROM old_table;
SELECT COUNT(*) FROM new_table;

fk integrity (orphan check):

SELECT COUNT(*) AS orphan_count
FROM child c
LEFT JOIN parent p ON p.id = c.parent_id
WHERE c.parent_id IS NOT NULL AND p.id IS NULL;

Rollback rules of thumb

  • prefer additive changes first (new columns/tables/indexes) to keep rollback easy
  • avoid destructive changes in the same deploy (drop/rename) unless you can restore quickly
  • keep old read path working until you prove new path is correct

Output format (runbook)

## migration: [name]

### objective

- what changes:
- why:

### risks

- locks:
- long running work:
- replication lag:

### plan

- [ ] step 1:
- [ ] step 2:
- [ ] step 3:

### verification

- query checks:
- app checks:

### rollback

- exact rollback steps:

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.