fix (migration1): add 'if not exixst' guard

This commit is contained in:
CREDO23 2025-12-02 20:40:59 +00:00
parent 509ef211fa
commit 6c43e8aa09
2 changed files with 43 additions and 32 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@
./surfsense_backend/podcasts/ ./surfsense_backend/podcasts/
.env .env
node_modules/ node_modules/
.ruff_cache/ .ruff_cache/
.venv

View file

@ -2,17 +2,12 @@
Revision ID: 1 Revision ID: 1
Revises: Revises:
""" """
from collections.abc import Sequence from collections.abc import Sequence
from alembic import op 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 identifiers, used by Alembic.
revision: str = "1" revision: str = "1"
down_revision: str | None = None down_revision: str | None = None
@ -21,10 +16,24 @@ depends_on: str | Sequence[str] | None = None
def upgrade() -> 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 # Add the new enum value if it doesn't exist
# Note: It's generally better to let autogenerate handle this, but we're bypassing it
op.execute( op.execute(
""" """
DO $$ 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: 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 -- Create new type without GITHUB_CONNECTOR
# if the value is in use. Often omitted or requires manual SQL based on context. CREATE TYPE searchsourceconnectortype AS ENUM(
# For now, we'll just pass. If you needed to reverse this, you'd likely 'SERPER_API',
# have to manually check if 'GITHUB_CONNECTOR' is used in the table 'TAVILY_API',
# and then potentially recreate the type without it. 'SLACK_CONNECTOR',
op.execute( 'NOTION_CONNECTOR'
"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")
pass -- Update table columns to use new type
# ### end Alembic commands ### ALTER TABLE search_source_connectors
ALTER COLUMN connector_type TYPE searchsourceconnectortype
USING connector_type::text::searchsourceconnectortype;
-- Drop old type
DROP TYPE searchsourceconnectortype_old;
END$$;
"""
)