{"name":"run-safe-database-migrations","description":"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.","category":"backend","tags":["database","rollback"],"models":["any"],"proven":false,"passing_families":0,"model_testing":"Not yet model-tested; testing Jan 2027","url":"https://theskillvault.ai/skills/run-safe-database-migrations","api_url":"https://theskillvault.ai/api/skills/run-safe-database-migrations","raw_url":"https://theskillvault.ai/api/skills/run-safe-database-migrations/raw","body":"# Run safe database migrations\n\nA 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.\n\n## When to use\n- You are about to change a production database.\n- A rename or a split of one field has to land without downtime.\n- You want a backup and a down path before the change is applied.\n\n## When not to use\n- You are choosing Postgres types, keys and indexes. Use design-postgres-tables.\n- You want a review of injection and grants. Use review-sql-safety-prompt.\n- You are laying out the release stages around the change. Use design-deployment-pipeline.\n\n## Steps\n1. Take a backup or a snapshot first. A down script does not bring lost rows back.\n2. Expand. Add the new field or table. Leave the old one in place. Do not drop or rename in this release.\n3. Ship code that writes both shapes. Backfill old rows in small batches. Compare counts before and after.\n4. Ship code that reads the new shape only. Keep writing the old shape until that code is gone.\n5. Contract in a later release. Drop the old field only after no running version still reads it.\n6. 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.\n7. Rehearse on a copy of production data. Use the same database engine as production.\n8. 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.\n9. If a step fails, stop. Restore the backup or run the down path. Do not patch production by hand.\n\n```sql\n-- expand only: keep the old column until a later release\nalter table users add column full_name text;\n-- backfill in batches, switch reads, then drop the old column later\n```\n\n## Gotchas\n- A required field with no default can lock or rewrite a large table. Add it as optional, backfill, then tighten the rule.\n- One giant update can lock writes. Batch it, and commit each batch.\n- Do not put a password, a connection string, or a dump of customer rows in the migration file.\n- During a rolling release both app versions run at once. Both must tolerate the expanded shape.\n- Change a type by adding a new field and copying values. Do not cast in place on a busy table.\n\n## Portability\nWorks 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.\n\n## Done when\n- A backup exists, and the migration has been run on a copy.\n- Expand, backfill and contract are separate releases.\n- The down path is written, or the reason there is none is written.\n- No secret and no customer dump is in the migration file.\n\n## Example prompt\n\"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.\"\n\n---\nAdapted from \"database-migration\" in wshobson/agents (MIT, Copyright (c) 2024 Seth Hobson). See LICENSE.txt.","files":["LICENSE.txt"],"license":"MIT","license_name":"MIT License","license_url":"https://opensource.org/license/mit","attribution":"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","install":{"claude_code":"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":"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'","agents":"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'","mcp_url":"https://theskillvault.ai/mcp","cursor_mcp_deeplink":"cursor://anysphere.cursor-deeplink/mcp/install?name=theskillvault&config=eyJ1cmwiOiJodHRwczovL3RoZXNraWxsdmF1bHQuYWkvbWNwIn0%3D","mcp_config":{"mcpServers":{"theskillvault":{"url":"https://theskillvault.ai/mcp"}}}},"bundles":[],"tested_on":[{"family":"chatgpt","name":"ChatGPT","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"claude","name":"Claude","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"gemini","name":"Gemini","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"grok","name":"Grok","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"llama","name":"Llama","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"deepseek","name":"DeepSeek","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"mistral","name":"Mistral","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"qwen","name":"Qwen","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"kimi","name":"Kimi K","outcome":null,"date":null,"source":null,"counts":false,"stale":false}],"proven_rule":{"threshold":5,"window_days":365,"computed_at":"2026-11-29T03:42:30.374Z"}}