PMFlow/backend/alembic/versions/20260503_add_pmflow_ai_feed...

121 lines
4.1 KiB
Python

"""add pmflow ai feedback events
Revision ID: 20260503_add_pmflow_ai_feedback_events
Revises: 20260501_add_pmflow_launch_draft_jobs
Create Date: 2026-05-03
"""
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 = "20260503_add_pmflow_ai_feedback_events"
down_revision = "20260501_add_pmflow_launch_draft_jobs"
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, "pmflow_ai_feedback_events"):
return
json_type = _json_type(bind)
op.create_table(
"pmflow_ai_feedback_events",
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("project_id", sa.BigInteger(), sa.ForeignKey("projects.id"), nullable=False),
sa.Column("thread_id", sa.BigInteger(), sa.ForeignKey("pmflow_threads.id"), nullable=True),
sa.Column("message_id", sa.BigInteger(), sa.ForeignKey("pmflow_thread_messages.id"), nullable=True),
sa.Column("change_set_id", sa.BigInteger(), sa.ForeignKey("pmflow_change_sets.id"), nullable=True),
sa.Column("ai_call_log_id", sa.BigInteger(), nullable=True),
sa.Column("actor_user_id", sa.BigInteger(), sa.ForeignKey("users.id"), nullable=False),
sa.Column("target_type", sa.String(length=64), nullable=False),
sa.Column("target_id", sa.String(length=128), nullable=False),
sa.Column("answer_kind", sa.String(length=64), nullable=False, server_default="chat_answer"),
sa.Column("rating", sa.String(length=16), nullable=False),
sa.Column("reason_tags", json_type, nullable=False),
sa.Column("comment", sa.Text(), nullable=True),
sa.Column("scene_key", sa.String(length=128), nullable=True),
sa.Column("prompt_key", sa.String(length=128), nullable=True),
sa.Column("prompt_release_id", sa.Integer(), nullable=True),
sa.Column("runtime_profile_id", sa.String(length=128), nullable=True),
sa.Column("model_provider", sa.String(length=64), nullable=True),
sa.Column("model_name", sa.String(length=120), nullable=True),
sa.Column("feedback_metadata", json_type, nullable=False),
sa.UniqueConstraint(
"tenant_id",
"project_id",
"actor_user_id",
"target_type",
"target_id",
name="uq_pmflow_ai_feedback_target_user",
),
)
for column_name in (
"tenant_id",
"project_id",
"thread_id",
"message_id",
"change_set_id",
"ai_call_log_id",
"actor_user_id",
"target_type",
"target_id",
"answer_kind",
"rating",
"scene_key",
"prompt_key",
"prompt_release_id",
"runtime_profile_id",
):
op.create_index(
f"ix_pmflow_ai_feedback_events_{column_name}",
"pmflow_ai_feedback_events",
[column_name],
unique=False,
)
def downgrade() -> None:
bind = op.get_bind()
if not _table_exists(bind, "pmflow_ai_feedback_events"):
return
for column_name in (
"runtime_profile_id",
"prompt_release_id",
"prompt_key",
"scene_key",
"rating",
"answer_kind",
"target_id",
"target_type",
"actor_user_id",
"ai_call_log_id",
"change_set_id",
"message_id",
"thread_id",
"project_id",
"tenant_id",
):
op.drop_index(f"ix_pmflow_ai_feedback_events_{column_name}", table_name="pmflow_ai_feedback_events")
op.drop_table("pmflow_ai_feedback_events")