"""Small SQLite foundation for durable state and future migration to PostgreSQL.""" import os, sqlite3, threading from contextlib import contextmanager class SQLiteStore: def __init__(self, path: str): self.path = path self._lock = threading.RLock() directory = os.path.dirname(os.path.abspath(path)) os.makedirs(directory, exist_ok=True) with self.connect() as con: con.executescript(''' PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL; PRAGMA foreign_keys=ON; CREATE TABLE IF NOT EXISTS schema_meta(key TEXT PRIMARY KEY, value TEXT NOT NULL); CREATE TABLE IF NOT EXISTS audit_log( id INTEGER PRIMARY KEY AUTOINCREMENT, ts TEXT NOT NULL, actor TEXT, action TEXT NOT NULL, target TEXT, details TEXT ); ''') @contextmanager def connect(self): con = sqlite3.connect(self.path, timeout=30, isolation_level=None) con.row_factory = sqlite3.Row try: yield con finally: con.close()