Fix startup runtime settings ordering and use effective database path for request DB connections

This commit is contained in:
2026-04-12 20:02:40 +02:00
parent ee880e6296
commit e6df045e5e
3 changed files with 33 additions and 133 deletions

View File

@@ -97,22 +97,31 @@ async def get_app_context(request: Request) -> ApplicationContext:
return _build_app_context(request)
async def get_db(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> AsyncGenerator[aiosqlite.Connection, None]:
async def get_settings(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> Settings:
"""Provide the effective application settings for the current request."""
return app_context.runtime_settings if app_context.runtime_settings is not None else app_context.settings
async def get_db(
settings: Annotated[Settings, Depends(get_settings)],
) -> AsyncGenerator[aiosqlite.Connection, None]:
"""Provide a request-scoped :class:`aiosqlite.Connection` for the current request.
Opens a fresh connection for every request and closes it when the request
is finished. This avoids contention and locking issues from a single shared
SQLite connection across concurrent requests.
The database path is taken from the effective application settings so
runtime overrides stored during setup are respected.
Args:
app_context: The injected shared application context.
settings: The effective application settings for the current request.
Yields:
An open :class:`aiosqlite.Connection` for the request.
"""
from app.db import open_db # noqa: PLC0415
settings = app_context.settings
try:
db = await open_db(settings.database_path)
except Exception as exc:
@@ -128,11 +137,6 @@ async def get_db(app_context: Annotated[ApplicationContext, Depends(get_app_cont
await db.close()
async def get_settings(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> Settings:
"""Provide the effective application settings for the current request."""
return app_context.runtime_settings if app_context.runtime_settings is not None else app_context.settings
async def get_http_session(app_context: Annotated[ApplicationContext, Depends(get_app_context)]) -> aiohttp.ClientSession:
"""Provide the shared HTTP client session from application context.