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.4---56# Database Migration Helper78This skill helps you create database migration files that follow your project's ORM conventions and naming patterns.910## When to Use This Skill1112- User requests to create a database migration13- Adding new tables or columns to the database14- Modifying existing database schema15- Creating indexes, constraints, or relationships16- User mentions "migration", "schema change", or "database update"1718## Instructions1920### 1. Detect the ORM/Migration Tool2122First, identify which ORM or migration tool the project uses:2324- **Prisma**: Look for `prisma/schema.prisma` or `@prisma/client` in package.json25- **Sequelize**: Look for `.sequelizerc` or `sequelize-cli` in package.json26- **Knex**: Look for `knexfile.js` or `knex` in package.json27- **TypeORM**: Look for `ormconfig.json` or `typeorm` in package.json28- **Alembic** (Python): Look for `alembic.ini` or `alembic/` directory29- **Django**: Look for `manage.py` and Django migrations in `*/migrations/`30- **Active Record** (Rails): Look for `db/migrate/` directory31- **Flyway**: Look for `flyway.conf` or `db/migration/`32- **Liquibase**: Look for `liquibase.properties` or changelog files3334Use Glob to search for these indicator files.3536### 2. Examine Existing Migrations3738Read existing migration files to understand:3940- Naming conventions (timestamp format, description format)41- Directory structure42- Migration file format (SQL, JavaScript, TypeScript, Python, etc.)43- Coding patterns (up/down functions, forwards/rollback, etc.)4445Use Grep to find recent migrations: look in common directories like:46- `prisma/migrations/`47- `db/migrate/`48- `migrations/` or `database/migrations/`49- `alembic/versions/`5051### 3. Generate Migration File5253Based on the detected ORM, create an appropriate migration file:5455#### Prisma56- Run `npx prisma migrate dev --name <description>` OR57- Manually create migration SQL in `prisma/migrations/<timestamp>_<name>/migration.sql`5859#### Sequelize60- Generate: `npx sequelize-cli migration:generate --name <description>`61- Then fill in the up/down functions with the schema changes6263#### Knex64- Generate: `npx knex migrate:make <description>`65- Fill in exports.up and exports.down functions6667#### TypeORM68- Generate: `npm run typeorm migration:create src/migrations/<Name>`69- Implement up() and down() methods7071#### Alembic72- Generate: `alembic revision -m "<description>"`73- Fill in upgrade() and downgrade() functions7475#### Django76- Run: `python manage.py makemigrations`77- Or manually create migration in `<app>/migrations/`7879#### Rails80- Generate: `rails generate migration <ClassName>`81- Fill in the change method (or up/down for complex migrations)8283### 4. Follow Naming Conventions8485Use consistent, descriptive names:8687- **Good**: `add_user_email_index`, `create_products_table`, `add_payment_status_to_orders`88- **Bad**: `migration1`, `update`, `fix`8990Format based on project patterns:91- Timestamp prefix: `20231215120000_add_email_to_users`92- Sequential: `001_create_users`, `002_add_indexes`9394### 5. Include Both Up and Down/Rollback9596Always provide both directions when supported:9798- **Up/Upgrade/Forward**: Apply the schema change99- **Down/Downgrade/Rollback**: Revert the schema change100101For ORMs that use reversible operations (Rails, some Sequelize), a single `change` method may be sufficient.102103### 6. Migration Content Guidelines104105**Creating Tables:**106- Define all columns with appropriate types107- Set NOT NULL constraints where appropriate108- Add primary keys109- Include timestamps (created_at, updated_at) if project uses them110- Add foreign keys and indexes in the same migration or separate if project prefers111112**Altering Tables:**113- Be specific: `ADD COLUMN`, `DROP COLUMN`, `MODIFY COLUMN`114- Handle existing data appropriately (defaults, backfills)115- Consider backwards compatibility116117**Adding Indexes:**118- Name indexes clearly: `idx_users_email`, `idx_orders_user_id_created_at`119- Use appropriate index types (B-tree, Hash, GIN, etc.)120- Consider partial indexes for large tables121122**Data Migrations:**123- Separate schema migrations from data migrations if possible124- Be cautious with large datasets (batch operations)125- Test rollback with realistic data volumes126127### 7. Validate Migration Safety128129Before finalizing, check:130131- **Reversibility**: Can the migration be rolled back?132- **Data loss**: Will any data be lost? Warn the user!133- **Downtime**: Will this lock tables? Consider online migrations for large tables134- **Dependencies**: Are there dependent migrations that must run first?135136### 8. Testing Recommendations137138Suggest to the user:139- Run migration on a development database first140- Test rollback functionality141- For production: test on a staging environment142- Review generated SQL (for ORMs that auto-generate)143144## ORM-Specific Templates145146Reference the templates in `templates/` directory:147148- `prisma-migration.sql` - Prisma migration example149- `sequelize-migration.js` - Sequelize migration example150- `knex-migration.js` - Knex migration example151- `typeorm-migration.ts` - TypeORM migration example152- `alembic-migration.py` - Alembic migration example153- `rails-migration.rb` - Rails migration example154155## Best Practices1561571. **One purpose per migration**: Don't mix unrelated changes1582. **Descriptive names**: Names should explain what the migration does1593. **Timestamps**: Use the ORM's timestamp format for ordering1604. **Idempotent when possible**: Safe to run multiple times1615. **Test rollbacks**: Ensure down/rollback works correctly1626. **Document complex logic**: Add comments for non-obvious operations1637. **Batch large operations**: For data migrations affecting many rows1648. **Use transactions**: Wrap operations in transactions when supported165166## Supporting Files167168- `templates/`: Migration templates for various ORMs169- `reference.md`: Naming conventions and migration patterns