28 lines
623 B
Python
28 lines
623 B
Python
from collections.abc import Iterator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
|
|
|
from app.config import settings
|
|
|
|
engine = create_engine(
|
|
settings.database_url,
|
|
pool_pre_ping=True, # tote Verbindungen nach DB-Neustart erkennen
|
|
pool_recycle=1800, # unter MariaDBs wait_timeout bleiben
|
|
future=True,
|
|
)
|
|
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def get_db() -> Iterator[Session]:
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|