Create safe, reversible database migration scripts with rollback capabilities, data validation, and zero-downtime deployments. Use when changing database schemas, migrating data between systems, or performing large-scale data transformations.
Create robust, safe, and reversible data migration scripts for database schema changes and data transformations with minimal downtime.
Minimal working example:
import { Knex } from "knex";
// migrations/20240101000000_add_user_preferences.ts
export async function up(knex: Knex): Promise<void> {
// Create new table
await knex.schema.createTable("user_preferences", (table) => {
table.uuid("id").primary().defaultTo(knex.raw("gen_random_uuid()"));
table
.uuid("user_id")
.notNullable()
.references("id")
.inTable("users")
.onDelete("CASCADE");
table.jsonb("preferences").defaultTo("{}");
table.timestamp("created_at").defaultTo(knex.fn.now());
table.timestamp("updated_at").defaultTo(knex.fn.now());
table.index("user_id");
});
// Migrate existing data
await knex.raw(`
INSERT INTO user_preferences (user_id, preferences)
SELECT id, jsonb_build_object(
'theme', COALESCE(theme, 'light'),
// ... (see reference guides for full implementation)
Detailed implementations in the references/ directory:
| Guide | Contents | |---|---| | Knex.js Migrations (Node.js) | Knex.js Migrations (Node.js) | | Alembic Migrations (Python/SQLAlchemy) | Alembic Migrations (Python/SQLAlchemy) | | Large Data Migration with Batching | Large Data Migration with Batching | | Zero-Downtime Migration Pattern | Zero-Downtime Migration Pattern | | Migration Validation | Migration Validation | | Cross-Database Migration | Cross-Database Migration |
up and down migrationsCopy a source-pinned command for your client. You run it yourself.
Destination: .claude/skills/data-migration-scripts · pinned to the source commit
git clone https://github.com/aj-geddes/useful-ai-prompts.git
cd useful-ai-prompts
git checkout 3f5182cfd739fc113f4af5244a1cf342ad7f7911
mkdir -p ".claude/skills/data-migration-scripts"
cp -r "skills/data-migration-scripts" ".claude/skills/data-migration-scripts"Review the source before running. This copies files into your project; it is not a one-click install and does not verify runtime safety.
Scanner static-checks@0.1.0 · commit 3f5182cfd739. Static checks cannot prove runtime safety – review the source and the exact diff before installing. How checks work.
No static rules matched. This is not a safety guarantee.