2026-06-11 01:01:37 +02:00
|
|
|
from collections.abc import Iterator
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
|
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
|
|
|
|
|
|
|
|
|
from app.config import settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2026-07-06 07:14:28 +02:00
|
|
|
# Pool sized above the sync-endpoint threadpool (~40) so a burst of short DB sessions can't starve on
|
|
|
|
|
# checkout. The image proxies deliberately DON'T hold a connection during their slow external fetch
|
|
|
|
|
# (see routes/plex.py), so this headroom is only for the many brief sessions, not long ones.
|
|
|
|
|
engine = create_engine(
|
|
|
|
|
settings.database_url,
|
|
|
|
|
pool_pre_ping=True,
|
|
|
|
|
pool_size=20,
|
|
|
|
|
max_overflow=30,
|
|
|
|
|
pool_timeout=30,
|
|
|
|
|
future=True,
|
|
|
|
|
)
|
2026-06-11 01:01:37 +02:00
|
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_db() -> Iterator[Session]:
|
|
|
|
|
db = SessionLocal()
|
|
|
|
|
try:
|
|
|
|
|
yield db
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|