30 lines
745 B
Python
30 lines
745 B
Python
from collections.abc import Generator
|
|
|
|
from app.config import settings
|
|
from sqlalchemy.orm import Session
|
|
|
|
from packages.adapters.sqlalchemy_impl import (
|
|
build_session_factory,
|
|
ensure_database_exists,
|
|
initialize_database,
|
|
)
|
|
|
|
ensure_database_exists(settings.DATABASE_URL)
|
|
engine, SessionLocal = build_session_factory(
|
|
settings.DATABASE_URL,
|
|
pool_size=settings.DATABASE_POOL_SIZE,
|
|
max_overflow=settings.DATABASE_MAX_OVERFLOW,
|
|
pool_timeout=settings.DATABASE_POOL_TIMEOUT,
|
|
pool_recycle=settings.DATABASE_POOL_RECYCLE,
|
|
)
|
|
if settings.AUTO_CREATE_SCHEMA:
|
|
initialize_database(engine)
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|