Data Migration
Purpose
Move or transform existing data safely: backfills that accompany schema change, document reshaping, store-to-store moves — designed for interruption, re-run, verification, and rollback, because data migrations fail midway in production, on real data, at volume.
When to Use
- Backfilling new columns/shapes (between
database-migrations' expand and contract steps).
- Reshaping documents (
document-schema-design changes), merging/splitting entities, moving stores.
- Not for schema DDL itself (
database-migrations) or environment seed content (seed-data).
Inputs
- Source and target shapes + the transformation rules (including edge/legacy values).
- Data volume, write traffic on affected tables/collections, downtime tolerance.
../../migration-planning for the surrounding project when the migration is the project.
Discovery Questions
- Volume and rate: how many rows/documents, how fast can we process without hurting production (
database-performance headroom)?
- Is the data live (writes during migration) — dual-write/dual-read needed, or is a quiet window real?
- What are the dirty-data cases (nulls, legacy encodings, orphans) and their rulings?
- How is success proven (counts, checksums, sampled field comparison) — and what's the rollback if proof fails?
Responsibilities
- Write the transformation spec: field mappings, derivations, and an explicit ruling for every dirty-data case found by profiling the real data first — surprises belong in profiling, not mid-run.
- Design the runner: batched (bounded transactions, no table-length locks), rate-limited, resumable (checkpointed progress — crash at row 3M restarts at 3M), idempotent (re-processing a batch converges; upsert/merge semantics).
- Handle live traffic: expand schema first (
database-migrations), then either dual-write (app writes old+new during backfill) or ordered backfill-then-switch; reads cut over only after verification; contract last.
- Verify: row/document counts per cohort, checksums or field-level sampling, application-level invariant checks (
transactions-era invariants still hold); verification is a step with pass/fail criteria, not a vibe.
- Plan rollback: backup/snapshot point (
backup-recovery) taken before mutation; reversible switch (reads back to old path) until contract; after contract, roll-forward-only — stated in advance.
- Log progress + errors per batch (
../../backend/backend-observability); quarantine-and-continue vs halt-on-error ruled per error class.
- Rehearse on a production-scale copy: timing, locks, error rates — a migration first run in production is a gamble, not a plan.
Required Workflow
- Profile source data; write the transformation spec with dirty-data rulings.
- Build the batched/resumable/idempotent runner.
- Sequence with schema steps and live-traffic strategy (dual-write windows).
- Rehearse on production-scale data; measure duration and impact.
- Snapshot, run with monitoring, verify against criteria.
- Cut reads over; hold the old path until stability; contract per
database-migrations.
Decision Rules
- Batch size balances lock time vs total duration — measured in rehearsal, not guessed.
- Idempotency is non-negotiable: every batch must be safely re-runnable.
- Verification criteria are defined before the run; "it looks right" is not criteria.
- Dirty data gets explicit rulings (transform/quarantine/reject) — silent coercion corrupts.
- If the old path can't be kept readable during cutover, downtime is being chosen — record it.
Rules
- Snapshot before any mutating run.
- Never run unrehearsed against production.
- Progress observable while running; halt criteria pre-agreed.
Anti-Patterns
UPDATE everything in one transaction on a hot table.
- Non-resumable scripts that must restart from zero after a crash.
- Migrating unprofiled data and discovering legacy encodings at row 2 million.
- Declaring success from a completed run with no count/checksum verification.
- Dropping the old column/collection the same day reads switched.
Validation Checklist
Definition of Done
A rehearsed, batched, resumable, idempotent migration with a written transformation spec, pre-agreed verification criteria that passed, a snapshot-backed rollback path per phase, and a clean cutover — recorded end to end.
Related Skills
database-migrations, backup-recovery, ../../migration-planning, transactions, concurrency, database-performance, ../../backend/backend-observability, seed-data.
Related Knowledge
../../../knowledge/ (data volumes, traffic patterns, dirty-data rulings).
Related References
../../../references/database/migrations/ (runner patterns, when populated).
Context Loading Guidance
- Requires: source/target shapes, volume + traffic profile, downtime tolerance.
- Does not require: unrelated schema areas, application feature plans.
- May load:
database-migrations (sequencing), backup-recovery (snapshot).
- Stop when: spec, runner design, rehearsal results, and verification are recorded.
Token Efficiency Guidance
The transformation spec table (source → rule → target, dirty-case rulings) plus the phase sequence is the artifact; profile summaries beat row dumps.
1---2name: data-migration3description: Use to plan data movement/transformation — backfills for schema change, reshaping documents, moving between stores — batched and resumable, idempotent, verified by counts/checksums, with dual-write/dual-read cutover for live systems and a real rollback path.4---56# Data Migration78## Purpose910Move or transform existing data safely: backfills that accompany schema change, document reshaping, store-to-store moves — designed for interruption, re-run, verification, and rollback, because data migrations fail midway in production, on real data, at volume.1112## When to Use1314- Backfilling new columns/shapes (between `database-migrations`' expand and contract steps).15- Reshaping documents (`document-schema-design` changes), merging/splitting entities, moving stores.16- **Not** for schema DDL itself (`database-migrations`) or environment seed content (`seed-data`).1718## Inputs1920- Source and target shapes + the transformation rules (including edge/legacy values).21- Data volume, write traffic on affected tables/collections, downtime tolerance.22- `../../migration-planning` for the surrounding project when the migration is the project.2324## Discovery Questions2526- Volume and rate: how many rows/documents, how fast can we process without hurting production (`database-performance` headroom)?27- Is the data live (writes during migration) — dual-write/dual-read needed, or is a quiet window real?28- What are the dirty-data cases (nulls, legacy encodings, orphans) and their rulings?29- How is success *proven* (counts, checksums, sampled field comparison) — and what's the rollback if proof fails?3031## Responsibilities3233- Write the **transformation spec**: field mappings, derivations, and an explicit ruling for every dirty-data case found by *profiling the real data first* — surprises belong in profiling, not mid-run.34- Design the runner: **batched** (bounded transactions, no table-length locks), **rate-limited**, **resumable** (checkpointed progress — crash at row 3M restarts at 3M), **idempotent** (re-processing a batch converges; upsert/merge semantics).35- Handle **live traffic**: expand schema first (`database-migrations`), then either dual-write (app writes old+new during backfill) or ordered backfill-then-switch; reads cut over only after verification; contract last.36- **Verify**: row/document counts per cohort, checksums or field-level sampling, application-level invariant checks (`transactions`-era invariants still hold); verification is a step with pass/fail criteria, not a vibe.37- Plan **rollback**: backup/snapshot point (`backup-recovery`) taken before mutation; reversible switch (reads back to old path) until contract; after contract, roll-forward-only — stated in advance.38- Log progress + errors per batch (`../../backend/backend-observability`); quarantine-and-continue vs halt-on-error ruled per error class.39- Rehearse on a production-scale copy: timing, locks, error rates — a migration first run in production is a gamble, not a plan.4041## Required Workflow42431. Profile source data; write the transformation spec with dirty-data rulings.442. Build the batched/resumable/idempotent runner.453. Sequence with schema steps and live-traffic strategy (dual-write windows).464. Rehearse on production-scale data; measure duration and impact.475. Snapshot, run with monitoring, verify against criteria.486. Cut reads over; hold the old path until stability; contract per `database-migrations`.4950## Decision Rules5152- Batch size balances lock time vs total duration — measured in rehearsal, not guessed.53- Idempotency is non-negotiable: every batch must be safely re-runnable.54- Verification criteria are defined *before* the run; "it looks right" is not criteria.55- Dirty data gets explicit rulings (transform/quarantine/reject) — silent coercion corrupts.56- If the old path can't be kept readable during cutover, downtime is being chosen — record it.5758## Rules5960- Snapshot before any mutating run.61- Never run unrehearsed against production.62- Progress observable while running; halt criteria pre-agreed.6364## Anti-Patterns6566- `UPDATE everything` in one transaction on a hot table.67- Non-resumable scripts that must restart from zero after a crash.68- Migrating unprofiled data and discovering legacy encodings at row 2 million.69- Declaring success from a completed run with no count/checksum verification.70- Dropping the old column/collection the same day reads switched.7172## Validation Checklist7374- [ ] Source profiled; transformation spec with dirty-data rulings.75- [ ] Runner batched, rate-limited, checkpointed, idempotent.76- [ ] Live-traffic strategy sequenced with schema expand/contract.77- [ ] Rehearsed at production scale; duration/impact measured.78- [ ] Snapshot taken; verification criteria defined and passed.79- [ ] Rollback path stated per phase; old path held until stable.8081## Definition of Done8283A rehearsed, batched, resumable, idempotent migration with a written transformation spec, pre-agreed verification criteria that passed, a snapshot-backed rollback path per phase, and a clean cutover — recorded end to end.8485## Related Skills8687`database-migrations`, `backup-recovery`, `../../migration-planning`, `transactions`, `concurrency`, `database-performance`, `../../backend/backend-observability`, `seed-data`.8889## Related Knowledge9091`../../../knowledge/` (data volumes, traffic patterns, dirty-data rulings).9293## Related References9495`../../../references/database/migrations/` (runner patterns, when populated).9697## Context Loading Guidance9899- **Requires:** source/target shapes, volume + traffic profile, downtime tolerance.100- **Does not require:** unrelated schema areas, application feature plans.101- **May load:** `database-migrations` (sequencing), `backup-recovery` (snapshot).102- **Stop when:** spec, runner design, rehearsal results, and verification are recorded.103104## Token Efficiency Guidance105106The transformation spec table (source → rule → target, dirty-case rulings) plus the phase sequence is the artifact; profile summaries beat row dumps.