diff --git a/.gitignore b/.gitignore index 342c0b258..3228f0dfe 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ ./surfsense_backend/podcasts/ .env node_modules/ -.ruff_cache/ \ No newline at end of file +.ruff_cache/ +.venv \ No newline at end of file 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..a359f8962 100644 --- a/surfsense_backend/alembic/versions/1_add_github_connector_enum.py +++ b/surfsense_backend/alembic/versions/1_add_github_connector_enum.py @@ -2,17 +2,12 @@ Revision ID: 1 Revises: - """ from collections.abc import Sequence from alembic import op -# Import pgvector if needed for other types, though not for this ENUM change -# import pgvector - - # revision identifiers, used by Alembic. revision: str = "1" down_revision: str | None = None @@ -21,10 +16,24 @@ depends_on: str | Sequence[str] | None = None def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### + # Ensure the enum type exists + op.execute( + """ +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'searchsourceconnectortype') THEN + CREATE TYPE searchsourceconnectortype AS ENUM( + 'SERPER_API', + 'TAVILY_API', + 'SLACK_CONNECTOR', + 'NOTION_CONNECTOR' + ); + END IF; +END$$; +""" + ) - # Manually add the command to add the enum value - # Note: It's generally better to let autogenerate handle this, but we're bypassing it + # Add the new enum value if it doesn't exist op.execute( """ DO $$ @@ -43,30 +52,31 @@ END$$; """ ) - # Pass for the rest, as autogenerate didn't run to add other schema details - pass - # ### end Alembic commands ### - def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### + # Removing an enum value safely requires recreating the type + op.execute( + """ +DO $$ +BEGIN + -- Rename existing type + ALTER TYPE searchsourceconnectortype RENAME TO searchsourceconnectortype_old; - # 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 - # have to manually check if 'GITHUB_CONNECTOR' is used in the table - # and then potentially recreate the type without it. - op.execute( - "ALTER TYPE searchsourceconnectortype RENAME TO searchsourceconnectortype_old" - ) - op.execute( - "CREATE TYPE searchsourceconnectortype AS ENUM('SERPER_API', 'TAVILY_API', 'SLACK_CONNECTOR', 'NOTION_CONNECTOR')" - ) - op.execute( - "ALTER TABLE search_source_connectors ALTER COLUMN connector_type TYPE searchsourceconnectortype USING " - "connector_type::text::searchsourceconnectortype" - ) - op.execute("DROP TYPE searchsourceconnectortype_old") + -- Create new type without GITHUB_CONNECTOR + CREATE TYPE searchsourceconnectortype AS ENUM( + 'SERPER_API', + 'TAVILY_API', + 'SLACK_CONNECTOR', + 'NOTION_CONNECTOR' + ); - pass - # ### end Alembic commands ### + -- Update table columns to use new type + ALTER TABLE search_source_connectors + ALTER COLUMN connector_type TYPE searchsourceconnectortype + USING connector_type::text::searchsourceconnectortype; + + -- Drop old type + DROP TYPE searchsourceconnectortype_old; +END$$; +""" + )