InferRoute / inferroute /database.py
Ypeng12's picture
Fix SQLite, OpenAI mock, circuit breaker fallback for pinned models, cache isolation, prefix cache answers, docs relative paths, and integration tests
0a1d5dd
Raw
History Blame Contribute Delete
1.21 kB
from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from inferroute.config import settings
from inferroute.models import Base
# Create async engine with parameters appropriate for the database backend
engine_args = {"echo": False}
if "sqlite" not in settings.DATABASE_URL:
engine_args["pool_size"] = 10
engine_args["max_overflow"] = 20
engine = create_async_engine(
settings.DATABASE_URL,
**engine_args
)
# Async session factory
async_session = async_sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False
)
async def init_db() -> None:
"""Initialize database tables."""
async with engine.begin() as conn:
# For development ease, we auto-create the schema
await conn.run_sync(Base.metadata.create_all)
async def get_db() -> AsyncGenerator[AsyncSession, None]:
"""Dependency for getting database sessions."""
async with async_session() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()