Peterase's picture
feat(rag): implement hybrid search with live sources and production-grade intent classification
a63c61f
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from src.core.config import settings
from src.core.domain.db_models import Base
engine = create_engine(
settings.SQLALCHEMY_DATABASE_URI,
pool_recycle=300,
pool_pre_ping=True
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def init_db():
Base.metadata.create_all(bind=engine)
_run_migrations()
def _run_migrations():
"""Apply any missing schema changes that create_all won't handle (existing tables)."""
migrations = [
"ALTER TABLE chat_history ADD COLUMN IF NOT EXISTS pinned BOOLEAN DEFAULT FALSE",
"ALTER TABLE chat_history ADD COLUMN IF NOT EXISTS user_id INTEGER REFERENCES users(id) ON DELETE SET NULL",
]
with engine.connect() as conn:
for sql in migrations:
try:
conn.execute(text(sql))
except Exception:
pass # Column may already exist or constraint may differ — safe to ignore
conn.commit()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()