fix: schedule_delete 外键约束导致无法删除定时任务

- schedule_delete_tool 执行前先解除 executions.schedule_id 引用
- 迁移 012: executions.schedule_id FK 改为 ON DELETE SET NULL

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
renjianbo
2026-05-07 23:08:31 +08:00
parent ded3ba2973
commit f30997c02a
2 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
"""alter executions.schedule_id FK to ON DELETE SET NULL
Revision ID: 012_schedule_fk_set_null
Revises: 011_add_agent_market_fields
Create Date: 2026-05-07
"""
from alembic import op
revision = "012_schedule_fk_set_null"
down_revision = "011_add_agent_market_fields"
branch_labels = None
depends_on = None
def upgrade() -> None:
"""将 executions.schedule_id 外键改为 SET NULL,删除定时任务时自动保留执行记录。"""
try:
op.drop_constraint("executions_ibfk_3", "executions", type_="foreignkey")
except Exception:
# FK name may vary; try alternate patterns
try:
op.drop_constraint("executions_ibfk_4", "executions", type_="foreignkey")
except Exception:
pass
op.create_foreign_key(
"executions_ibfk_3",
"executions", "agent_schedules",
["schedule_id"], ["id"],
ondelete="SET NULL",
)
def downgrade() -> None:
try:
op.drop_constraint("executions_ibfk_3", "executions", type_="foreignkey")
except Exception:
pass
op.create_foreign_key(
"executions_ibfk_3",
"executions", "agent_schedules",
["schedule_id"], ["id"],
ondelete="RESTRICT",
)