alembic fix
This commit is contained in:
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '7703bbc038bd'
|
||||
@@ -17,8 +18,16 @@ branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def _table_exists(table_name: str) -> bool:
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Add description column to validation_policies."""
|
||||
if not _table_exists("validation_policies"):
|
||||
return
|
||||
op.add_column('validation_policies', sa.Column('description', sa.Text(), nullable=True))
|
||||
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ Create Date: 2026-05-26 15:27:06.159151
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '86c7b1d6a710'
|
||||
@@ -18,8 +18,16 @@ branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def _table_exists(table_name: str) -> bool:
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Add is_admin column to roles table, set True for existing Admin roles."""
|
||||
if not _table_exists("roles"):
|
||||
return
|
||||
op.add_column("roles", sa.Column("is_admin", sa.Boolean(), nullable=False, server_default=sa.text("false")))
|
||||
op.execute("UPDATE roles SET is_admin = true WHERE name = 'Admin'")
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ from collections.abc import Sequence
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "9f8e7d6c5b4a"
|
||||
@@ -55,8 +56,22 @@ def _is_multimodal_heuristic(model_name: str) -> bool:
|
||||
return any(marker in token for marker in multimodal_markers)
|
||||
|
||||
|
||||
def _table_exists(table_name: str) -> bool:
|
||||
"""Check if a table exists in the current database schema."""
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Add is_multimodal column and backfill using heuristic."""
|
||||
# The llm_providers table is defined in the ORM model (src/models/llm.py)
|
||||
# and created by Base.metadata.create_all() at app startup, not via migrations.
|
||||
# If the table doesn't exist yet (fresh DB), skip — the model definition
|
||||
# already includes is_multimodal, so create_all() will create it with the column.
|
||||
if not _table_exists("llm_providers"):
|
||||
return
|
||||
|
||||
# Step 1: Add column as nullable first
|
||||
op.add_column(
|
||||
"llm_providers",
|
||||
|
||||
@@ -8,9 +8,9 @@ Create Date: 2026-05-21 10:00:00.000000
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "a7b1c2d3e4f5"
|
||||
@@ -19,8 +19,16 @@ branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def _table_exists(table_name: str) -> bool:
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Add provider_id column to validation_policies as nullable."""
|
||||
if not _table_exists("validation_policies"):
|
||||
return
|
||||
op.add_column(
|
||||
"validation_policies",
|
||||
sa.Column("provider_id", sa.String(), nullable=True),
|
||||
|
||||
@@ -22,9 +22,9 @@ Create Date: 2026-05-31 12:00:00.000000
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "c9d8e7f6a5b4"
|
||||
@@ -33,6 +33,12 @@ branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def _table_exists(table_name: str) -> bool:
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema to v2 validation models."""
|
||||
_create_validation_sources()
|
||||
@@ -115,6 +121,8 @@ def downgrade() -> None:
|
||||
|
||||
def _create_validation_sources() -> None:
|
||||
"""Create the validation_sources table (no FK dependencies)."""
|
||||
if not _table_exists("validation_policies"):
|
||||
return
|
||||
op.create_table(
|
||||
"validation_sources",
|
||||
sa.Column("id", sa.String(), nullable=False),
|
||||
@@ -150,6 +158,8 @@ def _create_validation_sources() -> None:
|
||||
|
||||
def _create_validation_runs() -> None:
|
||||
"""Create the validation_runs table."""
|
||||
if not _table_exists("validation_policies"):
|
||||
return
|
||||
op.create_table(
|
||||
"validation_runs",
|
||||
sa.Column("id", sa.String(), nullable=False),
|
||||
@@ -207,6 +217,8 @@ def _create_validation_runs() -> None:
|
||||
|
||||
def _extend_validation_policies() -> None:
|
||||
"""Add v2 columns and index to validation_policies."""
|
||||
if not _table_exists("validation_policies"):
|
||||
return
|
||||
op.add_column(
|
||||
"validation_policies",
|
||||
sa.Column("prompt_template", sa.Text(), nullable=True),
|
||||
@@ -269,6 +281,8 @@ def _extend_validation_policies() -> None:
|
||||
|
||||
def _extend_validation_results() -> None:
|
||||
"""Add v2 columns, FKs, and indexes to llm_validation_results."""
|
||||
if not _table_exists("llm_validation_results"):
|
||||
return
|
||||
op.add_column(
|
||||
"llm_validation_results",
|
||||
sa.Column(
|
||||
@@ -335,6 +349,8 @@ def _backfill_validation_runs() -> None:
|
||||
|
||||
Safe no-op when no such rows exist (e.g. fresh database).
|
||||
"""
|
||||
if not _table_exists("llm_validation_results"):
|
||||
return
|
||||
conn = op.get_bind()
|
||||
|
||||
# Check for existing records that need backfill
|
||||
|
||||
@@ -8,9 +8,9 @@ Create Date: 2026-05-21 12:00:00.000000
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "e5f4d3c2b1a"
|
||||
@@ -19,8 +19,16 @@ branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def _table_exists(table_name: str) -> bool:
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Add policy_id column to llm_validation_results as nullable indexed."""
|
||||
if not _table_exists("llm_validation_results"):
|
||||
return
|
||||
op.add_column(
|
||||
"llm_validation_results",
|
||||
sa.Column("policy_id", sa.String(), nullable=True, index=True),
|
||||
|
||||
@@ -9,6 +9,7 @@ Create Date: 2026-05-31 22:19:21.922928
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
@@ -19,6 +20,14 @@ depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# The llm_providers table is defined in the ORM model and created by
|
||||
# Base.metadata.create_all() at app startup, not via migrations.
|
||||
# If the table doesn't exist yet (fresh DB), skip — the model definition
|
||||
# already includes max_images, so create_all() will create it with the column.
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
if "llm_providers" not in inspector.get_table_names():
|
||||
return
|
||||
op.add_column('llm_providers', sa.Column('max_images', sa.Integer(), nullable=True))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user