Resume-Match-Analyzer / utils /db /database.py
Zayeemk's picture
Rename utils/database.py to utils/db/database.py
78ff2e6 verified
"""
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()