fix(db): make agent_id migration idempotent for fresh installs

This commit is contained in:
null 2026-05-20 22:40:15 -05:00
parent 7cc9e86de1
commit f1645a1715
1 changed files with 34 additions and 13 deletions

View File

@ -1,7 +1,7 @@
"""Add optional agent mapping to board webhooks.
Revision ID: b7a1d9c3e4f5
Revises: a2f6c9d4b7e8
Revises: a9b1c2d3e4f7
Create Date: 2026-02-15 14:00:00.000000
"""
@ -19,20 +19,41 @@ depends_on = None
def upgrade() -> None:
"""Add optional mapped agent reference on board webhooks."""
op.add_column("board_webhooks", sa.Column("agent_id", sa.Uuid(), nullable=True))
op.create_index("ix_board_webhooks_agent_id", "board_webhooks", ["agent_id"], unique=False)
op.create_foreign_key(
"fk_board_webhooks_agent_id_agents",
"board_webhooks",
"agents",
["agent_id"],
["id"],
"""Add optional mapped agent reference on board webhooks.
Note: agent_id column and its index/fk may already exist if the
board_webhooks table was created with them (a1b2c3d4e5f5), so we
check before adding.
"""
conn = op.get_bind()
result = conn.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.columns "
"WHERE table_name='board_webhooks' AND column_name='agent_id'"
)
)
if result.scalar() == 0:
op.add_column("board_webhooks", sa.Column("agent_id", sa.Uuid(), nullable=True))
op.create_index("ix_board_webhooks_agent_id", "board_webhooks", ["agent_id"], unique=False)
op.create_foreign_key(
"fk_board_webhooks_agent_id_agents",
"board_webhooks",
"agents",
["agent_id"],
["id"],
)
def downgrade() -> None:
"""Remove optional mapped agent reference from board webhooks."""
op.drop_constraint("fk_board_webhooks_agent_id_agents", "board_webhooks", type_="foreignkey")
op.drop_index("ix_board_webhooks_agent_id", table_name="board_webhooks")
op.drop_column("board_webhooks", "agent_id")
conn = op.get_bind()
result = conn.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.columns "
"WHERE table_name='board_webhooks' AND column_name='agent_id'"
)
)
if result.scalar() > 0:
op.drop_constraint("fk_board_webhooks_agent_id_agents", "board_webhooks", type_="foreignkey")
op.drop_index("ix_board_webhooks_agent_id", table_name="board_webhooks")
op.drop_column("board_webhooks", "agent_id")