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
This commit is contained in:
kiskaadee committed 2026-11-21 17:13:04 -05:00
1 parent 1005e95bcd
commit 71955d37b8
2 files changed
+19 -1

No files matched your search

+1 -1
View File
@@ -20,7 +20,6 @@ def get_connection(db_path: Path | None = None) -> Iterator[sqlite3.Connection]:
conn = sqlite3.connect(target, timeout=10.0) conn = sqlite3.connect(target, timeout=10.0)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
try: try:
conn.execute("PRAGMA journal_mode=WAL;")
conn.execute("PRAGMA foreign_keys=ON;") conn.execute("PRAGMA foreign_keys=ON;")
yield conn yield conn
conn.commit() 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: def init_db(db_path: Path | None = None) -> None:
"""Create the appointments table if it does not already exist.""" """Create the appointments table if it does not already exist."""
with get_connection(db_path) as conn: with get_connection(db_path) as conn:
conn.execute("PRAGMA journal_mode=WAL;")
conn.execute( conn.execute(
""" """
CREATE TABLE IF NOT EXISTS appointments ( CREATE TABLE IF NOT EXISTS appointments (
+18
View File
@@ -413,3 +413,21 @@ def test_check_slot_availability_invalid_datetime_returns_400(client, monkeypatc
}, },
) )
assert resp.status_code == 400 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"