"""add prompt quality linkage Revision ID: 20260429_add_prompt_quality_linkage Revises: 20260429_add_prompt_release_checks Create Date: 2026-04-29 """ from __future__ import annotations from alembic import op import sqlalchemy as sa try: from sqlalchemy.dialects.postgresql import JSONB except Exception: # pragma: no cover JSONB = sa.JSON revision = "20260429_add_prompt_quality_linkage" down_revision = "20260429_add_prompt_release_checks" branch_labels = None depends_on = None def _json_type(bind): return JSONB() if bind.dialect.name == "postgresql" else sa.JSON() def _table_exists(bind, table_name: str) -> bool: return sa.inspect(bind).has_table(table_name) def _column_exists(bind, table_name: str, column_name: str) -> bool: inspector = sa.inspect(bind) if not inspector.has_table(table_name): return False return any(str(item.get("name") or "") == column_name for item in inspector.get_columns(table_name)) def _index_exists(bind, table_name: str, index_name: str) -> bool: if not _table_exists(bind, table_name): return False return index_name in {str(index.get("name") or "") for index in sa.inspect(bind).get_indexes(table_name)} def upgrade() -> None: bind = op.get_bind() table_name = "ai_call_logs" if _table_exists(bind, table_name): for column_name, column in ( ("prompt_key", sa.Column("prompt_key", sa.String(length=128), nullable=True)), ("prompt_release_id", sa.Column("prompt_release_id", sa.Integer(), nullable=True)), ("prompt_source", sa.Column("prompt_source", sa.String(length=32), nullable=True)), ): if not _column_exists(bind, table_name, column_name): op.add_column(table_name, column) for index_name, column_names in ( ("ix_ai_call_logs_prompt_key", ["prompt_key"]), ("ix_ai_call_logs_prompt_release_id", ["prompt_release_id"]), ("ix_ai_call_logs_prompt_source", ["prompt_source"]), ): if not _index_exists(bind, table_name, index_name): op.create_index(index_name, table_name, column_names, unique=False) if not _table_exists(bind, "prompt_golden_cases"): op.create_table( "prompt_golden_cases", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("prompt_key", sa.String(length=128), nullable=False), sa.Column("case_name", sa.String(length=255), nullable=False), sa.Column("status", sa.String(length=24), nullable=False, server_default="active"), sa.Column("input_payload", _json_type(bind), nullable=True), sa.Column("expected_contract", _json_type(bind), nullable=True), sa.Column("expected_contains", sa.Text(), nullable=True), sa.Column("source", sa.String(length=48), nullable=False, server_default="manual"), sa.Column("source_ai_call_log_id", sa.Integer(), nullable=True), sa.Column("created_by", sa.BigInteger(), nullable=True), sa.Column("notes", sa.Text(), nullable=True), sa.Column("created_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()), sa.Column("updated_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()), ) for index_name, column_names in ( ("ix_prompt_golden_cases_tenant_id", ["tenant_id"]), ("ix_prompt_golden_cases_prompt_key", ["prompt_key"]), ("ix_prompt_golden_cases_status", ["status"]), ("ix_prompt_golden_cases_source", ["source"]), ("ix_prompt_golden_cases_source_ai_call_log_id", ["source_ai_call_log_id"]), ("ix_prompt_golden_cases_created_by", ["created_by"]), ): op.create_index(index_name, "prompt_golden_cases", column_names, unique=False) def downgrade() -> None: bind = op.get_bind() if _table_exists(bind, "prompt_golden_cases"): for index_name in ( "ix_prompt_golden_cases_created_by", "ix_prompt_golden_cases_source_ai_call_log_id", "ix_prompt_golden_cases_source", "ix_prompt_golden_cases_status", "ix_prompt_golden_cases_prompt_key", "ix_prompt_golden_cases_tenant_id", ): if _index_exists(bind, "prompt_golden_cases", index_name): op.drop_index(index_name, table_name="prompt_golden_cases") op.drop_table("prompt_golden_cases") table_name = "ai_call_logs" if _table_exists(bind, table_name): for index_name in ( "ix_ai_call_logs_prompt_source", "ix_ai_call_logs_prompt_release_id", "ix_ai_call_logs_prompt_key", ): if _index_exists(bind, table_name, index_name): op.drop_index(index_name, table_name=table_name) for column_name in ("prompt_source", "prompt_release_id", "prompt_key"): if _column_exists(bind, table_name, column_name): op.drop_column(table_name, column_name)