migraguard 0.8.3 → 0.9.0
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 +108 -2
- package/cli-contract.yaml +657 -19
- package/dist/cli.js +979 -47
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.js +62 -37
- package/dist/index.js.map +1 -1
- package/docs/cli-reference.md +14 -1
- package/docs/commands.md +1 -0
- package/docs/state-model.md +2 -1
- package/package.json +15 -5
package/README.md
CHANGED
|
@@ -46,7 +46,11 @@ npx migraguard squash
|
|
|
46
46
|
npx migraguard lint && npx migraguard check
|
|
47
47
|
npx migraguard dump
|
|
48
48
|
|
|
49
|
-
#
|
|
49
|
+
# LLM-powered safety audit (optional — requires agent-contracts-runtime + API key)
|
|
50
|
+
npm install --save-dev agent-contracts-runtime
|
|
51
|
+
npx migraguard audit --adapter openai
|
|
52
|
+
|
|
53
|
+
# In PRs, CI runs lint + check (+ optionally verify + audit)
|
|
50
54
|
```
|
|
51
55
|
|
|
52
56
|
## Design Philosophy
|
|
@@ -56,6 +60,7 @@ npx migraguard dump
|
|
|
56
60
|
- **One release = one file**: Migration files are squashed into a single file before release, simplifying error recovery. In DAG mode, independent DDL can be released individually
|
|
57
61
|
- **Parallel releases via dependency tree**: DDL dependencies are analyzed to build a DAG, enabling parallel releases for independent changes
|
|
58
62
|
- **Shift verification left**: Linting, checksum-based tamper detection, and schema dump diffs run at the PR stage
|
|
63
|
+
- **Agent-native**: Domain-specific semantic reasoning is encapsulated inside the toolchain itself. Higher-level agents do not need to know every PostgreSQL lock rule or expand/contract pattern — they invoke migraguard and consume structured findings
|
|
59
64
|
- **Minimal footprint**: Two CLI tools (`psql`, `pg_dump`) and one npm library ([libpg-query](https://github.com/pganalyze/libpg-query)) for the primary PostgreSQL path. MySQL uses `mysql` + `mysqldump` CLIs + [mysql2](https://github.com/sidorares/node-mysql2) (optional peer dep); SQLite uses `sqlite3` CLI + [better-sqlite3](https://github.com/WiseLibs/better-sqlite3) (optional peer dep). No external linter required — lint rules are built in via AST analysis. MySQL/SQLite linting uses [node-sql-parser](https://github.com/taozhi8833998/node-sql-parser) as a parallel, feature-limited engine
|
|
60
65
|
|
|
61
66
|
## Dialect support
|
|
@@ -124,7 +129,8 @@ CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
|
124
129
|
checksum VARCHAR(64) NOT NULL,
|
|
125
130
|
status VARCHAR(16) NOT NULL DEFAULT 'applied', -- applied / failed / skipped
|
|
126
131
|
applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
127
|
-
resolved_at TIMESTAMPTZ
|
|
132
|
+
resolved_at TIMESTAMPTZ, -- resolution timestamp for skipped
|
|
133
|
+
tag VARCHAR(256) -- caller-supplied tag (e.g. commit hash, release tag)
|
|
128
134
|
);
|
|
129
135
|
```
|
|
130
136
|
|
|
@@ -209,6 +215,8 @@ See [docs/expand-contract.md](docs/expand-contract.md) for the complete guide: f
|
|
|
209
215
|
|
|
210
216
|
## Commands
|
|
211
217
|
|
|
218
|
+
### Deterministic Commands
|
|
219
|
+
|
|
212
220
|
| Command | Description |
|
|
213
221
|
|---------|-------------|
|
|
214
222
|
| `new <name>` | Generate a new migration SQL file |
|
|
@@ -217,6 +225,7 @@ See [docs/expand-contract.md](docs/expand-contract.md) for the complete guide: f
|
|
|
217
225
|
| `apply` | Execute pending migrations via native CLI (`psql` / `mysql` / `sqlite3`) |
|
|
218
226
|
| `apply --with-drift-check` | Local: drift check → apply → dump update |
|
|
219
227
|
| `apply --from-baseline` | Apply `schema.sql` baseline, then remaining migrations |
|
|
228
|
+
| `apply --dry-run` | Preview pending migrations without executing |
|
|
220
229
|
| `resolve <file>` | Mark a failed migration as skipped (explicit judgment) |
|
|
221
230
|
| `status` | Display migration status per file |
|
|
222
231
|
| `editable` | List currently editable files (tail / leaf) |
|
|
@@ -233,6 +242,29 @@ See [docs/expand-contract.md](docs/expand-contract.md) for the complete guide: f
|
|
|
233
242
|
| `gate` | Evaluate deployment gate conditions |
|
|
234
243
|
| `baseline` | Squash applied migrations into `schema.sql` |
|
|
235
244
|
|
|
245
|
+
### LLM-Powered Commands
|
|
246
|
+
|
|
247
|
+
| Command | Description |
|
|
248
|
+
|---------|-------------|
|
|
249
|
+
| `audit [target]` | Semantic migration safety audit via LLM |
|
|
250
|
+
| `propose-expand-contract <file>` | Generate expand/contract migration group proposal |
|
|
251
|
+
| `explain` | Explain command output in human-readable form (accepts JSON or text from `lint`, `check`, `diff`, `deps`, `verify` via stdin) |
|
|
252
|
+
|
|
253
|
+
LLM-powered commands are read-only by default. `audit` and `explain` do not modify migration files or database state. `propose-expand-contract` produces a proposal; generated SQL should be reviewed before being written or applied. LLM commands do not replace deterministic gates — they are an additional semantic review layer on top of `lint`, `check`, `diff`, and `verify`.
|
|
254
|
+
|
|
255
|
+
All LLM commands require `agent-contracts-runtime` (optional peer dependency) and an adapter key, and support `--dry-run` to inspect the prompt without calling the LLM.
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
# Audit a migration for operational risks
|
|
259
|
+
npx migraguard audit db/migrations/20260510__add_user_status.sql --adapter openai
|
|
260
|
+
|
|
261
|
+
# Propose expand/contract decomposition for unsafe DDL
|
|
262
|
+
npx migraguard propose-expand-contract db/migrations/20260510__rename_column.sql --adapter cursor
|
|
263
|
+
|
|
264
|
+
# Explain lint output for a PR comment
|
|
265
|
+
npx migraguard lint --format json | npx migraguard explain --adapter openai
|
|
266
|
+
```
|
|
267
|
+
|
|
236
268
|
All commands honor the `dialect` setting. See [Dialect support](#dialect-support) for per-dialect details.
|
|
237
269
|
|
|
238
270
|
See [docs/commands.md](docs/commands.md) for detailed usage, options, and examples.
|
|
@@ -258,6 +290,10 @@ jobs:
|
|
|
258
290
|
- run: npm ci
|
|
259
291
|
- run: npx migraguard lint
|
|
260
292
|
- run: npx migraguard check
|
|
293
|
+
# Optional: LLM semantic audit (requires API key)
|
|
294
|
+
# - run: npx migraguard audit --adapter openai --format json --fail-on error
|
|
295
|
+
# env:
|
|
296
|
+
# OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
|
261
297
|
```
|
|
262
298
|
|
|
263
299
|
### Automatic Apply on Merge
|
|
@@ -549,6 +585,8 @@ migraguard embeds operational policies into the tool and prevents incidents via
|
|
|
549
585
|
| **Parallel releases** | ✅ DAG | ❌ | ❌ | ⚠️ | ❌ |
|
|
550
586
|
| **Offline CI gate** | ✅ check | ❌ | ✅ atlas.sum | ❌ | ❌ |
|
|
551
587
|
| **Failure handling** | DB-recorded, explicit resolve | repair overwrites | manual fix | revert scripts | manual fix |
|
|
588
|
+
| **LLM semantic audit** | ✅ audit, propose-expand-contract, explain | ❌ | ❌ | ❌ | ❌ |
|
|
589
|
+
| **Agent-readable contract** | ✅ cli-contract.yaml + DSL | ❌ | ❌ | ❌ | ❌ |
|
|
552
590
|
| **Execution** | psql / mysql / sqlite3 (plain SQL) | Java / JDBC | Go / DB driver | psql / sqitch | pg (Node.js) |
|
|
553
591
|
|
|
554
592
|
**vs Flyway / Liquibase**: migraguard adds offline CI tamper detection, regression detection, idempotency proof, and apply mutual exclusion. MySQL and SQLite are supported as secondary dialects with full DB runtime commands and 17 generic lint rules. No GUI or rich generic execution engine.
|
|
@@ -559,6 +597,70 @@ migraguard embeds operational policies into the tool and prevents incidents via
|
|
|
559
597
|
|
|
560
598
|
**vs Graphile Migrate**: Graphile optimizes for development speed (current.sql). migraguard preserves iterative development (latest file is freely re-appliable) but adds "squash before release" for production-grade hotfix recovery.
|
|
561
599
|
|
|
600
|
+
## Agent-Native Toolchain
|
|
601
|
+
|
|
602
|
+
migraguard is designed for development workflows where AI agents are first-class participants in implementation, validation, and operations.
|
|
603
|
+
|
|
604
|
+
In traditional developer tooling, toolchains perform deterministic operations such as parsing, linting, validation, and diffing. Semantic judgment is left to humans or to an external agent runtime. This does not scale well because the outer agent must learn and maintain every domain-specific rule about PostgreSQL lock behavior, expand/contract safety, backfill batching, and deployment ordering.
|
|
605
|
+
|
|
606
|
+
migraguard takes a different approach: it encapsulates domain-specific semantic reasoning inside the toolchain itself. In addition to deterministic checks, the toolchain runs LLM-based semantic audits, expand/contract proposals, and command output explanations — returning structured results that humans, CI systems, and AI agents consume in the same way.
|
|
607
|
+
|
|
608
|
+
### Deterministic checks first
|
|
609
|
+
|
|
610
|
+
Anything that can be validated mechanically is validated deterministically: AST-based lint rules (38 for PostgreSQL), checksum-based tamper detection, schema drift comparison, idempotency verification on shadow databases, and expand/contract phase enforcement.
|
|
611
|
+
|
|
612
|
+
### Semantic audit inside the toolchain
|
|
613
|
+
|
|
614
|
+
Domain-specific reasoning that is difficult to express as static rules is handled by LLM-based commands:
|
|
615
|
+
|
|
616
|
+
- **`audit`** — Lock risk assessment under concurrent load, expand/contract necessity, backfill safety (batching, timeouts, resumability), deployment ordering with application releases, validity of `migraguard:allow` directives
|
|
617
|
+
- **`propose-expand-contract`** — Decompose unsafe DDL into phased expand/backfill/switch/contract SQL with deployment gates
|
|
618
|
+
- **`explain`** — Translate machine output into human-readable explanations for PR comments and release decisions
|
|
619
|
+
|
|
620
|
+
### Structured findings
|
|
621
|
+
|
|
622
|
+
LLM output is not free-form text. Results conform to typed schemas such as `MigrationAuditResult`, `ExpandContractProposal`, and `ExplainResult`. Audit-style results are compatible with the common `AgentAuditResult` / `AgentFinding` shape so that higher-level workflow agents can aggregate findings across toolchains.
|
|
623
|
+
|
|
624
|
+
### Tool-owned domain knowledge
|
|
625
|
+
|
|
626
|
+
migraguard owns the rules and reasoning for database migration safety. Instead of embedding PostgreSQL-specific knowledge into a top-level agent prompt, domain expertise is encapsulated inside the tool. Higher-level agents only need to invoke the command and interpret the structured output.
|
|
627
|
+
|
|
628
|
+
### Agent-readable interface
|
|
629
|
+
|
|
630
|
+
Tool capabilities are described in machine-readable form via [cli-contract.yaml](cli-contract.yaml): artifacts read/written, side effects (`database_write`, `database_read`, `file_write`, `network`), risk levels, confirmation requirements, and output schemas.
|
|
631
|
+
|
|
632
|
+
### LLM Adapter Configuration
|
|
633
|
+
|
|
634
|
+
| Adapter | Default Model | Environment Variable |
|
|
635
|
+
|---------|---------------|---------------------|
|
|
636
|
+
| `cursor` | runtime default | `CURSOR_API_KEY` |
|
|
637
|
+
| `openai` | runtime default | `OPENAI_API_KEY` |
|
|
638
|
+
| `gemini` | runtime default | `GEMINI_API_KEY` |
|
|
639
|
+
| `claude` | runtime default | `ANTHROPIC_API_KEY` |
|
|
640
|
+
| `mock` | — | — |
|
|
641
|
+
|
|
642
|
+
Default models are defined by `agent-contracts-runtime` and may change between releases. Use `--model` to pin a specific model.
|
|
643
|
+
|
|
644
|
+
```bash
|
|
645
|
+
# Semantic safety audit
|
|
646
|
+
npx migraguard audit --adapter openai --model gpt-4o
|
|
647
|
+
|
|
648
|
+
# Propose expand/contract decomposition
|
|
649
|
+
npx migraguard propose-expand-contract migration.sql --adapter cursor
|
|
650
|
+
|
|
651
|
+
# Explain lint output for a PR comment
|
|
652
|
+
npx migraguard lint --format json | npx migraguard explain --adapter openai
|
|
653
|
+
|
|
654
|
+
# Inspect the prompt without calling the LLM
|
|
655
|
+
npx migraguard audit --dry-run
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
Install the runtime dependency to enable LLM features:
|
|
659
|
+
|
|
660
|
+
```bash
|
|
661
|
+
npm install agent-contracts-runtime
|
|
662
|
+
```
|
|
663
|
+
|
|
562
664
|
## FAQ
|
|
563
665
|
|
|
564
666
|
### What happens if someone adds a comment to an already-applied migration?
|
|
@@ -601,6 +703,9 @@ No. `verify` creates a temporary shadow DB, applies migrations twice, then drops
|
|
|
601
703
|
| DB state management (SQLite) | [better-sqlite3](https://github.com/WiseLibs/better-sqlite3) (optional peer dep) |
|
|
602
704
|
| SQL lint / parser (PostgreSQL) | [libpg-query](https://github.com/pganalyze/libpg-query) — 38 built-in rules |
|
|
603
705
|
| SQL lint / parser (MySQL, SQLite) | [node-sql-parser](https://github.com/taozhi8833998/node-sql-parser) — 17 generic rules |
|
|
706
|
+
| LLM integration | [agent-contracts-runtime](https://www.npmjs.com/package/agent-contracts-runtime) (optional peer dep) |
|
|
707
|
+
| Agent DSL | [agent-contracts](https://www.npmjs.com/package/agent-contracts) — agent/task/workflow definitions |
|
|
708
|
+
| CLI contract | [cli-contracts](https://www.npmjs.com/package/cli-contracts) — machine-readable interface spec |
|
|
604
709
|
| Package manager | npm |
|
|
605
710
|
|
|
606
711
|
## Detailed Documentation
|
|
@@ -613,3 +718,4 @@ No. `verify` creates a temporary shadow DB, applies migrations twice, then drops
|
|
|
613
718
|
- [docs/safe-ddl.md](docs/safe-ddl.md) — Safe DDL patterns for PostgreSQL (lock timeout, CONCURRENTLY, batch backfills)
|
|
614
719
|
- [docs/expand-contract.md](docs/expand-contract.md) — Expand/contract pattern: phased migrations, state machine, CI/CD deployment gate
|
|
615
720
|
- [docs/typescript-api.md](docs/typescript-api.md) — TypeScript programmatic API: all commands as typed async functions
|
|
721
|
+
- [dsl/](dsl/) — Agent DSL definitions (agent, tasks, workflows, handoff types, guardrails)
|