mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-11 00:32:38 +02:00
Make migration 1 idempotent
This commit is contained in:
parent
8646fecc8b
commit
d256fdc7a5
1 changed files with 22 additions and 0 deletions
|
|
@ -7,6 +7,8 @@ Revises:
|
||||||
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
|
||||||
# Import pgvector if needed for other types, though not for this ENUM change
|
# 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
|
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:
|
def upgrade() -> None:
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### 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
|
# Manually add the command to add the enum value
|
||||||
# Note: It's generally better to let autogenerate handle this, but we're bypassing it
|
# Note: It's generally better to let autogenerate handle this, but we're bypassing it
|
||||||
op.execute(
|
op.execute(
|
||||||
|
|
@ -51,6 +69,10 @@ END$$;
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### 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
|
# 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.
|
# 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
|
# For now, we'll just pass. If you needed to reverse this, you'd likely
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue