137 lines
6.7 KiB
Python
137 lines
6.7 KiB
Python
"""add prompt replay runs
|
|
|
|
Revision ID: 20260430_add_prompt_replay_runs
|
|
Revises: 20260429_add_prompt_quality_linkage
|
|
Create Date: 2026-04-30
|
|
"""
|
|
|
|
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 = "20260430_add_prompt_replay_runs"
|
|
down_revision = "20260429_add_prompt_quality_linkage"
|
|
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 _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()
|
|
if not _table_exists(bind, "prompt_replay_runs"):
|
|
op.create_table(
|
|
"prompt_replay_runs",
|
|
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("release_id", sa.Integer(), nullable=False),
|
|
sa.Column("baseline_release_id", sa.Integer(), nullable=True),
|
|
sa.Column("run_status", sa.String(length=24), nullable=False, server_default="pending"),
|
|
sa.Column("run_mode", sa.String(length=32), nullable=False, server_default="contract_smoke"),
|
|
sa.Column("case_count", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("passed_count", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("failed_count", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("warning_count", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("score_delta", sa.Float(), nullable=True),
|
|
sa.Column("summary", sa.Text(), nullable=True),
|
|
sa.Column("risk_flags_json", _json_type(bind), nullable=True),
|
|
sa.Column("created_by", sa.BigInteger(), nullable=True),
|
|
sa.Column("started_at", sa.String(length=64), nullable=True),
|
|
sa.Column("completed_at", sa.String(length=64), 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()),
|
|
sa.ForeignKeyConstraint(["release_id"], ["prompt_releases.id"], ondelete="CASCADE"),
|
|
)
|
|
for index_name, column_names in (
|
|
("ix_prompt_replay_runs_tenant_id", ["tenant_id"]),
|
|
("ix_prompt_replay_runs_prompt_key", ["prompt_key"]),
|
|
("ix_prompt_replay_runs_release_id", ["release_id"]),
|
|
("ix_prompt_replay_runs_baseline_release_id", ["baseline_release_id"]),
|
|
("ix_prompt_replay_runs_run_status", ["run_status"]),
|
|
("ix_prompt_replay_runs_run_mode", ["run_mode"]),
|
|
("ix_prompt_replay_runs_created_by", ["created_by"]),
|
|
):
|
|
op.create_index(index_name, "prompt_replay_runs", column_names, unique=False)
|
|
|
|
if not _table_exists(bind, "prompt_replay_case_results"):
|
|
op.create_table(
|
|
"prompt_replay_case_results",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("tenant_id", sa.BigInteger(), nullable=False),
|
|
sa.Column("run_id", sa.Integer(), nullable=False),
|
|
sa.Column("golden_case_id", sa.Integer(), nullable=False),
|
|
sa.Column("prompt_key", sa.String(length=128), nullable=False),
|
|
sa.Column("release_id", sa.Integer(), nullable=False),
|
|
sa.Column("baseline_output", sa.Text(), nullable=True),
|
|
sa.Column("candidate_output", sa.Text(), nullable=True),
|
|
sa.Column("status", sa.String(length=24), nullable=False),
|
|
sa.Column("contract_result_json", _json_type(bind), nullable=True),
|
|
sa.Column("diff_summary", sa.Text(), nullable=True),
|
|
sa.Column("risk_flags_json", _json_type(bind), nullable=True),
|
|
sa.Column("latency_ms", sa.Integer(), nullable=True),
|
|
sa.Column("estimated_cost", sa.Float(), nullable=True),
|
|
sa.Column("error_summary", 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()),
|
|
sa.ForeignKeyConstraint(["run_id"], ["prompt_replay_runs.id"], ondelete="CASCADE"),
|
|
sa.ForeignKeyConstraint(["golden_case_id"], ["prompt_golden_cases.id"], ondelete="CASCADE"),
|
|
)
|
|
for index_name, column_names in (
|
|
("ix_prompt_replay_case_results_tenant_id", ["tenant_id"]),
|
|
("ix_prompt_replay_case_results_run_id", ["run_id"]),
|
|
("ix_prompt_replay_case_results_golden_case_id", ["golden_case_id"]),
|
|
("ix_prompt_replay_case_results_prompt_key", ["prompt_key"]),
|
|
("ix_prompt_replay_case_results_release_id", ["release_id"]),
|
|
("ix_prompt_replay_case_results_status", ["status"]),
|
|
):
|
|
op.create_index(index_name, "prompt_replay_case_results", column_names, unique=False)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
if _table_exists(bind, "prompt_replay_case_results"):
|
|
for index_name in (
|
|
"ix_prompt_replay_case_results_status",
|
|
"ix_prompt_replay_case_results_release_id",
|
|
"ix_prompt_replay_case_results_prompt_key",
|
|
"ix_prompt_replay_case_results_golden_case_id",
|
|
"ix_prompt_replay_case_results_run_id",
|
|
"ix_prompt_replay_case_results_tenant_id",
|
|
):
|
|
if _index_exists(bind, "prompt_replay_case_results", index_name):
|
|
op.drop_index(index_name, table_name="prompt_replay_case_results")
|
|
op.drop_table("prompt_replay_case_results")
|
|
if _table_exists(bind, "prompt_replay_runs"):
|
|
for index_name in (
|
|
"ix_prompt_replay_runs_created_by",
|
|
"ix_prompt_replay_runs_run_mode",
|
|
"ix_prompt_replay_runs_run_status",
|
|
"ix_prompt_replay_runs_baseline_release_id",
|
|
"ix_prompt_replay_runs_release_id",
|
|
"ix_prompt_replay_runs_prompt_key",
|
|
"ix_prompt_replay_runs_tenant_id",
|
|
):
|
|
if _index_exists(bind, "prompt_replay_runs", index_name):
|
|
op.drop_index(index_name, table_name="prompt_replay_runs")
|
|
op.drop_table("prompt_replay_runs")
|