dbgov-cli 0.2.2 → 0.2.4
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.
- package/README.md +219 -64
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,111 +1,266 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# dbgov-cli
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
**Governed MySQL & PostgreSQL operations for humans _and_ AI agents.**
|
|
6
|
+
|
|
7
|
+
Run queries, change schemas, and execute DML behind guardrails — every change is measured by `EXPLAIN`, previewed, snapshotted for rollback, and audited, so neither you nor an AI assistant can accidentally nuke a production database.
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/dbgov-cli)
|
|
10
|
+
[](https://github.com/JiangHe12/dbgov-cli/actions/workflows/ci.yml)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+
[](#-trust--verification)
|
|
13
|
+
|
|
14
|
+
[English](README.md) · [简体中文](README_zh.md)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## 🧭 What is this? (read me first)
|
|
4
21
|
|
|
5
|
-
|
|
22
|
+
Touching a production database is one of the scariest things in ops: a missing `WHERE` clause, a careless `DROP COLUMN`, or a schema migration gone wrong can lose data in seconds — usually with no preview, no backup, and no record of what happened. Handing that power to a script or an AI agent is even scarier.
|
|
6
23
|
|
|
7
|
-
|
|
24
|
+
**dbgov-cli wraps every database operation in guardrails.** Think of it as a careful DBA sitting between you and the database:
|
|
8
25
|
|
|
9
|
-
`
|
|
26
|
+
- 📏 **Measures impact before acting** — `explain`, `schema plan`, and `--dry-run` tell you *exactly* how many rows or which columns a change will hit. dbgov never guesses; if it can't measure, it refuses.
|
|
27
|
+
- 🛡️ **Scales the friction to the danger** — a one-row update just needs a confirmation; a no-`WHERE` `DELETE` or a `DROP COLUMN` needs a change ticket *and* an explicit "yes, allow destruction" flag.
|
|
28
|
+
- 📸 **Snapshots schema before every mutation** — so you can roll structure back if something's wrong.
|
|
29
|
+
- 👥 **Honors roles** — readers can't write, writers can't do destructive ops, only admins can.
|
|
30
|
+
- 📜 **Audits everything** — every action (including denied ones) lands in a tamper-evident log.
|
|
31
|
+
- 🤖 **Is safe to hand to an AI agent** — it can read and preview freely, but **cannot** invent the human approvals that destructive changes require.
|
|
10
32
|
|
|
11
|
-
|
|
33
|
+
Works with **MySQL** and **PostgreSQL**.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## ✨ Features
|
|
38
|
+
|
|
39
|
+
| | |
|
|
40
|
+
|---|---|
|
|
41
|
+
| 🗄️ **Two engines** | **MySQL** and **PostgreSQL**, at full parity. `dbgov capabilities` reports what the current build supports. |
|
|
42
|
+
| 🔎 **Read & explain** | `query` (read-only SQL, rejects writes) and `explain` (real execution plan + estimated rows). |
|
|
43
|
+
| 🧱 **Declarative schema** | `schema list / describe / dump / diff / plan / apply` — diff your DB against a desired `.sql` and apply the delta. |
|
|
44
|
+
| ✏️ **Governed DML** | `data exec` runs `UPDATE`/`DELETE`/`INSERT` with `EXPLAIN`-measured blast radius and risk-scaled authorization. |
|
|
45
|
+
| 🔄 **GitOps for schema** | `export` → `import` → `reconcile` (with drift detection + optional `--prune`) → `rollback` from snapshots. |
|
|
46
|
+
| 🚦 **R0–R3 governance** | every operation is risk-classified; protected contexts escalate one tier; AI callers can never self-authorize. |
|
|
47
|
+
| 👥 **RBAC** | per-context `reader` / `writer` / `admin` roles cap the maximum risk a write path can reach. |
|
|
48
|
+
| 📸 **Snapshots & rollback** | automatic pre-change schema snapshot; structure-level restore with explicit data-loss warnings. |
|
|
49
|
+
| 📜 **Tamper-evident audit** | every operation appended to a hash-verifiable log; `audit verify` detects tampering. |
|
|
50
|
+
| 🔏 **Trusted supply chain** | **cosign-signed** binaries, npm **provenance**, and a **SHA-256**-verified installer. |
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 📦 Install
|
|
12
55
|
|
|
13
56
|
```bash
|
|
14
57
|
npm install -g dbgov-cli
|
|
15
|
-
# or
|
|
16
|
-
go install github.com/JiangHe12/dbgov-cli@latest
|
|
17
58
|
```
|
|
18
59
|
|
|
19
|
-
|
|
60
|
+
This installs a tiny launcher; on first run it downloads the right pre-built binary for your OS/arch from the signed [GitHub Release](https://github.com/JiangHe12/dbgov-cli/releases) and **verifies its SHA-256** before use. Requires Node.js ≥ 14 for the installer (the CLI itself is a self-contained Go binary).
|
|
61
|
+
|
|
62
|
+
<details>
|
|
63
|
+
<summary>Other ways to install</summary>
|
|
64
|
+
|
|
65
|
+
- **Direct download** — grab the binary from the [Releases page](https://github.com/JiangHe12/dbgov-cli/releases), verify it against the cosign-signed `checksums.txt`, put it on your `PATH`, and rename it to `dbgov`.
|
|
66
|
+
- **From source** — `go install github.com/JiangHe12/dbgov-cli@latest` (Go 1.22+).
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
dbgov version
|
|
70
|
+
dbgov doctor config -o json # static + read-only diagnostics
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
</details>
|
|
74
|
+
|
|
75
|
+
---
|
|
20
76
|
|
|
21
|
-
##
|
|
77
|
+
## 🚀 Quick start (60 seconds)
|
|
22
78
|
|
|
23
79
|
```bash
|
|
24
|
-
|
|
25
|
-
dbgov
|
|
26
|
-
|
|
27
|
-
dbgov
|
|
28
|
-
|
|
80
|
+
# 1. Point dbgov at your database (stored as a reusable "context"; password via env)
|
|
81
|
+
DBGOV_PASSWORD='***' dbgov ctx set prod --engine mysql \
|
|
82
|
+
--host 127.0.0.1 --port 3306 --database app --username appuser --env prod --protected
|
|
83
|
+
dbgov ctx use prod
|
|
84
|
+
|
|
85
|
+
# 2. Read — read-only SQL is free (R0) and rejects writes
|
|
86
|
+
dbgov query --sql "SELECT id, name FROM users LIMIT 10" -o json
|
|
87
|
+
|
|
88
|
+
# 3. Measure before you change — see the plan & estimated rows
|
|
89
|
+
dbgov explain --sql "UPDATE users SET active = 0 WHERE last_seen < '2025-01-01'" -o json
|
|
90
|
+
|
|
91
|
+
# 4. Preview a DML change (nothing runs yet)
|
|
92
|
+
dbgov data exec --sql "UPDATE users SET active = 0 WHERE id = 42" --dry-run -o json
|
|
93
|
+
|
|
94
|
+
# 5. Apply it — a small, scoped write (R1) just needs your confirmation
|
|
95
|
+
dbgov data exec --sql "UPDATE users SET active = 0 WHERE id = 42" --yes -o json
|
|
96
|
+
|
|
97
|
+
# 6. See what happened
|
|
98
|
+
dbgov audit query --since 1h -o json
|
|
29
99
|
```
|
|
30
100
|
|
|
31
|
-
|
|
101
|
+
> 💡 **Tip:** create production contexts with `--protected`. dbgov then raises every operation one risk tier in that context automatically.
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## 🔐 The governance model (the important part)
|
|
32
106
|
|
|
33
|
-
|
|
107
|
+
Every command is sorted into a **risk tier**. The more dangerous it is, the more explicit human sign-off it needs:
|
|
34
108
|
|
|
35
|
-
|
|
|
36
|
-
|
|
37
|
-
| R0 |
|
|
38
|
-
| R1 |
|
|
39
|
-
| R2 |
|
|
40
|
-
| R3 |
|
|
109
|
+
| Tier | What it covers | What you must provide |
|
|
110
|
+
|:---:|---|---|
|
|
111
|
+
| **R0** | Reads & inspection (`query`, `explain`, `schema list/describe/dump/diff/plan`, `audit`, `doctor`) | Nothing — but it's still audited |
|
|
112
|
+
| **R1** | Small, safe writes (add a column, `data exec` with `WHERE` and small estimated impact) | `--yes` (or interactive confirmation) |
|
|
113
|
+
| **R2** | Elevated writes (`data exec` whose `EXPLAIN` rows exceed the threshold; any R1 in a protected context) | `--yes` **and** a non-empty `--ticket` |
|
|
114
|
+
| **R3** | Destructive operations (drop/modify column, no-`WHERE` `UPDATE`/`DELETE`, prune, destructive rollback) | The above **plus** the precise `--allow-*` flag |
|
|
41
115
|
|
|
42
|
-
|
|
116
|
+
**R3 allow flags** — destruction is never implicit:
|
|
43
117
|
|
|
44
|
-
|
|
118
|
+
| Operation | Required flag |
|
|
119
|
+
|---|---|
|
|
120
|
+
| `schema apply` / `import` dropping or modifying a column | `--allow-destructive` |
|
|
121
|
+
| `data exec` with a no-`WHERE` `UPDATE`/`DELETE` | `--allow-no-where` |
|
|
122
|
+
| `reconcile --prune` dropping tables | `--allow-production-prune` |
|
|
123
|
+
| `rollback --to` that drops columns / tables | `--allow-destructive` / `--allow-production-prune` |
|
|
45
124
|
|
|
46
|
-
|
|
125
|
+
**RBAC** (when roles are configured on a context): `reader` → max R0, `writer` → max R2, `admin` → max R3.
|
|
47
126
|
|
|
48
|
-
|
|
127
|
+
Three rules keep this safe — especially for automation:
|
|
128
|
+
|
|
129
|
+
1. **Impact comes from the database, not a guess.** Use `explain` / `schema plan` / `--dry-run`. dbgov fails closed rather than estimating.
|
|
130
|
+
2. **Mutations are snapshotted first.** Rollback restores *structure* only — dropped row data is never recovered (dbgov warns you loudly).
|
|
131
|
+
3. **🤖 AI agents must never invent `--ticket`, `--allow-*`, or a high-risk `--yes`.** Those are *human* authorization inputs. An agent should surface "this needs approval X" and stop.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## 📚 Command reference
|
|
136
|
+
|
|
137
|
+
`dbgov <command> [flags]`. Add `-o json` for machine-readable output, `--help` on any command for its full flags, and `dbgov capabilities -o json` for the supported engines/features.
|
|
138
|
+
|
|
139
|
+
<details open>
|
|
140
|
+
<summary><b>Read & explain</b></summary>
|
|
49
141
|
|
|
50
142
|
```bash
|
|
51
|
-
dbgov
|
|
52
|
-
dbgov
|
|
53
|
-
dbgov-cli doctor config -o json
|
|
54
|
-
dbgov-cli ctx list -o json
|
|
55
|
-
dbgov-cli ctx export local > local.ctx.yaml
|
|
56
|
-
dbgov-cli ctx import -f local.ctx.yaml --rename local-copy -o json
|
|
57
|
-
dbgov-cli query --sql "SELECT * FROM users" -o json
|
|
58
|
-
dbgov-cli explain --sql "SELECT * FROM users WHERE active = 1" -o json
|
|
59
|
-
dbgov-cli schema dump --dir ./schema -o json
|
|
60
|
-
dbgov-cli schema plan -f desired.sql -o json
|
|
61
|
-
dbgov-cli schema apply -f desired.sql --dry-run -o json
|
|
62
|
-
dbgov-cli data exec --sql "UPDATE users SET active=0 WHERE id=1" --dry-run -o json
|
|
63
|
-
dbgov-cli export --dir ./schema -o json
|
|
64
|
-
dbgov-cli import ./schema --dry-run -o json
|
|
65
|
-
dbgov-cli reconcile ./schema --dry-run -o json
|
|
66
|
-
dbgov-cli rollback list -o json
|
|
67
|
-
dbgov-cli audit query --since 24h -o json
|
|
143
|
+
dbgov query --sql "SELECT ..." -o json # read-only; rejects writes (R0)
|
|
144
|
+
dbgov explain --sql "SELECT ..." -o json # execution plan + estimated rows (R0)
|
|
68
145
|
```
|
|
146
|
+
</details>
|
|
69
147
|
|
|
70
|
-
|
|
148
|
+
<details>
|
|
149
|
+
<summary><b>Schema</b> — inspect, diff & apply DDL</summary>
|
|
71
150
|
|
|
72
|
-
|
|
151
|
+
```bash
|
|
152
|
+
dbgov schema list -o json # R0
|
|
153
|
+
dbgov schema describe <table> -o json # R0
|
|
154
|
+
dbgov schema dump --dir ./schema -o json # R0
|
|
155
|
+
dbgov schema diff -f desired.sql -o json # R0
|
|
156
|
+
dbgov schema plan -f desired.sql -o json # R0 — treat plan risk as authoritative
|
|
157
|
+
dbgov schema apply -f desired.sql --dry-run -o json
|
|
158
|
+
dbgov schema apply -f desired.sql --yes -o json # R1 (incremental)
|
|
159
|
+
dbgov schema apply -f desired.sql --ticket DB-123 --allow-destructive --yes -o json # R3 (destructive)
|
|
160
|
+
```
|
|
73
161
|
|
|
74
|
-
|
|
162
|
+
> Auto-increment columns are modeled as a normalized boolean across both engines; create / diff / apply / snapshot / rollback are preserved, but PostgreSQL `serial`-vs-identity, `ALWAYS`-vs-`BY DEFAULT`, and sequence start/increment options are intentionally **not** preserved.
|
|
163
|
+
</details>
|
|
164
|
+
|
|
165
|
+
<details>
|
|
166
|
+
<summary><b>Governed DML</b> — <code>data exec</code></summary>
|
|
75
167
|
|
|
76
168
|
```bash
|
|
77
|
-
dbgov-
|
|
78
|
-
dbgov
|
|
79
|
-
dbgov
|
|
80
|
-
dbgov
|
|
169
|
+
dbgov data exec --sql "UPDATE ... WHERE ..." --dry-run -o json # preview impact + required authz
|
|
170
|
+
dbgov data exec --sql "UPDATE ... WHERE id = 42" --yes -o json # R1 small impact
|
|
171
|
+
dbgov data exec --sql "UPDATE ... WHERE <wide>" --ticket DB-123 --yes -o json # R2 large impact
|
|
172
|
+
dbgov data exec --sql "DELETE FROM sessions" --ticket DB-123 --allow-no-where --yes -o json # R3
|
|
173
|
+
dbgov data exec -f change.sql --dry-run -o json # read DML from a file
|
|
81
174
|
```
|
|
175
|
+
</details>
|
|
176
|
+
|
|
177
|
+
<details>
|
|
178
|
+
<summary><b>GitOps schema</b> — export · import · reconcile · rollback</summary>
|
|
82
179
|
|
|
83
|
-
|
|
180
|
+
```bash
|
|
181
|
+
dbgov export --dir ./schema -o json # dump current schema to files
|
|
182
|
+
dbgov import ./schema --dry-run -o json
|
|
183
|
+
dbgov import ./schema --yes -o json # R1 / R3 if destructive
|
|
184
|
+
dbgov reconcile ./schema --dry-run -o json # detect drift
|
|
185
|
+
dbgov reconcile ./schema --yes -o json
|
|
186
|
+
dbgov reconcile ./schema --prune --ticket DB-123 --allow-production-prune --yes -o json # R3 prune
|
|
187
|
+
dbgov rollback list -o json # list pre-change snapshots
|
|
188
|
+
dbgov rollback --to <snapshot-id> --dry-run -o json
|
|
189
|
+
dbgov rollback --to <snapshot-id> --ticket DB-123 --yes -o json # structure only; data not recovered
|
|
190
|
+
```
|
|
191
|
+
</details>
|
|
84
192
|
|
|
85
|
-
|
|
193
|
+
<details>
|
|
194
|
+
<summary><b>Contexts, roles, audit & diagnostics</b></summary>
|
|
86
195
|
|
|
87
|
-
|
|
196
|
+
```bash
|
|
197
|
+
# Contexts (MySQL or PostgreSQL)
|
|
198
|
+
dbgov ctx set <name> --engine mysql|postgres --host <h> --port <p> --database <db> --username <u> [--protected]
|
|
199
|
+
dbgov ctx use|list|current|delete
|
|
200
|
+
dbgov ctx export <name> [--include-credentials] -o json
|
|
201
|
+
dbgov ctx import -f ctx.yaml [--rename <new>] [--force] -o json
|
|
202
|
+
dbgov ctx migrate-credentials --to encrypted-file|keychain [--context <name>] -o json
|
|
203
|
+
|
|
204
|
+
# RBAC (write paths only): reader → R0, writer → R2, admin → R3
|
|
205
|
+
dbgov ctx role set <context> --target-operator alice --role writer -o json
|
|
206
|
+
dbgov ctx role list <context> -o json
|
|
207
|
+
|
|
208
|
+
# Audit (tamper-evident; rotated logs only for prune)
|
|
209
|
+
dbgov audit query [--since 24h] [--risk R2] [--limit 50] -o json
|
|
210
|
+
dbgov audit verify -o json
|
|
211
|
+
dbgov audit prune (--before <30d|YYYY-MM-DD> | --keep-last <n>) [--confirm] -o json
|
|
212
|
+
|
|
213
|
+
# Diagnostics & ecosystem
|
|
214
|
+
dbgov doctor config|network|auth -o json
|
|
215
|
+
dbgov capabilities -o json
|
|
216
|
+
dbgov completion bash|zsh|fish|powershell
|
|
217
|
+
dbgov install <agent> --skills # install the dbgov AI skill (claude, codex, …)
|
|
218
|
+
dbgov version
|
|
219
|
+
```
|
|
88
220
|
|
|
89
|
-
|
|
221
|
+
> `audit prune` only deletes **rotated** logs (never the active `audit.log`), defaults to a dry-run, and needs `--confirm` to remove files. Set `DBGOV_OPERATOR` in CI to keep audit/RBAC identity stable.
|
|
222
|
+
</details>
|
|
90
223
|
|
|
91
|
-
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## 🤖 For AI agents
|
|
227
|
+
|
|
228
|
+
- Run `dbgov capabilities -o json` first — it's the authoritative source for supported engines and features.
|
|
229
|
+
- Use `-o json` everywhere; every command returns a stable, versioned envelope.
|
|
230
|
+
- Get blast radius from `explain` / `schema plan` / `--dry-run`, **never** from your own reasoning.
|
|
231
|
+
- **Never self-fill `--ticket`, `--allow-*`, or a high-risk `--yes`.** Surface the required human approval and stop.
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
dbgov install claude --skills # also: codex, opencode, copilot, cursor, windsurf, aider, cc-switch
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## 🔏 Trust & verification
|
|
240
|
+
|
|
241
|
+
- **Signed binaries** — every release artifact is signed with [cosign](https://github.com/sigstore/cosign) (keyless / OIDC); a signed `checksums.txt` covers all platforms.
|
|
242
|
+
- **npm provenance** — published from CI via OpenID Connect with [provenance attestations](https://docs.npmjs.com/generating-provenance-statements) tying the package to this repo and workflow.
|
|
243
|
+
- **Verified installs** — the npm postinstall checks the binary's SHA-256 against the signed `checksums.txt` before installing.
|
|
244
|
+
- **Tamper-evident audit** — `dbgov audit verify` re-walks the log and reports any gap or modification.
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## 🏗️ Build from source & contribute
|
|
92
249
|
|
|
93
250
|
```bash
|
|
251
|
+
git clone https://github.com/JiangHe12/dbgov-cli && cd dbgov-cli
|
|
94
252
|
go build ./...
|
|
95
253
|
go test -count=1 ./...
|
|
96
|
-
gofmt -l main.go cmd internal
|
|
254
|
+
gofmt -l main.go cmd internal # must print nothing
|
|
97
255
|
golangci-lint run --timeout=5m
|
|
98
256
|
```
|
|
99
257
|
|
|
100
|
-
MySQL
|
|
258
|
+
MySQL / PostgreSQL integration tests are opt-in via `DBGOV_TEST_MYSQL_DSN` and `DBGOV_TEST_POSTGRES_DSN`. See [CONTRIBUTING.md](CONTRIBUTING.md) and the security policy in [SECURITY.md](SECURITY.md).
|
|
101
259
|
|
|
102
|
-
|
|
260
|
+
dbgov-cli is built on the shared [`opskit-core`](https://github.com/JiangHe12/opskit-core) governance engine and is part of the **opskit** family of governed CLIs for AI agents — alongside [`cfgov-cli`](https://www.npmjs.com/package/cfgov-cli) (config & Sentinel rules) and `srvgov-cli` (remote servers).
|
|
103
261
|
|
|
104
|
-
|
|
105
|
-
dbgov-cli install claude --skills
|
|
106
|
-
dbgov-cli install codex --skills
|
|
107
|
-
```
|
|
262
|
+
---
|
|
108
263
|
|
|
109
|
-
##
|
|
264
|
+
## 📄 License
|
|
110
265
|
|
|
111
|
-
|
|
266
|
+
[MIT](LICENSE) © JiangHe12
|