"""Add policy_id column to llm_validation_results

Revision ID: e5f4d3c2b1a
Revises: a7b1c2d3e4f5
Create Date: 2026-05-21 12:00:00.000000

"""

from collections.abc import Sequence

from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision: str = "e5f4d3c2b1a"
down_revision: str | Sequence[str] | None = "a7b1c2d3e4f5"
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),
    )


def downgrade() -> None:
    """Remove policy_id column from llm_validation_results."""
    op.drop_column("llm_validation_results", "policy_id")
