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/
.env
node_modules/
.ruff_cache/
.ruff_cache/
.venv

View file

@ -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$$;
"""
)