Download app/db/session.py from Prabu17/askmydocs: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/Prabu17/askmydocs/resolve/main/app/db/session.py
- Command line
-
hf download hf://spaces/Prabu17/askmydocs/app/db/session.py
-
curl -L -o session.py https://huggingface.co/spaces/Prabu17/askmydocs/resolve/main/app/db/session.py
1.22 kB
| """Database engine, session factory and initialisation helpers.""" | |
| from __future__ import annotations | |
| from collections.abc import Iterator | |
| from contextlib import contextmanager | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import Session, sessionmaker | |
| from app.config import get_settings | |
| from app.db.models import Base | |
| settings = get_settings() | |
| engine = create_engine(settings.database_url, pool_pre_ping=True, future=True) | |
| SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, expire_on_commit=False) | |
| def init_db() -> None: | |
| """Create all tables. Callable from the init script and tests.""" | |
| Base.metadata.create_all(bind=engine) | |
| def session_scope() -> Iterator[Session]: | |
| """Context-managed database session (commits or rolls back).""" | |
| session = SessionLocal() | |
| try: | |
| yield session | |
| session.commit() | |
| except Exception: | |
| session.rollback() | |
| raise | |
| finally: | |
| session.close() | |
| def get_db_dependency(): | |
| """FastAPI dependency that yields a database session.""" | |
| session = SessionLocal() | |
| try: | |
| yield session | |
| finally: | |
| session.close() |