DB Migration Helper
分析 model 变更,生成安全的数据库迁移 SQL。
Overview
对比代码中的实体/模型定义与当前数据库 schema,检测结构变更(新增表、增删列、改类型),生成向前兼容的迁移 SQL。支持 MySQL、PostgreSQL、SQLite。
When to Use
- User wants to create database migrations
- User modified model/entity definitions
- User mentions migration, schema change, or sync
- User says "生成迁移" / "create migration"
- User inputs
/db-migration-helper
When NOT to Use:
- User only wants to view database structure
- User wants data migration (not schema)
- User uses ORM auto-migration
- User wants to generate seed data
- User wants to backup/restore database
Core Pattern
Step 1: 检测项目类型和 ORM
Step 2: 提取当前 Schema
提取:
- 表名和列定义
- 列类型、约束(NOT NULL、DEFAULT、UNIQUE)
- 索引和外键
Step 3: 对比变更
对比代码中的 model 定义与已有 schema(或上一次迁移),检测:
Step 4: 生成迁移文件
使用 templates/migration.sql 模板,生成:
- Up 迁移 — 正向变更 SQL
- Down 回滚 — 反向回滚 SQL
- 风险评估 — 标注高风险操作


