refactor migration to use op.execute pattern

This commit is contained in:
CREDO23 2026-01-23 20:02:42 +02:00
parent 68dc7723af
commit 3a8a9734d6

View file

@ -10,8 +10,6 @@ public sharing of chat threads via secure tokenized URLs.
from collections.abc import Sequence from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op from alembic import op
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
@ -24,20 +22,19 @@ depends_on: str | Sequence[str] | None = None
def upgrade() -> None: def upgrade() -> None:
"""Add public sharing columns to new_chat_threads.""" """Add public sharing columns to new_chat_threads."""
# Add public_share_token column # Add public_share_token column
op.add_column( op.execute(
"new_chat_threads", """
sa.Column("public_share_token", sa.String(64), nullable=True), ALTER TABLE new_chat_threads
ADD COLUMN IF NOT EXISTS public_share_token VARCHAR(64);
"""
) )
# Add public_share_enabled column # Add public_share_enabled column with default false
op.add_column( op.execute(
"new_chat_threads", """
sa.Column( ALTER TABLE new_chat_threads
"public_share_enabled", ADD COLUMN IF NOT EXISTS public_share_enabled BOOLEAN NOT NULL DEFAULT FALSE;
sa.Boolean(), """
nullable=False,
server_default="false",
),
) )
# Add unique partial index on public_share_token (only non-null values) # Add unique partial index on public_share_token (only non-null values)
@ -45,7 +42,7 @@ def upgrade() -> None:
""" """
CREATE UNIQUE INDEX IF NOT EXISTS ix_new_chat_threads_public_share_token CREATE UNIQUE INDEX IF NOT EXISTS ix_new_chat_threads_public_share_token
ON new_chat_threads(public_share_token) ON new_chat_threads(public_share_token)
WHERE public_share_token IS NOT NULL WHERE public_share_token IS NOT NULL;
""" """
) )
@ -54,7 +51,7 @@ def upgrade() -> None:
""" """
CREATE INDEX IF NOT EXISTS ix_new_chat_threads_public_share_enabled CREATE INDEX IF NOT EXISTS ix_new_chat_threads_public_share_enabled
ON new_chat_threads(public_share_enabled) ON new_chat_threads(public_share_enabled)
WHERE public_share_enabled = TRUE WHERE public_share_enabled = TRUE;
""" """
) )
@ -63,5 +60,7 @@ def downgrade() -> None:
"""Remove public sharing columns from new_chat_threads.""" """Remove public sharing columns from new_chat_threads."""
op.execute("DROP INDEX IF EXISTS ix_new_chat_threads_public_share_enabled") op.execute("DROP INDEX IF EXISTS ix_new_chat_threads_public_share_enabled")
op.execute("DROP INDEX IF EXISTS ix_new_chat_threads_public_share_token") op.execute("DROP INDEX IF EXISTS ix_new_chat_threads_public_share_token")
op.drop_column("new_chat_threads", "public_share_enabled") op.execute(
op.drop_column("new_chat_threads", "public_share_token") "ALTER TABLE new_chat_threads DROP COLUMN IF EXISTS public_share_enabled"
)
op.execute("ALTER TABLE new_chat_threads DROP COLUMN IF EXISTS public_share_token")