Make migration 24 idempotent

This commit is contained in:
CREDO23 2026-01-13 20:13:08 +02:00
parent 8ed295c053
commit 60a7269ce8

View file

@ -7,6 +7,8 @@ Revises: 23
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.
@ -16,11 +18,27 @@ branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None depends_on: str | Sequence[str] | None = None
def table_exists(table_name: str) -> bool:
"""Check if a table exists in the database."""
conn = op.get_bind()
result = conn.execute(
sa.text(
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = :table_name)"
),
{"table_name": table_name},
)
return result.scalar()
def upgrade() -> None: def upgrade() -> None:
""" """
Fix any chats with NULL type values by setting them to QNA. Fix any chats with NULL type values by setting them to QNA.
This handles edge cases from previous migrations where type values were not properly migrated. This handles edge cases from previous migrations where type values were not properly migrated.
""" """
# Skip if chats table doesn't exist (fresh database)
if not table_exists("chats"):
return
# Update any NULL type values to QNA (the default chat type) # Update any NULL type values to QNA (the default chat type)
op.execute( op.execute(
""" """