Data migration scripts

From aj-geddes/useful-ai-prompts · View interactive version

Data migration scripts is an AI agent skill from the GitHub repository aj-geddes/useful-ai-prompts. 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.

How to use this skill

Skill file

Data Migration Scripts

Table of Contents

Overview

Create robust, safe, and reversible data migration scripts for database schema changes and data transformations with minimal downtime.

When to Use

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

❌ DON'T

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.