From d256fdc7a5a30e8548c3eb2e728ac311cb59e183 Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Tue, 13 Jan 2026 20:12:51 +0200 Subject: [PATCH] Make migration 1 idempotent --- .../versions/1_add_github_connector_enum.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/surfsense_backend/alembic/versions/1_add_github_connector_enum.py b/surfsense_backend/alembic/versions/1_add_github_connector_enum.py index 235908b1f..ee008d8c5 100644 --- a/surfsense_backend/alembic/versions/1_add_github_connector_enum.py +++ b/surfsense_backend/alembic/versions/1_add_github_connector_enum.py @@ -7,6 +7,8 @@ Revises: from collections.abc import Sequence +import sqlalchemy as sa + from alembic import op # Import pgvector if needed for other types, though not for this ENUM change @@ -20,9 +22,25 @@ branch_labels: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None +def enum_exists(enum_name: str) -> bool: + """Check if an enum type exists in the database.""" + conn = op.get_bind() + result = conn.execute( + sa.text( + "SELECT EXISTS (SELECT 1 FROM pg_type WHERE typname = :enum_name)" + ), + {"enum_name": enum_name}, + ) + return result.scalar() + + def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### + # Skip if the enum doesn't exist (fresh DB after downgrade - create_db_and_tables will handle it) + if not enum_exists("searchsourceconnectortype"): + return + # Manually add the command to add the enum value # Note: It's generally better to let autogenerate handle this, but we're bypassing it op.execute( @@ -51,6 +69,10 @@ END$$; def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### + # Skip if the enum doesn't exist + if not enum_exists("searchsourceconnectortype"): + return + # Downgrading removal of an enum value is complex and potentially dangerous # if the value is in use. Often omitted or requires manual SQL based on context. # For now, we'll just pass. If you needed to reverse this, you'd likely