Spaces:
Running
Running
File size: 1,087 Bytes
14c1acb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | """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()
|