Plan and write a safe database migration with expand and contract steps, backfills in batches, lock awareness, a rollback path and data checks before and after. Use for "add a column", "write a migration", "change the schema", "rename this table", "backfill the data".
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Change Database Schema?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/kinzarra-change-database-schema)
---
name: change-database-schema
description: Plan and write a safe database migration with expand and contract steps, backfills in batches, lock awareness, a rollback path and data checks before and after. Use for "add a column", "write a migration", "change the schema", "rename this table", "backfill the data".
---
# Change a database schema
The result is a migration that can run against a live database with real data, in steps that each leave the old and the new application version working, with a tested way back.
## 1. Before writing anything
- Find how the project manages migrations: the tool, the folder, naming and numbering, whether migrations run in a transaction, whether they run at deploy or by hand. Read the last 3 to 5 migrations and follow their style.
- Read the current definition of every table involved: columns, types, defaults, constraints, indexes, foreign keys, triggers, views and anything else that depends on them.
- Find every reader and writer of the affected tables: application code, raw queries, reports, background jobs, exports, other services. Search by table and column name as strings too.
- Learn the size: row counts and the write rate of each table, from the task, from statistics or by asking. Treat a table as large when it has over about 1 million rows or is written many times per second.
- Check which database engine and version the project uses. What locks, what rewrites the table and what can run online differ between engines; verify in the engine's own documentation, not from memory.
## 2. Shape the change as expand and contract
Never change a thing in place when old code still runs. Split into steps that can be deployed separately:
1. **Expand**: add the new column, table or index. New columns are nullable or have a default that needs no table rewrite. Nothing is removed or renamed.
2. **Write both**: the application writes the old and the new shape.
3. **Backfill**: copy existing data to the new shape.
4. **Read new**: the application reads the new shape; the old one is still written.
5. **Contract**: stop writing the old shape, then drop it, in a later release, only when the task says so.
A rename is add, copy, switch, drop. A type change is a new column. A split or merge of tables follows the same five steps. Say in the report which steps this change contains and which are left for later releases.
## 3. Locks and long tables
- Know what each statement locks and for how long. A statement that rewrites or scans a large table under an exclusive lock is replaced by its online form or by smaller steps.
- Set a short lock timeout for schema statements (seconds, not minutes) so that a blocked migration fails instead of queueing all traffic behind it. Make it safe to retry.
- Build indexes on large tables with the engine's non-blocking option when it has one; those often cannot run inside a transaction, so put them in their own migration.
- Add constraints in two phases where the engine allows it: add without validating the existing rows, then validate separately.
- Adding a required column: add it nullable, backfill, then add the constraint.
- Keep schema changes and data changes in separate migrations.
## 4. Backfills
- In batches by primary key range, 1,000 to 10,000 rows per batch, each batch its own transaction, with a pause between batches and a way to stop.
- Idempotent and resumable: a rerun skips rows that are already done. Record the progress.
- Do not load whole tables into memory, and do not call the application's current model code from an old migration; use plain statements, so that the migration still works after the models change.
- Estimate the run time from a sample. A backfill longer than a few minutes runs outside the deploy, as a separate job.
- Decide what happens to rows that do not fit the new rule (nulls, duplicates, out-of-range values) before the run, not during it.
## 5. Rollback
- Every migration has a tested reverse, or the report states plainly that it is one-way and why.
- A reverse must not lose data written after the forward step. If it would, say so and describe the recovery from backup.
- Before any destructive step, name where the data survives: a backup, a renamed table kept for a set period, an export.
- State the application rollback order: which version works with which schema state.
## 6. Data checks
- **Before**: counts of rows that the change affects, rows that break the new rule (nulls, duplicates, orphans, overlong values), and the queries used.
- **After**: the same counts, row totals unchanged where they should be, old and new shape equal for a sample and in aggregate, constraints valid, no invalid indexes.
- Run the migration forward, backward and forward again on a local or test database, with data close in shape to the real data. Run the project's test command after it.
- Never run a migration against a shared or production database from the worktree.
## 7. Report
1. **What changes**: tables, columns, indexes, constraints.
2. **Steps and deploy order**: which migration runs with which application version, and what is left for later releases.
3. **Lock and time estimate** for each step, at the stated table sizes.
4. **Backfill plan**: batch size, run time, how to pause and resume.
5. **Rollback** for each step, and what is one-way.
6. **Checks**: the queries with their results before and after on the test database.
7. **Code changed** to match, and readers or writers found outside the repository.
Rules: never drop, truncate, delete or overwrite data unless the task says so in words; if a destructive step seems needed, stop as blocked and ask. Do not commit, push or deploy unless the task says so. Never print connection strings or credentials. When blocked, state exactly what is needed: the table sizes, the engine version, the decision.