import os import sqlite3 _SCHEMA_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "schema.sql") _db_path: str | None = None def init_db(database_path: str) -> None: global _db_path _db_path = database_path os.makedirs(os.path.dirname(database_path), exist_ok=True) with get_conn() as conn, open(_SCHEMA_PATH) as f: conn.executescript(f.read()) def get_conn() -> sqlite3.Connection: if _db_path is None: raise RuntimeError("init_db() must be called before get_conn()") conn = sqlite3.connect(_db_path) conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys = ON") return conn