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"