Database Migration Helper
This skill helps you create database migration files that follow your project's ORM conventions and naming patterns.
When to Use This Skill
- User requests to create a database migration
- Adding new tables or columns to the database
- Modifying existing database schema
- Creating indexes, constraints, or relationships
- User mentions "migration", "schema change", or "database update"
Instructions
1. Detect the ORM/Migration Tool
First, identify which ORM or migration tool the project uses:
- Prisma: Look for
prisma/schema.prisma or @prisma/client in package.json
- Sequelize: Look for
.sequelizerc or sequelize-cli in package.json
- Knex: Look for
knexfile.js or knex in package.json
- TypeORM: Look for
ormconfig.json or typeorm in package.json
- Alembic (Python): Look for
alembic.ini or alembic/ directory
- Django: Look for
manage.py and Django migrations in */migrations/
- Active Record (Rails): Look for
db/migrate/ directory
- Flyway: Look for
flyway.conf or db/migration/
- Liquibase: Look for
liquibase.properties or changelog files
Use Glob to search for these indicator files.
2. Examine Existing Migrations
Read existing migration files to understand:
- Naming conventions (timestamp format, description format)
- Directory structure
- Migration file format (SQL, JavaScript, TypeScript, Python, etc.)
- Coding patterns (up/down functions, forwards/rollback, etc.)
Use Grep to find recent migrations: look in common directories like:
prisma/migrations/
db/migrate/
migrations/ or database/migrations/
alembic/versions/
3. Generate Migration File
Based on the detected ORM, create an appropriate migration file:
Prisma
- Run
npx prisma migrate dev --name <description> OR
- Manually create migration SQL in
prisma/migrations/<timestamp>_<name>/migration.sql
Sequelize
- Generate:
npx sequelize-cli migration:generate --name <description>
- Then fill in the up/down functions with the schema changes
Knex
- Generate:
npx knex migrate:make <description>
- Fill in exports.up and exports.down functions
TypeORM
- Generate:
npm run typeorm migration:create src/migrations/<Name>
- Implement up() and down() methods
Alembic
- Generate:
alembic revision -m "<description>"
- Fill in upgrade() and downgrade() functions
Django
- Run:
python manage.py makemigrations
- Or manually create migration in
<app>/migrations/
Rails
- Generate:
rails generate migration <ClassName>
- Fill in the change method (or up/down for complex migrations)
4. Follow Naming Conventions
Use consistent, descriptive names:
- Good:
add_user_email_index, create_products_table, add_payment_status_to_orders
- Bad:
migration1, update, fix
Format based on project patterns:
- Timestamp prefix:
20231215120000_add_email_to_users
- Sequential:
001_create_users, 002_add_indexes
5. Include Both Up and Down/Rollback
Always provide both directions when supported:
- Up/Upgrade/Forward: Apply the schema change
- Down/Downgrade/Rollback: Revert the schema change
For ORMs that use reversible operations (Rails, some Sequelize), a single change method may be sufficient.
6. Migration Content Guidelines
Creating Tables:
- Define all columns with appropriate types
- Set NOT NULL constraints where appropriate
- Add primary keys
- Include timestamps (created_at, updated_at) if project uses them
- Add foreign keys and indexes in the same migration or separate if project prefers
Altering Tables:
- Be specific:
ADD COLUMN, DROP COLUMN, MODIFY COLUMN
- Handle existing data appropriately (defaults, backfills)
- Consider backwards compatibility
Adding Indexes:
- Name indexes clearly:
idx_users_email, idx_orders_user_id_created_at
- Use appropriate index types (B-tree, Hash, GIN, etc.)
- Consider partial indexes for large tables
Data Migrations:
- Separate schema migrations from data migrations if possible
- Be cautious with large datasets (batch operations)
- Test rollback with realistic data volumes
7. Validate Migration Safety
Before finalizing, check:
- Reversibility: Can the migration be rolled back?
- Data loss: Will any data be lost? Warn the user!
- Downtime: Will this lock tables? Consider online migrations for large tables
- Dependencies: Are there dependent migrations that must run first?
8. Testing Recommendations
Suggest to the user:
- Run migration on a development database first
- Test rollback functionality
- For production: test on a staging environment
- Review generated SQL (for ORMs that auto-generate)
ORM-Specific Templates
Reference the templates in templates/ directory:
prisma-migration.sql - Prisma migration example
sequelize-migration.js - Sequelize migration example
knex-migration.js - Knex migration example
typeorm-migration.ts - TypeORM migration example
alembic-migration.py - Alembic migration example
rails-migration.rb - Rails migration example
Best Practices
- One purpose per migration: Don't mix unrelated changes
- Descriptive names: Names should explain what the migration does
- Timestamps: Use the ORM's timestamp format for ordering
- Idempotent when possible: Safe to run multiple times
- Test rollbacks: Ensure down/rollback works correctly
- Document complex logic: Add comments for non-obvious operations
- Batch large operations: For data migrations affecting many rows
- Use transactions: Wrap operations in transactions when supported
Supporting Files
templates/: Migration templates for various ORMs
reference.md: Naming conventions and migration patterns
1---2name: database-migration-helper3description: Creates database migration files following project conventions for Prisma, Sequelize, Alembic, Knex, TypeORM, and other ORMs. Use when adding tables, modifying schemas, or when user mentions database changes.4allowed-tools: Read, Grep, Glob, Write, Bash5---67# Database Migration Helper89This skill helps you create database migration files that follow your project's ORM conventions and naming patterns.1011## When to Use This Skill1213- User requests to create a database migration14- Adding new tables or columns to the database15- Modifying existing database schema16- Creating indexes, constraints, or relationships17- User mentions "migration", "schema change", or "database update"1819## Instructions2021### 1. Detect the ORM/Migration Tool2223First, identify which ORM or migration tool the project uses:2425- **Prisma**: Look for `prisma/schema.prisma` or `@prisma/client` in package.json26- **Sequelize**: Look for `.sequelizerc` or `sequelize-cli` in package.json27- **Knex**: Look for `knexfile.js` or `knex` in package.json28- **TypeORM**: Look for `ormconfig.json` or `typeorm` in package.json29- **Alembic** (Python): Look for `alembic.ini` or `alembic/` directory30- **Django**: Look for `manage.py` and Django migrations in `*/migrations/`31- **Active Record** (Rails): Look for `db/migrate/` directory32- **Flyway**: Look for `flyway.conf` or `db/migration/`33- **Liquibase**: Look for `liquibase.properties` or changelog files3435Use Glob to search for these indicator files.3637### 2. Examine Existing Migrations3839Read existing migration files to understand:4041- Naming conventions (timestamp format, description format)42- Directory structure43- Migration file format (SQL, JavaScript, TypeScript, Python, etc.)44- Coding patterns (up/down functions, forwards/rollback, etc.)4546Use Grep to find recent migrations: look in common directories like:47- `prisma/migrations/`48- `db/migrate/`49- `migrations/` or `database/migrations/`50- `alembic/versions/`5152### 3. Generate Migration File5354Based on the detected ORM, create an appropriate migration file:5556#### Prisma57- Run `npx prisma migrate dev --name <description>` OR58- Manually create migration SQL in `prisma/migrations/<timestamp>_<name>/migration.sql`5960#### Sequelize61- Generate: `npx sequelize-cli migration:generate --name <description>`62- Then fill in the up/down functions with the schema changes6364#### Knex65- Generate: `npx knex migrate:make <description>`66- Fill in exports.up and exports.down functions6768#### TypeORM69- Generate: `npm run typeorm migration:create src/migrations/<Name>`70- Implement up() and down() methods7172#### Alembic73- Generate: `alembic revision -m "<description>"`74- Fill in upgrade() and downgrade() functions7576#### Django77- Run: `python manage.py makemigrations`78- Or manually create migration in `<app>/migrations/`7980#### Rails81- Generate: `rails generate migration <ClassName>`82- Fill in the change method (or up/down for complex migrations)8384### 4. Follow Naming Conventions8586Use consistent, descriptive names:8788- **Good**: `add_user_email_index`, `create_products_table`, `add_payment_status_to_orders`89- **Bad**: `migration1`, `update`, `fix`9091Format based on project patterns:92- Timestamp prefix: `20231215120000_add_email_to_users`93- Sequential: `001_create_users`, `002_add_indexes`9495### 5. Include Both Up and Down/Rollback9697Always provide both directions when supported:9899- **Up/Upgrade/Forward**: Apply the schema change100- **Down/Downgrade/Rollback**: Revert the schema change101102For ORMs that use reversible operations (Rails, some Sequelize), a single `change` method may be sufficient.103104### 6. Migration Content Guidelines105106**Creating Tables:**107- Define all columns with appropriate types108- Set NOT NULL constraints where appropriate109- Add primary keys110- Include timestamps (created_at, updated_at) if project uses them111- Add foreign keys and indexes in the same migration or separate if project prefers112113**Altering Tables:**114- Be specific: `ADD COLUMN`, `DROP COLUMN`, `MODIFY COLUMN`115- Handle existing data appropriately (defaults, backfills)116- Consider backwards compatibility117118**Adding Indexes:**119- Name indexes clearly: `idx_users_email`, `idx_orders_user_id_created_at`120- Use appropriate index types (B-tree, Hash, GIN, etc.)121- Consider partial indexes for large tables122123**Data Migrations:**124- Separate schema migrations from data migrations if possible125- Be cautious with large datasets (batch operations)126- Test rollback with realistic data volumes127128### 7. Validate Migration Safety129130Before finalizing, check:131132- **Reversibility**: Can the migration be rolled back?133- **Data loss**: Will any data be lost? Warn the user!134- **Downtime**: Will this lock tables? Consider online migrations for large tables135- **Dependencies**: Are there dependent migrations that must run first?136137### 8. Testing Recommendations138139Suggest to the user:140- Run migration on a development database first141- Test rollback functionality142- For production: test on a staging environment143- Review generated SQL (for ORMs that auto-generate)144145## ORM-Specific Templates146147Reference the templates in `templates/` directory:148149- `prisma-migration.sql` - Prisma migration example150- `sequelize-migration.js` - Sequelize migration example151- `knex-migration.js` - Knex migration example152- `typeorm-migration.ts` - TypeORM migration example153- `alembic-migration.py` - Alembic migration example154- `rails-migration.rb` - Rails migration example155156## Best Practices1571581. **One purpose per migration**: Don't mix unrelated changes1592. **Descriptive names**: Names should explain what the migration does1603. **Timestamps**: Use the ORM's timestamp format for ordering1614. **Idempotent when possible**: Safe to run multiple times1625. **Test rollbacks**: Ensure down/rollback works correctly1636. **Document complex logic**: Add comments for non-obvious operations1647. **Batch large operations**: For data migrations affecting many rows1658. **Use transactions**: Wrap operations in transactions when supported166167## Supporting Files168169- `templates/`: Migration templates for various ORMs170- `reference.md`: Naming conventions and migration patterns