140 lines
5.9 KiB
Python
140 lines
5.9 KiB
Python
"""add qa sessions and session fields
|
|
|
|
Revision ID: add_qa_sessions_and_turns
|
|
Revises: add_adaptive_rag_tables
|
|
Create Date: 2026-03-02
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = "add_qa_sessions_and_turns"
|
|
down_revision = "add_adaptive_rag_tables"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _table_exists(bind, table_name: str) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
return inspector.has_table(table_name)
|
|
|
|
|
|
def _index_exists(bind, table_name: str, index_name: str) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
indexes = inspector.get_indexes(table_name)
|
|
return any(idx.get("name") == index_name for idx in indexes)
|
|
|
|
|
|
def _column_exists(bind, table_name: str, column_name: str) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
try:
|
|
columns = inspector.get_columns(table_name)
|
|
except Exception:
|
|
return False
|
|
return any(str(col.get("name")) == column_name for col in columns)
|
|
|
|
|
|
def _fk_exists(bind, table_name: str, constrained_columns: list[str]) -> bool:
|
|
inspector = sa.inspect(bind)
|
|
try:
|
|
fks = inspector.get_foreign_keys(table_name)
|
|
except Exception:
|
|
return False
|
|
expected = tuple(constrained_columns)
|
|
for fk in fks:
|
|
cols = tuple(fk.get("constrained_columns") or [])
|
|
if cols == expected:
|
|
return True
|
|
return False
|
|
|
|
|
|
def _create_index_if_not_exists(bind, index_name: str, table_name: str, columns: list[str]) -> None:
|
|
if not _index_exists(bind, table_name, index_name):
|
|
op.create_index(index_name, table_name, columns)
|
|
|
|
|
|
def _drop_index_if_exists(bind, index_name: str, table_name: str) -> None:
|
|
if _index_exists(bind, table_name, index_name):
|
|
op.drop_index(index_name, table_name=table_name)
|
|
|
|
|
|
def upgrade() -> None:
|
|
bind = op.get_bind()
|
|
dialect_name = str(bind.dialect.name or "").lower()
|
|
|
|
if not _table_exists(bind, "qa_sessions"):
|
|
op.create_table(
|
|
"qa_sessions",
|
|
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
|
sa.Column("tenant_id", sa.BigInteger(), nullable=False, server_default="1"),
|
|
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(), onupdate=sa.func.now()),
|
|
sa.Column("user_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("title", sa.String(length=120), nullable=False, server_default="新会话"),
|
|
sa.Column("project_id", sa.BigInteger(), nullable=True),
|
|
sa.Column("started_at", sa.TIMESTAMP(), nullable=True),
|
|
sa.Column("last_message_at", sa.TIMESTAMP(), nullable=True),
|
|
sa.Column("message_count", sa.Integer(), nullable=False, server_default="0"),
|
|
)
|
|
|
|
_create_index_if_not_exists(bind, "ix_qa_sessions_tenant_id", "qa_sessions", ["tenant_id"])
|
|
_create_index_if_not_exists(bind, "ix_qa_sessions_created_at", "qa_sessions", ["created_at"])
|
|
_create_index_if_not_exists(bind, "ix_qa_sessions_user_id", "qa_sessions", ["user_id"])
|
|
_create_index_if_not_exists(bind, "ix_qa_sessions_project_id", "qa_sessions", ["project_id"])
|
|
_create_index_if_not_exists(bind, "ix_qa_sessions_started_at", "qa_sessions", ["started_at"])
|
|
_create_index_if_not_exists(bind, "ix_qa_sessions_last_message_at", "qa_sessions", ["last_message_at"])
|
|
|
|
if _table_exists(bind, "qa_records"):
|
|
if not _column_exists(bind, "qa_records", "session_id"):
|
|
op.add_column("qa_records", sa.Column("session_id", sa.Integer(), nullable=True))
|
|
if not _column_exists(bind, "qa_records", "turn_index"):
|
|
op.add_column(
|
|
"qa_records",
|
|
sa.Column("turn_index", sa.Integer(), nullable=False, server_default="1"),
|
|
)
|
|
_create_index_if_not_exists(bind, "ix_qa_records_session_id", "qa_records", ["session_id"])
|
|
_create_index_if_not_exists(bind, "ix_qa_records_turn_index", "qa_records", ["turn_index"])
|
|
|
|
if (
|
|
dialect_name != "sqlite"
|
|
and _table_exists(bind, "qa_sessions")
|
|
and not _fk_exists(bind, "qa_records", ["session_id"])
|
|
):
|
|
op.create_foreign_key(
|
|
"fk_qa_records_session_id",
|
|
"qa_records",
|
|
"qa_sessions",
|
|
["session_id"],
|
|
["id"],
|
|
ondelete="SET NULL",
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
dialect_name = str(bind.dialect.name or "").lower()
|
|
|
|
if _table_exists(bind, "qa_records"):
|
|
if dialect_name != "sqlite" and _fk_exists(bind, "qa_records", ["session_id"]):
|
|
op.drop_constraint("fk_qa_records_session_id", "qa_records", type_="foreignkey")
|
|
_drop_index_if_exists(bind, "ix_qa_records_turn_index", "qa_records")
|
|
_drop_index_if_exists(bind, "ix_qa_records_session_id", "qa_records")
|
|
if _column_exists(bind, "qa_records", "turn_index") or _column_exists(bind, "qa_records", "session_id"):
|
|
with op.batch_alter_table("qa_records") as batch_op:
|
|
if _column_exists(bind, "qa_records", "turn_index"):
|
|
batch_op.drop_column("turn_index")
|
|
if _column_exists(bind, "qa_records", "session_id"):
|
|
batch_op.drop_column("session_id")
|
|
|
|
if _table_exists(bind, "qa_sessions"):
|
|
_drop_index_if_exists(bind, "ix_qa_sessions_last_message_at", "qa_sessions")
|
|
_drop_index_if_exists(bind, "ix_qa_sessions_started_at", "qa_sessions")
|
|
_drop_index_if_exists(bind, "ix_qa_sessions_project_id", "qa_sessions")
|
|
_drop_index_if_exists(bind, "ix_qa_sessions_user_id", "qa_sessions")
|
|
_drop_index_if_exists(bind, "ix_qa_sessions_created_at", "qa_sessions")
|
|
_drop_index_if_exists(bind, "ix_qa_sessions_tenant_id", "qa_sessions")
|
|
op.drop_table("qa_sessions")
|