Initial commit with webpage
This commit is contained in:
commit
26875de1dd
55 files changed
+5726
No files matched your search
@@ -0,0 +1,49 @@
|
||||
"""Database engine and sessions."""
|
||||
|
||||
from collections.abc import Iterator
|
||||
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from .config import get_settings
|
||||
|
||||
|
||||
def make_engine(url: str) -> Engine:
|
||||
if url.startswith("sqlite"):
|
||||
engine = create_engine(url, connect_args={"check_same_thread": False})
|
||||
|
||||
@event.listens_for(engine, "connect")
|
||||
def _sqlite_pragmas(dbapi_conn, _record): # noqa: ANN001
|
||||
cur = dbapi_conn.cursor()
|
||||
cur.execute("PRAGMA foreign_keys=ON")
|
||||
cur.execute("PRAGMA journal_mode=WAL")
|
||||
cur.close()
|
||||
|
||||
return engine
|
||||
|
||||
return create_engine(
|
||||
url,
|
||||
# Managed Postgres drops idle connections (Neon scales to zero, poolers
|
||||
# time out). Test each connection before use instead of failing a request.
|
||||
pool_pre_ping=True,
|
||||
pool_size=5,
|
||||
max_overflow=5,
|
||||
pool_recycle=300,
|
||||
# Prepared statements break behind PgBouncer-style transaction poolers
|
||||
# (Supabase port 6543, Neon "-pooler" hosts). This app is tiny; skip them.
|
||||
connect_args={"prepare_threshold": None, "connect_timeout": 10},
|
||||
)
|
||||
|
||||
|
||||
engine = make_engine(get_settings().database_url)
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
||||
|
||||
|
||||
def get_db() -> Iterator[Session]:
|
||||
"""FastAPI dependency: one session per request, always closed."""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in new issue
Block a user