ishaq101's picture
feat: persist RAG sources to DB and return them in room detail
7323952
"""Database initialization."""
from sqlalchemy import text
from src.db.postgres.connection import engine, Base
from src.db.postgres.models import Document, Room, ChatMessage, User, MessageSource
async def init_db():
"""Initialize database tables and required extensions."""
async with engine.begin() as conn:
# Create pgvector extension using two separate statements.
# Must NOT be combined into one string — asyncpg rejects multi-statement
# prepared statements (langchain_postgres bug workaround via create_extension=False).
await conn.execute(text("SELECT pg_advisory_xact_lock(1573678846307946496)"))
await conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector"))
# Create application tables
await conn.run_sync(Base.metadata.create_all)
# Schema migrations (idempotent — safe to run on every startup)
await conn.execute(text(
"ALTER TABLE rooms ADD COLUMN IF NOT EXISTS status VARCHAR NOT NULL DEFAULT 'active'"
))