Skip to main content

修复不稳定的数据库迁移 Playbook

你的 db-migration Playbook 在简单的 schema 上工作正常,但一遇到外键(FK)就会崩溃。将四个会话链接输入 Advanced Devin,让它补上这些空缺。
AuthorCognition
CategoryDevin 优化
Features高级, Playbooks
1

跨会话发现模式

你的团队已经使用 !db-migration playbook 几周了。它在重命名列和添加索引时一直工作正常——但最近两次会话在迁移中途崩溃了,因为它们尝试删除的是被其他表引用的列。打开每个会话并查看失败点。在这个例子中,会话 3 和 4 都在同一步骤出错:
ERROR: cannot drop column "account_id" because other objects depend on it
DETAIL: constraint fk_orders_account_id on table orders depends on column account_id
HINT: Use DROP ... CASCADE to drop dependent objects too.
你现在有了一个明确的信号:这个 playbook 在执行破坏性操作前,没有检查外键依赖关系的步骤。两个会话之所以成功,是因为它们操作的是独立表;另外两个失败,则是因为它们不是在独立表上操作。
2

使用会话链接打开 Improve Playbook 选项卡

前往 app.devin.ai,点击输入框下方的 Advanced。选择 Improve Playbook 选项卡。在 playbook 下拉菜单中选择 !db-migration,然后在 session multi-dropdown 中选择全部四个会话——包括成功和失败的会话。包含成功的会话可以让 Devin 了解这个 playbook 做得好的地方,而不仅仅是它在什么地方出错。这个提示之所以有效,是因为:
  • 准确点出失败原因——用“外键约束(foreign key constraints)”而不是“它有时会失败”
  • 对比成功与失败——Devin 可以对比会话记录,查看它们从哪里开始分歧
  • 列出具体修复措施,同时也为 Devin 留出空间,让它发现你可能遗漏的问题
3

审查 playbook 的差异

Devin 会读取全部四个会话记录,精确定位失败与成功分歧的地方,并提出有针对性的修改建议。输出看起来就像是你的 playbook 的变更日志:
## Analysis

Sessions 1-2 succeeded because they only modified tables with no
incoming foreign keys. Sessions 3-4 failed at step 4 ("Drop the
old column") because the orders and invoices tables had FK
references to the target column.

## Changes to !db-migration

Added step 3a: "Before any DROP or ALTER that removes a column,
  query information_schema.key_column_usage to list dependent FKs.
  If any exist, generate ALTER TABLE ... DROP CONSTRAINT statements
  and execute them first. Save the original constraint DDL for
  rollback."

Added step 7: "Rollback procedure — if any step after FK removal
  fails, re-create the dropped constraints using the saved DDL
  from step 3a."

Updated Advice section: "Always run the migration against a staging
  database first. FK chains can be deeper than expected — a column
  in table A may be referenced by B, which is referenced by C.
  Query recursively."
playbook 会自动保存。如果有哪里不对,可以在同一个会话中回复——例如:“Also add a step to notify the #database Slack channel before running destructive migrations.”
4

在新的迁移上验证修复

你无需离开当前的 Advanced Devin 会话。playbook 更新保存后,使用同一个会话启动一个标准的 Devin 会话,在之前失败的同一场景下测试这个已更新的 playbook:如果这个会话成功,说明修复生效。如果它又碰到了新的边缘情况(例如循环外键引用),就把该会话再次送回 Improve Playbook 选项卡,进行新一轮改进。