AgentSkillsCN

migration

适用于任何ORM或数据库栈的安全数据库模式迁移。适用场景: - 当您需要对数据库结构进行变更(新增或删除列、表)时; - 当您需要安全地执行迁移操作时; - 当您需要管理数据库版本与回滚机制时; - 当您需要评估迁移风险或规划安全部署时; - 当您需要应对模式漂移或迁移冲突时。 触发短语:数据库迁移、模式变更、新增列、迁移回滚、模式漂移。

SKILL.md
--- frontmatter
name: migration
description: |
  Safe database schema migration patterns applicable to any ORM/database stack. Use when:
  - Making database schema changes (add/remove columns, tables)
  - Running migrations safely
  - Managing database versioning and rollbacks
  - Assessing migration risk or planning safe deployment
  - Dealing with schema drift or migration conflicts
  Trigger phrases: database migration, schema change, add column, migration rollback, schema drift
allowed-tools: Bash, Read, AskUserQuestion, Write, Glob, Grep, WebSearch, WebFetch
model: sonnet
user-invocable: true

Database Migration

Safe schema migration patterns applicable to any ORM and database. This skill defines migration principles and safety patterns, not specific ORM commands.

Design Principles

  1. Discover project tools: Detect which ORM/migration tool the project uses
  2. Research commands: Use WebSearch for current ORM command syntax
  3. Safety first: Follow safe migration patterns regardless of tool
  4. Always have rollback: Plan for reverting changes

Migration Safety Checklist

Before any migration:

  • Backup exists or can be restored
  • Migration is reversible (down migration defined)
  • No data loss risk identified
  • Tested in non-production environment
  • Schema drift checked

Tool Discovery

Step 1: Detect Migration Tool

Look for migration-related configuration without assuming specific tools:

bash
# Check for migration directories and configs
ls -la migrations/ db/migrate/ alembic/ prisma/ drizzle/ 2>/dev/null
ls -la *migrate* *migration* *.prisma diesel.toml 2>/dev/null

# Check for migration tools in dependencies
grep -E 'prisma|drizzle|alembic|django|sequelize|typeorm|knex|goose|diesel|flyway' \
  package.json pyproject.toml requirements.txt go.mod Cargo.toml pom.xml 2>/dev/null

Step 2: Find Project Commands

bash
# Check for migration scripts
grep -E 'migrate|migration|db:' package.json 2>/dev/null
grep -E '^(migrate|db)' Makefile 2>/dev/null

Step 3: Research Current Commands

If tool is detected but commands are unfamiliar:

code
WebSearch: "[ORM/tool name] migration commands [year]"
WebFetch: [official docs] → "Extract migration CLI commands"

Safe Migration Patterns

These patterns apply to any database and migration tool:

Adding a Column

Safe approach (2-step for NOT NULL):

code
Step 1: Add column as nullable
Step 2: Backfill data if needed
Step 3: Add NOT NULL constraint (if required)

Why: Adding NOT NULL column directly fails on existing rows.

Removing a Column

Safe approach (3-step):

code
Step 1: Stop using column in application code
Step 2: Deploy application change
Step 3: Remove column in migration

Why: Removing column while code uses it causes errors.

Renaming a Column

Safe approach (4-step):

code
Step 1: Add new column (copy of old)
Step 2: Copy data from old to new
Step 3: Update code to use new column
Step 4: Remove old column

Why: Renaming directly breaks running code during deployment.

Adding an Index

Safe approach:

code
- For small tables: Direct index creation
- For large tables: Use concurrent/online index creation

Why: Index creation locks table; concurrent creation avoids downtime.

Note: Concurrent index syntax varies by database - research for your specific database.


Risk Assessment

Change TypeRisk LevelMitigation
Add nullable columnLowNone needed
Add column with defaultLowCheck default value
Add NOT NULL columnMediumAdd nullable first, backfill, then constrain
Drop columnHighEnsure code doesn't use it
Rename columnHighUse add/copy/drop pattern
Change column typeHighTest data compatibility
Add index (small table)LowNone needed
Add index (large table)MediumUse concurrent creation
Drop indexLowVerify not needed for queries

Rollback Strategy

Always plan for rollback:

  1. Reversible migrations: Define both up and down migrations
  2. Test rollback: Verify down migration works before deploying
  3. Keep rollback scripts: Store manual rollback SQL if needed
  4. Document rollback steps: Include in deployment checklist

Rollback Commands

Discover the rollback command for your migration tool:

code
WebSearch: "[migration tool] rollback command"

Production Checklist

Before deploying migrations to production:

  1. Migration tested locally
  2. Migration tested in staging/preview
  3. Database backup created/verified
  4. Rollback plan documented and tested
  5. Downtime window scheduled (if needed)
  6. Team notified of migration
  7. Monitoring ready for issues
  8. Application code compatible with both old and new schema

Common Issues

Schema Drift

When database differs from migrations:

  1. Compare current schema to expected schema
  2. Identify divergent changes
  3. Either:
    • Generate migration to match current state
    • Reset to migration state (dev only)

Migration Conflicts

When multiple migrations conflict:

  1. Pull latest migrations
  2. Check for ordering issues
  3. Resolve conflicts in migration files
  4. Re-run migrations

Failed Migration

When migration fails mid-way:

  1. Check what was applied
  2. Manually fix or rollback partial changes
  3. Fix migration and re-attempt
  4. Mark migration as resolved (tool-specific)

Rules (L1 - Hard)

Critical for data safety. Violations can cause data loss.

  • ALWAYS backup before migration (recovery requirement)
  • NEVER run destructive migrations without explicit confirmation
  • NEVER run reset/drop commands in production
  • NEVER assume migrations are automatically reversible

Defaults (L2 - Soft)

Important for safe operations. Override with reasoning when appropriate.

  • Test migrations in non-production first
  • Have a rollback plan documented
  • Discover the project's migration tool before running commands
  • Use WebSearch to verify current command syntax
  • Never hardcode ORM-specific commands (discover them)

Guidelines (L3)

Recommendations for robust migration practices.

  • Consider using the add/copy/drop pattern for column renames
  • Prefer concurrent index creation for large tables
  • Consider schema drift checks before migrations