"""add conversation sessions table Revision ID: add_conversation_sessions_table Revises: add_kb_chunk_ivfflat_index Create Date: 2026-03-11 """ from __future__ import annotations from alembic import op import sqlalchemy as sa revision = "add_conversation_sessions_table" down_revision = "add_kb_chunk_ivfflat_index" branch_labels = None depends_on = None 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: inspector = sa.inspect(bind) if not inspector.has_table(table_name): return False return any(idx.get("name") == index_name for idx in inspector.get_indexes(table_name)) def _create_index_if_missing(bind, table_name: str, index_name: str, columns: list[str], *, unique: bool = False) -> None: if not _index_exists(bind, table_name, index_name): op.create_index(index_name, table_name, columns, unique=unique) def _drop_index_if_exists(bind, table_name: str, index_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() if not _table_exists(bind, "conversation_sessions"): op.create_table( "conversation_sessions", sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), sa.Column("tenant_id", sa.BigInteger(), nullable=False, server_default="1"), sa.Column("sender", sa.String(length=128), nullable=False), sa.Column("current_project_id", sa.BigInteger(), sa.ForeignKey("projects.id"), nullable=True), sa.Column("last_intent", sa.String(length=64), nullable=True), sa.Column("last_message_text", sa.String(length=200), nullable=True), sa.Column("last_active_at", sa.TIMESTAMP(), nullable=False, server_default=sa.func.now()), 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.UniqueConstraint("tenant_id", "sender", name="uq_conversation_sessions_tenant_sender"), ) _create_index_if_missing(bind, "conversation_sessions", "ix_conversation_sessions_tenant_id", ["tenant_id"]) _create_index_if_missing(bind, "conversation_sessions", "ix_conversation_sessions_sender", ["sender"]) _create_index_if_missing(bind, "conversation_sessions", "ix_conversation_sessions_current_project_id", ["current_project_id"]) _create_index_if_missing(bind, "conversation_sessions", "ix_conversation_sessions_last_intent", ["last_intent"]) _create_index_if_missing(bind, "conversation_sessions", "ix_conversation_sessions_last_active_at", ["last_active_at"]) _create_index_if_missing( bind, "conversation_sessions", "ix_conversation_sessions_tenant_sender_last_active", ["tenant_id", "sender", "last_active_at"], ) def downgrade() -> None: bind = op.get_bind() if not _table_exists(bind, "conversation_sessions"): return _drop_index_if_exists(bind, "conversation_sessions", "ix_conversation_sessions_tenant_sender_last_active") _drop_index_if_exists(bind, "conversation_sessions", "ix_conversation_sessions_last_active_at") _drop_index_if_exists(bind, "conversation_sessions", "ix_conversation_sessions_last_intent") _drop_index_if_exists(bind, "conversation_sessions", "ix_conversation_sessions_current_project_id") _drop_index_if_exists(bind, "conversation_sessions", "ix_conversation_sessions_sender") _drop_index_if_exists(bind, "conversation_sessions", "ix_conversation_sessions_tenant_id") op.drop_table("conversation_sessions")