Files
aiagent/backend/alembic/versions/012_schedule_fk_set_null.py

44 lines
1.2 KiB
Python
Raw Normal View History

"""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",
)