home / skills / ntaksh42 / agents / database-migration-generator

database-migration-generator skill

/.claude/skills/database-migration-generator

This skill generates database migration scripts with up and down migrations, covering table creation, alterations, indexes, and foreign keys across databases.

npx playbooks add skill ntaksh42/agents --skill database-migration-generator

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.8 KB
---
name: database-migration-generator
description: Generate database migration scripts with rollback support for various databases. Use when creating schema migrations or database changes.
---

# Database Migration Generator Skill

データベースマイグレーションスクリプトを生成するスキルです。

## 主な機能

- **テーブル作成**: CREATE TABLE
- **カラム追加/削除**: ALTER TABLE
- **インデックス**: CREATE INDEX
- **外部キー**: FOREIGN KEY制約
- **ロールバック**: DOWN migration

## 生成例

```sql
-- migrations/001_create_users.up.sql
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    name VARCHAR(100) NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_users_email ON users(email);

-- migrations/001_create_users.down.sql
DROP TABLE IF EXISTS users;
```

## TypeORM (TypeScript)

```typescript
import { MigrationInterface, QueryRunner, Table } from 'typeorm';

export class CreateUsers1234567890 implements MigrationInterface {
    async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.createTable(new Table({
            name: 'users',
            columns: [
                {
                    name: 'id',
                    type: 'int',
                    isPrimary: true,
                    isGenerated: true,
                    generationStrategy: 'increment'
                },
                {
                    name: 'email',
                    type: 'varchar',
                    isUnique: true
                }
            ]
        }));
    }

    async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.dropTable('users');
    }
}
```

## バージョン情報
- Version: 1.0.0

Overview

This skill generates database migration scripts with built-in rollback support for a range of relational databases and common ORMs. It produces up/down SQL files and can scaffold migration classes for TypeORM-style projects. The output focuses on schema changes: create/drop tables, add/remove columns, indexes, and foreign keys, with clear rollback steps.

How this skill works

Provide the desired schema change or current/target models and the skill outputs paired migration artifacts: an "up" migration to apply changes and a "down" migration to revert them. It can emit raw SQL compatible with common dialects and scaffold migration code for TypeORM (TypeScript). Each migration includes explicit operations for schema, indexes, constraints, and timestamps to ensure safe rollbacks.

When to use it

  • When adding, removing, or renaming tables and columns in a production schema
  • When creating repeatable, auditable migration files for deployment pipelines
  • When you need both SQL migrations and ORM-compatible migration classes (e.g., TypeORM)
  • When introducing indexes or foreign-key constraints with reversible changes
  • When preparing rollback plans for schema changes before applying to production

Best practices

  • Review generated down migrations to ensure destructive rollbacks handle data carefully
  • Run migrations in a staging environment matching production to verify behavior
  • Keep migrations small and focused: one logical change per migration file
  • Include explicit index and constraint creation/removal to avoid implicit behavior
  • Version and name migration files clearly (timestamp or incremental) for ordering

Example use cases

  • Generate an initial users table with email uniqueness, timestamps, and an index
  • Add a nullable profile_id column and corresponding foreign key with rollback
  • Create and later drop a performance index during optimization work
  • Scaffold TypeORM MigrationInterface classes for a TypeScript service
  • Produce paired up/down SQL files to include in CI/CD deployment artifacts

FAQ

Which databases and dialects are supported?

The skill targets common relational dialects (PostgreSQL, MySQL, SQLite) and produces standard SQL and ORM scaffolding; dialect-specific adjustments may be needed for proprietary features.

Does the skill handle data migrations (transformations) as well as schema?

It primarily focuses on schema operations and reversible structural changes. Simple data transformations can be included, but complex ETL-like operations should be reviewed and tested carefully.