From 71955d37b8a4fc2063472dcfd8ea1372aadd6df2 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Sat, 21 Nov 2026 17:13:04 -0500 Subject: [PATCH] fix(CR-09): set SQLite WAL journal_mode once in init_db instead of per-connection - Move PRAGMA journal_mode=WAL; from get_connection() to init_db() in app/db.py - Add test verifying WAL is enabled by init_db and not re-executed on every connection --- app/db.py | 2 +- tests/test_app.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/db.py b/app/db.py index 72b2404..fed2871 100644 --- a/app/db.py +++ b/app/db.py @@ -20,7 +20,6 @@ def get_connection(db_path: Path | None = None) -> Iterator[sqlite3.Connection]: conn = sqlite3.connect(target, timeout=10.0) conn.row_factory = sqlite3.Row try: - conn.execute("PRAGMA journal_mode=WAL;") conn.execute("PRAGMA foreign_keys=ON;") yield conn conn.commit() @@ -31,6 +30,7 @@ def get_connection(db_path: Path | None = None) -> Iterator[sqlite3.Connection]: def init_db(db_path: Path | None = None) -> None: """Create the appointments table if it does not already exist.""" with get_connection(db_path) as conn: + conn.execute("PRAGMA journal_mode=WAL;") conn.execute( """ CREATE TABLE IF NOT EXISTS appointments ( diff --git a/tests/test_app.py b/tests/test_app.py index 6e43cd7..87bc52a 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -413,3 +413,21 @@ def test_check_slot_availability_invalid_datetime_returns_400(client, monkeypatc }, ) assert resp.status_code == 400 + + +def test_db_init_sets_wal_once_not_on_every_connection(tmp_path: Path): + from app import db + + db_file = tmp_path / "wal_test.db" + db.init_db(db_file) + with db.get_connection(db_file) as conn: + mode = conn.execute("PRAGMA journal_mode;").fetchone()[0] + assert mode.lower() == "wal" + + raw_conn = sqlite3.connect(db_file) + raw_conn.execute("PRAGMA journal_mode=DELETE;") + raw_conn.close() + + with db.get_connection(db_file) as conn: + mode_after = conn.execute("PRAGMA journal_mode;").fetchone()[0] + assert mode_after.lower() == "delete"