Create robust, safe, and reversible data migration scripts for database schema changes and data transformations with minimal downtime.
When to Use
Database schema changes
Adding/removing/modifying columns
Migrating between database systems
Data transformations and cleanup
Splitting or merging tables
Changing data types
Adding indexes and constraints
Backfilling data
Multi-tenant data migrations
Quick Start
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)
Reference Guides
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
Best Practices
✅ DO
Always write both up and down migrations
Test migrations on production-like data
Use transactions for atomic operations
Process large datasets in batches
Add indexes after data insertion
Validate data after migration
Log progress and errors
Use feature flags for application code changes
Back up database before running migrations
Test rollback procedures
Document migration side effects
Version control all migrations
Use idempotent operations
❌ DON'T
Run untested migrations on production
Make breaking changes without backwards compatibility
Process millions of rows in single transaction
Skip rollback implementation
Ignore migration failures
Modify old migrations
Delete data without backups
Run migrations manually in production
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: aj-geddes-useful-ai-prompts-data-migration-scripts3description: Data Migration Scripts4---56# Data Migration Scripts78## Table of Contents910- [Overview](#overview)11- [When to Use](#when-to-use)12- [Quick Start](#quick-start)13- [Reference Guides](#reference-guides)14- [Best Practices](#best-practices)1516## Overview1718Create robust, safe, and reversible data migration scripts for database schema changes and data transformations with minimal downtime.1920## When to Use2122- Database schema changes23- Adding/removing/modifying columns24- Migrating between database systems25- Data transformations and cleanup26- Splitting or merging tables27- Changing data types28- Adding indexes and constraints29- Backfilling data30- Multi-tenant data migrations3132## Quick Start3334Minimal working example:3536```typescript37import { Knex } from "knex";3839// migrations/20240101000000_add_user_preferences.ts40export async function up(knex: Knex): Promise<void> {41 // Create new table42 await knex.schema.createTable("user_preferences", (table) => {43 table.uuid("id").primary().defaultTo(knex.raw("gen_random_uuid()"));44 table45 .uuid("user_id")46 .notNullable()47 .references("id")48 .inTable("users")49 .onDelete("CASCADE");50 table.jsonb("preferences").defaultTo("{}");51 table.timestamp("created_at").defaultTo(knex.fn.now());52 table.timestamp("updated_at").defaultTo(knex.fn.now());5354 table.index("user_id");55 });5657 // Migrate existing data58 await knex.raw(`59 INSERT INTO user_preferences (user_id, preferences)60 SELECT id, jsonb_build_object(61 'theme', COALESCE(theme, 'light'),62// ... (see reference guides for full implementation)63```6465## Reference Guides6667Detailed implementations in the `references/` directory:6869| Guide | Contents |70|---|---|71| [Knex.js Migrations (Node.js)](references/knexjs-migrations-nodejs.md) | Knex.js Migrations (Node.js) |72| [Alembic Migrations (Python/SQLAlchemy)](references/alembic-migrations-pythonsqlalchemy.md) | Alembic Migrations (Python/SQLAlchemy) |73| [Large Data Migration with Batching](references/large-data-migration-with-batching.md) | Large Data Migration with Batching |74| [Zero-Downtime Migration Pattern](references/zero-downtime-migration-pattern.md) | Zero-Downtime Migration Pattern |75| [Migration Validation](references/migration-validation.md) | Migration Validation |76| [Cross-Database Migration](references/cross-database-migration.md) | Cross-Database Migration |7778## Best Practices7980### ✅ DO8182- Always write both `up` and `down` migrations83- Test migrations on production-like data84- Use transactions for atomic operations85- Process large datasets in batches86- Add indexes after data insertion87- Validate data after migration88- Log progress and errors89- Use feature flags for application code changes90- Back up database before running migrations91- Test rollback procedures92- Document migration side effects93- Version control all migrations94- Use idempotent operations9596### ❌ DON'T9798- Run untested migrations on production99- Make breaking changes without backwards compatibility100- Process millions of rows in single transaction101- Skip rollback implementation102- Ignore migration failures103- Modify old migrations104- Delete data without backups105- Run migrations manually in production106107---108> Converted and distributed by [TomeVault](https://tomevault.io/claim/aj-geddes) — claim your Tome and manage your conversions.109<!-- tomevault:4.0:skill_md:2026-04-11 -->
Run npx skillmds@latest add tomevault-io/aj-geddes-useful-ai-prompts-data-migration-scripts in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Data Migration Scripts It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.