Spaces:
Sleeping
Sleeping
File size: 1,380 Bytes
a4a314c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """
db/database.py — SQLAlchemy async engine and session management
"""
from __future__ import annotations
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase
from utils.core.config import settings
from utils.core.logging_config import get_logger
logger = get_logger(__name__)
engine = create_async_engine(
settings.db_path,
echo=settings.debug,
connect_args={"check_same_thread": False},
)
AsyncSessionLocal = async_sessionmaker(
bind=engine,
class_=AsyncSession,
expire_on_commit=False,
autoflush=False,
autocommit=False,
)
class Base(DeclarativeBase):
pass
async def create_tables():
"""Create all tables if they don't exist."""
from utils.db.models import AnalysisResult, ResumeFile # Ensure models are loaded
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
logger.info("Database tables created/verified")
async def get_db():
"""FastAPI dependency — provides an async DB session."""
async with AsyncSessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
|