From 16f79aa88b89a99f1d5640b6a64c597362512ada Mon Sep 17 00:00:00 2001 From: Manoj Aggarwal Date: Fri, 16 Jan 2026 11:00:20 -0800 Subject: [PATCH] fix migrations --- ..._allow_multiple_connectors_with_unique_.py | 94 ++++++++++++++----- .../versions/63_add_message_author_id.py | 9 +- .../a1b2c3d4e5f6_add_mcp_connector_type.py | 4 +- 3 files changed, 74 insertions(+), 33 deletions(-) diff --git a/surfsense_backend/alembic/versions/5263aa4e7f94_allow_multiple_connectors_with_unique_.py b/surfsense_backend/alembic/versions/5263aa4e7f94_allow_multiple_connectors_with_unique_.py index de9505e3a..d49009c94 100644 --- a/surfsense_backend/alembic/versions/5263aa4e7f94_allow_multiple_connectors_with_unique_.py +++ b/surfsense_backend/alembic/versions/5263aa4e7f94_allow_multiple_connectors_with_unique_.py @@ -7,6 +7,8 @@ Create Date: 2026-01-13 12:23:31.481643 """ from collections.abc import Sequence +from sqlalchemy import text + from alembic import op # revision identifiers, used by Alembic. @@ -18,33 +20,77 @@ depends_on: str | Sequence[str] | None = None def upgrade() -> None: """Upgrade schema.""" - # Drop the old unique constraint - op.drop_constraint( - 'uq_searchspace_user_connector_type', - 'search_source_connectors', - type_='unique' - ) + connection = op.get_bind() - # Create new unique constraint that includes name - op.create_unique_constraint( - 'uq_searchspace_user_connector_type_name', - 'search_source_connectors', - ['search_space_id', 'user_id', 'connector_type', 'name'] - ) + # Check if old constraint exists before trying to drop it + old_constraint_exists = connection.execute( + text(""" + SELECT 1 FROM information_schema.table_constraints + WHERE table_name='search_source_connectors' + AND constraint_type='UNIQUE' + AND constraint_name='uq_searchspace_user_connector_type' + """) + ).scalar() + + if old_constraint_exists: + op.drop_constraint( + 'uq_searchspace_user_connector_type', + 'search_source_connectors', + type_='unique' + ) + + # Check if new constraint already exists before creating + new_constraint_exists = connection.execute( + text(""" + SELECT 1 FROM information_schema.table_constraints + WHERE table_name='search_source_connectors' + AND constraint_type='UNIQUE' + AND constraint_name='uq_searchspace_user_connector_type_name' + """) + ).scalar() + + if not new_constraint_exists: + op.create_unique_constraint( + 'uq_searchspace_user_connector_type_name', + 'search_source_connectors', + ['search_space_id', 'user_id', 'connector_type', 'name'] + ) def downgrade() -> None: """Downgrade schema.""" - # Drop the new constraint - op.drop_constraint( - 'uq_searchspace_user_connector_type_name', - 'search_source_connectors', - type_='unique' - ) + connection = op.get_bind() - # Restore the old constraint - op.create_unique_constraint( - 'uq_searchspace_user_connector_type', - 'search_source_connectors', - ['search_space_id', 'user_id', 'connector_type'] - ) + # Check if new constraint exists before dropping + new_constraint_exists = connection.execute( + text(""" + SELECT 1 FROM information_schema.table_constraints + WHERE table_name='search_source_connectors' + AND constraint_type='UNIQUE' + AND constraint_name='uq_searchspace_user_connector_type_name' + """) + ).scalar() + + if new_constraint_exists: + op.drop_constraint( + 'uq_searchspace_user_connector_type_name', + 'search_source_connectors', + type_='unique' + ) + + # Only restore old constraint if it doesn't exist + old_constraint_exists = connection.execute( + text(""" + SELECT 1 FROM information_schema.table_constraints + WHERE table_name='search_source_connectors' + AND constraint_type='UNIQUE' + AND constraint_name='uq_searchspace_user_connector_type' + """) + ).scalar() + + if not old_constraint_exists: + op.create_unique_constraint( + 'uq_searchspace_user_connector_type', + 'search_source_connectors', + ['search_space_id', 'user_id', 'connector_type'] + ) diff --git a/surfsense_backend/alembic/versions/63_add_message_author_id.py b/surfsense_backend/alembic/versions/63_add_message_author_id.py index 2fc3f0b4c..42d4af85e 100644 --- a/surfsense_backend/alembic/versions/63_add_message_author_id.py +++ b/surfsense_backend/alembic/versions/63_add_message_author_id.py @@ -37,11 +37,6 @@ def upgrade() -> None: def downgrade() -> None: """Remove author_id column from new_chat_messages table.""" - op.execute( - """ - DROP INDEX IF EXISTS ix_new_chat_messages_author_id; - ALTER TABLE new_chat_messages - DROP COLUMN IF EXISTS author_id; - """ - ) + op.execute("DROP INDEX IF EXISTS ix_new_chat_messages_author_id") + op.execute("ALTER TABLE new_chat_messages DROP COLUMN IF EXISTS author_id") diff --git a/surfsense_backend/alembic/versions/a1b2c3d4e5f6_add_mcp_connector_type.py b/surfsense_backend/alembic/versions/a1b2c3d4e5f6_add_mcp_connector_type.py index e47bb2fa3..2fb4b65c2 100644 --- a/surfsense_backend/alembic/versions/a1b2c3d4e5f6_add_mcp_connector_type.py +++ b/surfsense_backend/alembic/versions/a1b2c3d4e5f6_add_mcp_connector_type.py @@ -1,7 +1,7 @@ """Add MCP connector type Revision ID: a1b2c3d4e5f6 -Revises: 61 +Revises: 63 Create Date: 2026-01-09 15:19:51.827647 """ @@ -11,7 +11,7 @@ from alembic import op # revision identifiers, used by Alembic. revision: str = 'a1b2c3d4e5f6' -down_revision: str | None = '61' +down_revision: str | None = '63' branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None