AgentSkillsCN

Database Migration

数据库迁移

SKILL.md

Skill: Database Migration

Description

Handles safe database schema changes and data migrations.

When to Use

  • Adding/modifying database tables or columns
  • Changing relationships between entities
  • Data transformations

Instructions

Migration Rules

  1. Always reversible — every migration needs an up and down
  2. One change per migration — don't bundle unrelated schema changes
  3. Test on dev first — never run untested migrations in production
  4. Backup before production — always have a rollback plan
  5. No data loss — add columns as nullable first, then backfill

Drizzle Workflow

bash
# Generate migration from schema changes
npx drizzle-kit generate

# Review generated SQL in drizzle/ folder
# Apply migration
npx drizzle-kit push

# Or use migrate for production
npx drizzle-kit migrate

Naming Convention

YYYYMMDD_HHMMSS_description.sql Example: 20260212_143000_add_user_roles.sql

Dangerous Operations Checklist

  • Column rename → Create new, copy data, drop old (3-step)
  • Column type change → Verify data compatibility first
  • Drop table → Confirm no foreign key references
  • Add NOT NULL → Set default or backfill first