Spaces:
Running
Running
Download core/storage/sqlite.py from Alialdlfy1/Teleautoadminbot: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/Alialdlfy1/Teleautoadminbot/resolve/main/core/storage/sqlite.py
- Command line
-
hf download hf://spaces/Alialdlfy1/Teleautoadminbot/core/storage/sqlite.py
-
curl -L -o sqlite.py https://huggingface.co/spaces/Alialdlfy1/Teleautoadminbot/resolve/main/core/storage/sqlite.py
1.09 kB
| """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 | |
| ); | |
| ''') | |
| def connect(self): | |
| con = sqlite3.connect(self.path, timeout=30, isolation_level=None) | |
| con.row_factory = sqlite3.Row | |
| try: yield con | |
| finally: con.close() | |