migrate-create

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Migrate Create

迁移创建

Generate a new database migration with sequential numbering and up/down SQL file pair.
生成一个带有顺序编号和up/down SQL文件对的新数据库迁移。

When to use

使用场景

When you need to create a new database migration for schema changes such as creating tables, adding columns, creating indexes, or modifying constraints.
当你需要为模式变更创建新的数据库迁移时,例如创建表、添加列、创建索引或修改约束。

Steps

步骤

  1. Determine next number -- use
    Glob
    to scan the migrations directory for existing migration files and find the highest number, then increment by 1 (zero-pad to 3 digits)
  2. Select template -- based on the
    <name>
    , choose the appropriate SQL template:
    • Names starting with
      create_
      -> CREATE TABLE template
    • Names starting with
      add_
      -> ALTER TABLE ADD COLUMN template
    • Names starting with
      drop_
      -> DROP with safety checks
    • Names containing
      index
      -> CREATE INDEX template
    • Other -> generic migration template with placeholder comments
  3. Generate up migration -- write
    NNN_<name>.up.sql
    with the appropriate SQL using IF NOT EXISTS for idempotency
  4. Generate down migration -- write
    NNN_<name>.down.sql
    with the reverse operation using IF EXISTS
  5. Search patterns -- call
    mcp__claude-flow__agentdb_pattern-search
    to find similar past migrations for reference
  6. Store metadata -- call
    mcp__claude-flow__agentdb_hierarchical-store
    to record the migration in
    migrations
    namespace with number, name, status (pending), and file paths
  7. Report -- display: migration number, file paths created, template used, any similar past migrations found
  1. 确定下一个编号——使用
    Glob
    扫描迁移目录中的现有迁移文件,找到最大编号,然后加1(补零至3位)
  2. 选择模板——根据
    <name>
    选择合适的SQL模板:
    • create_
      开头的名称 -> CREATE TABLE模板
    • add_
      开头的名称 -> ALTER TABLE ADD COLUMN模板
    • drop_
      开头的名称 -> 带有安全检查的DROP模板
    • 包含
      index
      的名称 -> CREATE INDEX模板
    • 其他 -> 带有占位符注释的通用迁移模板
  3. 生成up迁移文件——编写
    NNN_<name>.up.sql
    ,使用IF NOT EXISTS确保幂等性
  4. 生成down迁移文件——编写
    NNN_<name>.down.sql
    ,使用IF EXISTS执行反向操作
  5. 搜索模式——调用
    mcp__claude-flow__agentdb_pattern-search
    查找类似的过往迁移作为参考
  6. 存储元数据——调用
    mcp__claude-flow__agentdb_hierarchical-store
    migrations
    命名空间中记录迁移信息,包括编号、名称、状态(pending)和文件路径
  7. 报告——显示:迁移编号、创建的文件路径、使用的模板、找到的任何类似过往迁移

CLI alternative

CLI替代方案

bash
npx @claude-flow/cli@latest memory store --namespace migrations --key "migration-NNN_NAME" --value '{"number": NNN, "name": "NAME", "status": "pending"}'
bash
npx @claude-flow/cli@latest memory store --namespace migrations --key "migration-NNN_NAME" --value '{"number": NNN, "name": "NAME", "status": "pending"}'