From f1645a1715756d8181921db6946c0c91b7e44171 Mon Sep 17 00:00:00 2001 From: null Date: Wed, 20 May 2026 22:40:15 -0500 Subject: [PATCH] fix(db): make agent_id migration idempotent for fresh installs --- ...d9c3e4f5_add_agent_id_to_board_webhooks.py | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/backend/migrations/versions/b7a1d9c3e4f5_add_agent_id_to_board_webhooks.py b/backend/migrations/versions/b7a1d9c3e4f5_add_agent_id_to_board_webhooks.py index 02041f6..4467ca8 100644 --- a/backend/migrations/versions/b7a1d9c3e4f5_add_agent_id_to_board_webhooks.py +++ b/backend/migrations/versions/b7a1d9c3e4f5_add_agent_id_to_board_webhooks.py @@ -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") \ No newline at end of file