Spaces:
Running
Running
Download intel/db.py from Success-dev/venthrive-api: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/spaces/Success-dev/venthrive-api/resolve/main/intel/db.py
- Command line
-
hf download hf://spaces/Success-dev/venthrive-api/intel/db.py
-
curl -L -o db.py https://huggingface.co/spaces/Success-dev/venthrive-api/resolve/main/intel/db.py
1.55 kB
| """ | |
| Connection to the intel store. Phase 1 runs on a local SQLite file (ADR-0020); | |
| the schema in backend/sql/intel_schema.sql is portable so the same tables move | |
| to Postgres/Supabase later. | |
| INTEL_DB_PATH selects the file. When it's unset the store is "off": the CLI | |
| refuses to run and hooks from other systems (the news pipeline's event writer) | |
| silently do nothing — so the deployed backend, which has no intel store yet, | |
| behaves exactly as before. | |
| """ | |
| import os | |
| import sqlite3 | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| SCHEMA_PATH = Path(__file__).resolve().parent.parent / "sql" / "intel_schema.sql" | |
| def db_path() -> str | None: | |
| return os.environ.get("INTEL_DB_PATH") or None | |
| def enabled() -> bool: | |
| return db_path() is not None | |
| def connect(path: str | None = None) -> sqlite3.Connection: | |
| path = path or db_path() | |
| if not path: | |
| raise RuntimeError("INTEL_DB_PATH is not set — the intel store is off (see backend/intel/README.md).") | |
| if path != ":memory:": | |
| Path(path).parent.mkdir(parents=True, exist_ok=True) | |
| con = sqlite3.connect(path) | |
| con.row_factory = sqlite3.Row | |
| con.execute("pragma foreign_keys = on") | |
| if path != ":memory:": | |
| con.execute("pragma journal_mode = wal") | |
| return con | |
| def init(con: sqlite3.Connection) -> None: | |
| con.executescript(SCHEMA_PATH.read_text(encoding="utf8")) | |
| from intel.registry import seed | |
| seed(con) | |
| con.commit() | |
| def now_iso() -> str: | |
| return datetime.now(timezone.utc).isoformat(timespec="seconds") | |