migration-plan
Plan a safe online database migration in expand-migrate-contract phases; outputs the ordered steps and the rollback for each.
- License 许可
- MIT
- 最近更新
- 2026-10-29
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- migration-plan
- description
- Plan a safe online database migration in expand-migrate-contract phases; outputs the ordered steps and the rollback for each.
03 SKILL.md 正文
migration-plan
Lay out an online schema/data migration without downtime. Every step has a rollback.
Mode: analytical — judgment inside a fixed frame. Keep the output shape; every verdict cites its evidence; unknowns are marked [TBD] or [VERIFY], never filled in.
When to use
- A schema change involves rewriting existing rows, changing types, or renaming columns.
- A migration could lock a hot table for too long.
- The user says "is this migration safe?".
Inputs
- The current and target schema (or the change in words) — required.
- Table sizes, write rates, and the database engine + version (required for lock and duration judgments; unknown → treat the table as large and hot, and say so).
- The deploy model (optional): can app versions overlap during the migration? How long between deploys?
Related skills
polymath-backend:review-migration— Postgres-specific statement-level review: per-statement lock taxonomy (ACCESS EXCLUSIVEvsSHARE UPDATE EXCLUSIVE), the safeNOT NULLmulti-step pattern, big-table hazards. Use this skill to plan the expand-migrate-contract phases; usereview-migrationto audit each SQL statement before it ships on Postgres.polymath-data:sql-write— for backfill query authoring inside the migrate phase.
Procedure
Plan in three phases. The app keeps running on the old shape during expand, on both shapes during migrate, and on the new shape during contract.
-
Expand (additive only):
- Add the new column / table / index with no app code reading it.
- Defaults must be
NULLor a non-blocking server-side default — never a backfill in the migration itself. - Indexes built with
CREATE INDEX CONCURRENTLY(Postgres) or the equivalent. - Rollback: drop the new object.
-
Migrate (dual-write + backfill):
- App writes to both old and new shape on every mutation.
- Backfill historic rows in batches (a few thousand rows per transaction). Run during low-traffic windows. Walk a primary-key cursor (
WHERE id > :last AND id <= :last + :batch); never a single hugeUPDATE(and note PostgresUPDATEhas noLIMITclause). - App reads the new shape if it's populated, falling back to the old.
- Rollback: stop dual-write; drop the new column or table.
- Gate before contract: prove the backfill is complete (
SELECT count(*) … WHERE new_col IS NULL= 0, or a checksum/row-count comparison between shapes) and that reads of the new shape have run in production for at least one release without errors.
-
Contract (remove old):
- App writes only the new shape.
- Remove old code paths.
- Drop old columns / tables / indexes.
- Rollback: contract is the one-way door. Re-adding a dropped column restores the schema, not the data. Before dropping, archive the old column/table (copy or snapshot) and wait at least one release after the last read stopped; the rollback is "restore from that archive", and the plan must say where it lives.
Specific hazards to plan around
- Locks: any
ALTER TABLEthat rewrites the table is a hazard on Postgres / MySQL. Long locks cascade into request timeouts. Confirm the operation is metadata-only or fast. - Long index builds: build concurrently; monitor; ready a kill plan.
- Type changes: never change a column type in place on a hot table. Add a new column, dual-write, swap.
- Renames: a rename is a type change in disguise. Use add-new + dual-write + drop-old.
- Nullable → NOT NULL: only after the backfill is verified to have populated every row. Add the constraint with
NOT VALIDfirst, thenVALIDATE CONSTRAINTto avoid a full table scan under a lock. - Foreign keys: add with
NOT VALIDthenVALIDATEso the validation scan runs without a lock.
Output
Migration plan: refund_idempotency_keys
Goal: add `idempotency_key varchar(64) UNIQUE` to `refunds`.
Phase 1 — Expand
1. ALTER TABLE refunds ADD COLUMN idempotency_key varchar(64) NULL;
2. CREATE UNIQUE INDEX CONCURRENTLY refunds_idem_key_uq
ON refunds (idempotency_key) WHERE idempotency_key IS NOT NULL;
Rollback: DROP INDEX … ; ALTER TABLE refunds DROP COLUMN idempotency_key;
Phase 2 — Migrate
3. Deploy app version that writes idempotency_key on every refund create.
4. (No backfill needed — historic refunds have no idempotency key by
design; the partial unique index covers this.)
Rollback: revert app deploy.
Phase 3 — Contract
5. No-op — column stays NULL for historic rows; new rows always populated.
Monitoring during phases:
- Lock wait time on refunds.
- Refund p99 latency.
- Idempotency-Key collision rate (should be > 0 once retries are observed).
Anti-patterns to avoid
- Mixing expand + migrate + contract in one deploy.
- Backfilling in a single
UPDATEon a large hot table. - Adding
NOT NULLto an existing column in one step on a populated table. - Dropping a column the same release it stops being read.
04 同仓库更多 Skills
runbook
Write or refresh an operational runbook for an alert/symptom; structured as TL;DR + Pre-checks + Steps + Verification + Rollback + Escalation.
rfc
Draft a lightweight design RFC to docs/rfcs/<slug>.md; Summary, Motivation, Detailed design, Drawbacks, Alternatives, Unresolved questions. A proposal to debate, not a recorded decision (adr).
editorial-pass
Review and tighten prose in a Markdown artifact for a target tone (terse, formal, friendly) without dropping information; preserve quotations and load-bearing constraints.
architecture-review-facilitation
Run an architecture review as a process — entry criteria, reviewer checklist, decision log, and follow-up actions — pairing the design artifact with a red-team pass.
architecture-doc
Write a C4-style architecture document; System Context + Containers (+ Components when interesting) with mermaid diagrams. Describes structure, not a decision (adr) or proposal (rfc).
adr
Write an Architecture Decision Record (Michael Nygard format) to docs/adrs/NNNN-<slug>.md; frontmatter validated by the ADR artifact schema. Records a made decision, not a proposal to debate (rfc).