"""add query route memories Revision ID: 20260415_add_query_route_memories Revises: 20260413_rename_control_plane_status Create Date: 2026-04-15 """ 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 = "20260415_add_query_route_memories" down_revision = "20260413_rename_control_plane_status" 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 upgrade() -> None: bind = op.get_bind() if _table_exists(bind, "query_route_memories"): return op.create_table( "query_route_memories", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False), sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False), sa.Column("qa_record_id", sa.Integer(), sa.ForeignKey("qa_records.id", ondelete="CASCADE"), nullable=False), sa.Column("project_id", sa.BigInteger(), nullable=True), sa.Column("question_text", sa.Text(), nullable=False), sa.Column("route_mode_predicted", sa.String(length=32), nullable=False), sa.Column("route_mode_effective", sa.String(length=32), nullable=False), sa.Column("decision_origin", sa.String(length=64), nullable=True), sa.Column("reply_contract", _json_type(bind), nullable=True), sa.Column("probe_snapshot", _json_type(bind), nullable=True), sa.Column("query_profile", _json_type(bind), nullable=True), sa.Column("feedback_outcome", _json_type(bind), nullable=True), sa.Column("question_embedding", _json_type(bind), nullable=True), sa.UniqueConstraint( "tenant_id", "qa_record_id", name="uq_query_route_memories_tenant_qa_record", ), ) op.create_index("ix_query_route_memories_tenant_id", "query_route_memories", ["tenant_id"], unique=False) op.create_index("ix_query_route_memories_qa_record_id", "query_route_memories", ["qa_record_id"], unique=False) op.create_index("ix_query_route_memories_project_id", "query_route_memories", ["project_id"], unique=False) op.create_index( "ix_query_route_memories_route_mode_predicted", "query_route_memories", ["route_mode_predicted"], unique=False, ) op.create_index( "ix_query_route_memories_route_mode_effective", "query_route_memories", ["route_mode_effective"], unique=False, ) op.create_index( "ix_query_route_memories_decision_origin", "query_route_memories", ["decision_origin"], unique=False, ) def downgrade() -> None: bind = op.get_bind() if not _table_exists(bind, "query_route_memories"): return for index_name in ( "ix_query_route_memories_decision_origin", "ix_query_route_memories_route_mode_effective", "ix_query_route_memories_route_mode_predicted", "ix_query_route_memories_project_id", "ix_query_route_memories_qa_record_id", "ix_query_route_memories_tenant_id", ): op.drop_index(index_name, table_name="query_route_memories") op.drop_table("query_route_memories")