Harden SQLite connection defaults with WAL and busy timeout

This commit is contained in:
2026-04-10 19:24:21 +02:00
parent 1dfc17f4f5
commit 9b4cd17e3b
3 changed files with 68 additions and 5 deletions

View File

@@ -106,6 +106,13 @@ _SCHEMA_STATEMENTS: list[str] = [
# ---------------------------------------------------------------------------
async def _configure_connection(db: aiosqlite.Connection) -> None:
"""Apply hardening pragmas to a newly-opened SQLite connection."""
await db.execute("PRAGMA journal_mode=WAL;")
await db.execute("PRAGMA foreign_keys=ON;")
await db.execute("PRAGMA busy_timeout=5000;")
async def init_db(db: aiosqlite.Connection) -> None:
"""Create all BanGUI application tables if they do not already exist.
@@ -117,10 +124,7 @@ async def init_db(db: aiosqlite.Connection) -> None:
db: An open :class:`aiosqlite.Connection` to the application database.
"""
log.info("initialising_database_schema")
async with db.execute("PRAGMA journal_mode=WAL;"):
pass
async with db.execute("PRAGMA foreign_keys=ON;"):
pass
await _configure_connection(db)
for statement in _SCHEMA_STATEMENTS:
await db.executescript(statement)
await db.commit()
@@ -138,5 +142,5 @@ async def open_db(database_path: str) -> aiosqlite.Connection:
"""
db = await aiosqlite.connect(database_path)
db.row_factory = aiosqlite.Row
await db.execute("PRAGMA foreign_keys=ON;")
await _configure_connection(db)
return db