- Introduced a new function `get_authenticated_username` to centralize Mini App authentication logic, improving code readability and maintainability. - Updated the duty fetching logic to map unknown event types to "duty" for consistent API responses. - Enhanced the `get_duties` function to include duties starting on the last day of the specified date range. - Improved session management in the database layer to ensure rollback on exceptions. - Added tests to validate the new authentication flow and event type handling.
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
"""SQLAlchemy engine and session factory.
|
|
|
|
Note: Engine and session factory are cached globally per process. Only one
|
|
DATABASE_URL is effectively used for the process lifetime. Using a different
|
|
URL later (e.g. in tests with in-memory SQLite) would still use the first
|
|
engine. To support multiple URLs, cache by database_url (e.g. a dict keyed by URL).
|
|
"""
|
|
|
|
from contextlib import contextmanager
|
|
from typing import Generator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
|
|
_engine = None
|
|
_SessionLocal = None
|
|
|
|
|
|
@contextmanager
|
|
def session_scope(database_url: str) -> Generator[Session, None, None]:
|
|
"""Context manager: yields a session, rolls back on exception, closes on exit."""
|
|
session = get_session(database_url)
|
|
try:
|
|
yield session
|
|
except Exception:
|
|
session.rollback()
|
|
raise
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def get_engine(database_url: str):
|
|
global _engine
|
|
if _engine is None:
|
|
_engine = create_engine(
|
|
database_url,
|
|
connect_args={"check_same_thread": False}
|
|
if "sqlite" in database_url
|
|
else {},
|
|
echo=False,
|
|
)
|
|
return _engine
|
|
|
|
|
|
def get_session_factory(database_url: str) -> sessionmaker[Session]:
|
|
global _SessionLocal
|
|
if _SessionLocal is None:
|
|
engine = get_engine(database_url)
|
|
_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
return _SessionLocal
|
|
|
|
|
|
def get_session(database_url: str) -> Session:
|
|
return get_session_factory(database_url)()
|