Spaces:
Sleeping
Sleeping
Download src/adapters/persistence/skill_sqlite.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/adapters/persistence/skill_sqlite.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/adapters/persistence/skill_sqlite.py
-
curl -L -o skill_sqlite.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/adapters/persistence/skill_sqlite.py
3.24 kB
| """Local persistence adapter. Replace this port for the selected domestic DB.""" | |
| import json | |
| import sqlite3 | |
| import time | |
| from contextlib import contextmanager | |
| from pathlib import Path | |
| from src.modules.skill.domain.models import SkillError, canonical, digest | |
| class SqliteTransaction: | |
| def __init__(self, db): | |
| self.db = db | |
| def document(row): | |
| return {"key": row["doc_key"], "revision": row["revision"], "digest": row["digest"], "body": json.loads(row["body"])} if row else None | |
| def get(self, tenant, kind, key): | |
| return self.document(self.db.execute("SELECT * FROM skill_documents WHERE tenant=? AND kind=? AND doc_key=?", (tenant, kind, key)).fetchone()) | |
| def listing(self, tenant, kind): | |
| return [self.document(r) for r in self.db.execute("SELECT * FROM skill_documents WHERE tenant=? AND kind=? ORDER BY doc_key", (tenant, kind))] | |
| def create(self, tenant, kind, key, body): | |
| try: | |
| self.db.execute("INSERT INTO skill_documents VALUES(?,?,?,?,?,?)", (tenant, kind, key, 1, canonical(body), digest(body))) | |
| except sqlite3.IntegrityError: | |
| raise SkillError("VERSION_EXISTS", "对象或不可变版本已存在。", 409) from None | |
| return self.get(tenant, kind, key) | |
| def update(self, tenant, kind, key, body, expected_revision): | |
| if kind != "draft": | |
| raise SkillError("IMMUTABLE_VERSION", "版本对象不能原地更新。", 409) | |
| changed = self.db.execute("UPDATE skill_documents SET body=?,digest=?,revision=revision+1 WHERE tenant=? AND kind=? AND doc_key=? AND revision=?", | |
| (canonical(body), digest(body), tenant, kind, key, expected_revision)).rowcount | |
| if changed != 1: | |
| raise SkillError("REVISION_CONFLICT", "草稿已改变,请重新读取后编辑。", 409) | |
| return self.get(tenant, kind, key) | |
| def audit(self, tenant, user, action, target, target_digest): | |
| self.db.execute("INSERT INTO skill_audit(timestamp,tenant,user_id,action,target,digest) VALUES(?,?,?,?,?,?)", (time.time(), tenant, user, action, target, target_digest)) | |
| class SqliteSkillRepository: | |
| def __init__(self, path): | |
| self.path = Path(path) | |
| self.path.parent.mkdir(parents=True, exist_ok=True) | |
| with self.transaction(write=True) as tx: | |
| tx.db.execute("CREATE TABLE IF NOT EXISTS skill_documents(tenant TEXT NOT NULL,kind TEXT NOT NULL,doc_key TEXT NOT NULL,revision INTEGER NOT NULL,body TEXT NOT NULL,digest TEXT NOT NULL,PRIMARY KEY(tenant,kind,doc_key))") | |
| tx.db.execute("CREATE TABLE IF NOT EXISTS skill_audit(id INTEGER PRIMARY KEY,timestamp REAL NOT NULL,tenant TEXT NOT NULL,user_id TEXT NOT NULL,action TEXT NOT NULL,target TEXT NOT NULL,digest TEXT NOT NULL)") | |
| def transaction(self, *, write=False): | |
| db = sqlite3.connect(self.path, timeout=30, isolation_level=None) | |
| db.row_factory = sqlite3.Row | |
| try: | |
| db.execute("BEGIN IMMEDIATE" if write else "BEGIN") | |
| yield SqliteTransaction(db) | |
| db.commit() | |
| except BaseException: | |
| db.rollback() | |
| raise | |
| finally: | |
| db.close() | |