ai-interview-coach / app /database.py
LaelaZ's picture
Deploy InterviewCoach to HF Spaces (Docker)
473a23b verified
raw
history blame contribute delete
990 Bytes
"""SQLAlchemy engine, session factory, and a FastAPI dependency."""
from __future__ import annotations
from typing import Iterator
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, declarative_base, sessionmaker
from app.config import get_settings
_settings = get_settings()
# check_same_thread is only meaningful for SQLite; harmless otherwise.
_connect_args = {"check_same_thread": False} if _settings.database_url.startswith("sqlite") else {}
engine = create_engine(_settings.database_url, connect_args=_connect_args, future=True)
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, future=True)
Base = declarative_base()
def init_db() -> None:
"""Create tables if they don't exist. Idempotent."""
from app import models # noqa: F401 (register models on Base)
Base.metadata.create_all(bind=engine)
def get_db() -> Iterator[Session]:
db = SessionLocal()
try:
yield db
finally:
db.close()