Jul 9 - Chat - save chat sessions

This commit is contained in:
2026-07-09 13:40:56 -04:00
parent 71aef4fe8e
commit 5cf85c564b
12 changed files with 471 additions and 50 deletions
@@ -0,0 +1,58 @@
"""phase37 — persist AI support-chat conversations
Creates support_chat_sessions + support_chat_messages so customer AI-chat
conversations are saved for later reference and continuity.
Uses table existence checks — safe to re-run.
"""
revision = 'phase37_support_chat'
down_revision = 'phase36_scheduled_insp'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def _table_exists(conn, table):
return conn.execute(sa.text(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES "
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t"
), {"t": table}).scalar() > 0
def upgrade():
bind = op.get_bind()
if not _table_exists(bind, 'support_chat_sessions'):
op.create_table(
'support_chat_sessions',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('customer_id', sa.Integer,
sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('updated_at', sa.DateTime, nullable=False),
)
op.create_index('ix_scs_customer', 'support_chat_sessions', ['customer_id'])
op.create_index('ix_scs_updated', 'support_chat_sessions', ['updated_at'])
if not _table_exists(bind, 'support_chat_messages'):
op.create_table(
'support_chat_messages',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('session_id', sa.Integer,
sa.ForeignKey('support_chat_sessions.id', ondelete='CASCADE'), nullable=False),
sa.Column('role', sa.String(16), nullable=False),
sa.Column('content', sa.Text, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
)
op.create_index('ix_scm_session', 'support_chat_messages', ['session_id'])
def downgrade():
bind = op.get_bind()
if _table_exists(bind, 'support_chat_messages'):
op.drop_table('support_chat_messages')
if _table_exists(bind, 'support_chat_sessions'):
op.drop_table('support_chat_sessions')