from __future__ import annotations import sqlite3 from contextlib import contextmanager from pathlib import Path @contextmanager def open_db(path: Path, *, check_same_thread: bool = True): path.parent.mkdir(parents=True, exist_ok=True) conn = sqlite3.connect(path, check_same_thread=check_same_thread) conn.execute("PRAGMA journal_mode=WAL") conn.execute("PRAGMA foreign_keys=ON") try: yield conn conn.commit() finally: conn.close()