AgentSkillsCN

run_migration

安全执行数据库迁移操作,并配套制定回滚流程

SKILL.md
--- frontmatter
name: run_migration
description: Execute database migrations safely with rollback procedures
category: database
agent_affinity: database_architect

Run Migration Skill

Migration Execution Checklist

Pre-Migration

  • Backup database
  • Test migration on staging environment
  • Review migration script for errors
  • Ensure rollback (down migration) works
  • Notify team of migration window

During Migration

bash
# Run migration
npm run migrate:up
# or
python manage.py migrate
# or
npx prisma migrate deploy

Post-Migration

  • Verify schema changes applied
  • Run smoke tests
  • Check application functionality
  • Monitor error logs
  • Update schema documentation

If Migration Fails

bash
# Rollback migration
npm run migrate:down
# or
python manage.py migrate <previous_version>
# or
npx prisma migrate reset

Migration Best Practices

  1. Backwards Compatible: Ensure app works during migration
  2. Transactional: Wrap in BEGIN/COMMIT
  3. Idempotent: Safe to run multiple times
  4. Tested: Test on staging first
  5. Documented: Clear comments in migration

Example Migration

sql
-- Up migration
BEGIN;

ALTER TABLE users ADD COLUMN phone VARCHAR(20);
CREATE INDEX idx_users_phone ON users(phone);

COMMIT;
sql
-- Down migration  
BEGIN;

DROP INDEX IF EXISTS idx_users_phone;
ALTER TABLE users DROP COLUMN IF EXISTS phone;

COMMIT;

Related Skills

  • design_database_schema - Design schemas before migrating