Backend · Skill

Run safe database migrations

run-safe-database-migrations

Runs a production database migration without dropping rows: back up, expand, backfill, then contract in a later release, and keep a down path. Use when the user is writing a database migration, renaming a field in production, or needs a rollback before applying it.

Works with: Any AI Not yet model-tested; testing Jan 2027

Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works

Install in Claude Code, Cursor or Codex (optional)

Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.

Claude Code (all your projects)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/run-safe-database-migrations/raw' -o ~/'.claude/skills/run-safe-database-migrations/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/run-safe-database-migrations/files/LICENSE.txt' -o ~/'.claude/skills/run-safe-database-migrations/LICENSE.txt'

Cursor (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/run-safe-database-migrations/raw' -o '.cursor/skills/run-safe-database-migrations/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/run-safe-database-migrations/files/LICENSE.txt' -o '.cursor/skills/run-safe-database-migrations/LICENSE.txt'

Codex and other Agent Skills tools (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/run-safe-database-migrations/raw' -o '.agents/skills/run-safe-database-migrations/SKILL.md' && curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/run-safe-database-migrations/files/LICENSE.txt' -o '.agents/skills/run-safe-database-migrations/LICENSE.txt'

Or connect the whole library over MCP

Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):

{
  "mcpServers": {
    "theskillvault": {
      "url": "https://theskillvault.ai/mcp"
    }
  }
}

A migration changes stored data while the app is still serving traffic. Old code and new code both run during the release. They must both accept the shape you leave behind.

When to use

  • You are about to change a production database.
  • A rename or a split of one field has to land without downtime.
  • You want a backup and a down path before the change is applied.

When not to use

  • You are choosing Postgres types, keys and indexes. Use design-postgres-tables.
  • You want a review of injection and grants. Use review-sql-safety-prompt.
  • You are laying out the release stages around the change. Use design-deployment-pipeline.

Steps

  1. Take a backup or a snapshot first. A down script does not bring lost rows back.
  2. Expand. Add the new field or table. Leave the old one in place. Do not drop or rename in this release.
  3. Ship code that writes both shapes. Backfill old rows in small batches. Compare counts before and after.
  4. Ship code that reads the new shape only. Keep writing the old shape until that code is gone.
  5. Contract in a later release. Drop the old field only after no running version still reads it.
  6. Give each migration a down path, or write why there is no safe one. A down that drops a table is not a data rollback.
  7. Rehearse on a copy of production data. Use the same database engine as production.
  8. Apply with the tool you already have. Prisma uses migrate deploy. TypeORM uses migration:run. Sequelize uses db:migrate. A numbered SQL file is the same idea.
  9. If a step fails, stop. Restore the backup or run the down path. Do not patch production by hand.
-- expand only: keep the old column until a later release
alter table users add column full_name text;
-- backfill in batches, switch reads, then drop the old column later

Gotchas

  • A required field with no default can lock or rewrite a large table. Add it as optional, backfill, then tighten the rule.
  • One giant update can lock writes. Batch it, and commit each batch.
  • Do not put a password, a connection string, or a dump of customer rows in the migration file.
  • During a rolling release both app versions run at once. Both must tolerate the expanded shape.
  • Change a type by adding a new field and copying values. Do not cast in place on a busy table.

Portability

Works with any migration tool and any SQL database: Prisma, TypeORM, Sequelize, Rails, Django, Flyway or plain SQL. If you use a document store, apply the same expand, backfill and contract order to fields. The release train around this change is a separate skill.

Done when

  • A backup exists, and the migration has been run on a copy.
  • Expand, backfill and contract are separate releases.
  • The down path is written, or the reason there is none is written.
  • No secret and no customer dump is in the migration file.

Example prompt

"Write a safe migration that renames users.name to users.full_name on a production database. Expand, backfill, and contract later. Include the down path."

---

Adapted from "database-migration" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.

Bundled files

Licence

Adapted from Seth Hobson (wshobson/agents, database-migration) (Copyright (c) 2024 Seth Hobson), MIT License. Adapted by TheSkillVault: rewritten as expand, backfill and contract with a backup and a down path; dropped per-ORM class samples, the in-place type cast and the drop-table restore; added batching, gotchas and done-when.

Licence: MIT License. Attribution when you reuse it: Based on work by Seth Hobson (wshobson/agents, database-migration) (Copyright (c) 2024 Seth Hobson, MIT License), https://github.com/wshobson/agents/tree/7e2fb559cf4b824b27f1fec49fe88f7511066d26/plugins/framework-migration/skills/database-migration; adapted by TheSkillVault, https://theskillvault.ai