dbctl 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dbctl/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ """dbctl - generic CLI to monitor, control, and administer multiple databases."""
2
+
3
+ from __future__ import annotations
4
+
5
+ __version__ = "0.1.0"
dbctl/__main__.py ADDED
@@ -0,0 +1,6 @@
1
+ from __future__ import annotations
2
+
3
+ from dbctl.cli import main
4
+
5
+ if __name__ == "__main__":
6
+ main()
dbctl/audit.py ADDED
@@ -0,0 +1,74 @@
1
+ """Append-only JSONL audit log at ~/.dbctl/history.jsonl."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import time
7
+ import uuid
8
+ from pathlib import Path
9
+
10
+ from dbctl.config import history_path
11
+
12
+
13
+ def append(
14
+ *,
15
+ profile: str | None,
16
+ connection: str,
17
+ operation: str | None,
18
+ params: dict | None,
19
+ mode: str,
20
+ status: str,
21
+ rows_affected: int | None = None,
22
+ duration_ms: float = 0.0,
23
+ actor: str | None = None,
24
+ redact: set[str] | None = None,
25
+ ) -> str:
26
+ entry = {
27
+ "run_id": uuid.uuid4().hex[:12],
28
+ "ts": time.strftime("%Y-%m-%dT%H:%M:%S%z"),
29
+ "connection": connection,
30
+ "operation": operation,
31
+ "mode": mode,
32
+ "params": _redact(params or {}, redact or set()),
33
+ "status": status,
34
+ "rows_affected": rows_affected,
35
+ "duration_ms": round(duration_ms, 1),
36
+ "actor": actor,
37
+ }
38
+ path: Path = history_path(profile)
39
+ path.parent.mkdir(parents=True, exist_ok=True)
40
+ with path.open("a", encoding="utf-8") as f:
41
+ f.write(json.dumps(entry, default=str) + "\n")
42
+ return entry["run_id"]
43
+
44
+
45
+ def _redact(params: dict, secret_names: set[str]) -> dict:
46
+ return {k: ("***" if k in secret_names else v) for k, v in params.items()}
47
+
48
+
49
+ def read(profile: str | None, *, limit: int = 50) -> list[dict]:
50
+ """Return the last ``limit`` parseable entries, oldest→newest.
51
+
52
+ We parse *every* line and only truncate at the end: a half-written tail
53
+ line (crash mid-append) must not evict a valid older entry from the
54
+ window the way ``lines[-limit:]`` would have.
55
+ """
56
+ path = history_path(profile)
57
+ if not path.exists():
58
+ return []
59
+ entries: list[dict] = []
60
+ for line in path.read_text(encoding="utf-8").splitlines():
61
+ if not line.strip():
62
+ continue
63
+ try:
64
+ entries.append(json.loads(line))
65
+ except json.JSONDecodeError:
66
+ continue
67
+ return entries[-limit:] if limit > 0 else entries
68
+
69
+
70
+ def last_for(profile: str | None, connection: str) -> dict | None:
71
+ for entry in reversed(read(profile, limit=200)):
72
+ if entry.get("connection") == connection and entry.get("operation"):
73
+ return entry
74
+ return None