feat(ztd-cli): add reusable ddl risk evaluation - #649
Conversation
…-ddl-risk-evaluator # Conflicts: # packages/ztd-cli/tests/diff.unit.test.ts
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR extracts DDL diff risk analysis into reusable evaluators (plan-based and SQL-based), adds a new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant CLI as "ztd ddl risk"
participant FS as "Filesystem"
participant Evaluator as "analyzeMigrationSqlRisks"
participant Output as "Console / JSON"
User->>CLI: run `ztd ddl risk --file tmp/users.diff.sql`
CLI->>FS: read file `tmp/users.diff.sql`
FS-->>CLI: return SQL text
CLI->>Evaluator: analyzeMigrationSqlRisks(sql)
Evaluator-->>CLI: return DdlDiffRisks (destructive/operational)
CLI->>Output: emit JSON envelope or formatted risk lines
Output-->>User: display results / write stdout
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/ztd-cli/src/commands/ddl.ts`:
- Around line 172-178: Change the Commander option declaration for the "risk"
command from a requiredOption to an optional one so JSON payloads can supply the
file before runtime validation; specifically, update the chain that currently
calls requiredOption('--file', ...) to option('--file', ...), leaving the action
handler that constructs merged via parseJsonPayload(... ) and then calls
resolveRequiredProjectPath(merged.file, '--file') unchanged so that
resolveRequiredProjectPath enforces presence after merging JSON-provided values.
In `@packages/ztd-cli/src/commands/ddlRiskEvaluator.ts`:
- Around line 90-94: The current splitter (used to create statements from
normalized) only splits on /;\s*(?:\r?\n|$)/ which misses semicolon-separated
statements on the same line (e.g., "DROP...; CREATE...;"); change the split to
be formatting-independent by replacing that regex with a simple
semicolon+whitespace split such as .split(/;\s*/) when building statements from
normalized, keeping the subsequent .map(...).trim() and .filter(...) logic
intact (update the split call where statements is defined).
- Around line 100-155: The SQL analyzer never detects ADD/DROP CONSTRAINT or
constraint clauses in CREATE TABLEs; update the statement parsing loop to (1)
add regex checks for ALTER TABLE ... ADD CONSTRAINT and ALTER TABLE ... DROP
CONSTRAINT (use the same normalization helpers and call
createGuidedRisk/createDestructiveRisk as appropriate, e.g.,
createDestructiveRisk('drop_constraint',
`${normalizeQualifiedTarget(...)}.${constraintName}`) and
createGuidedRisk('add_constraint', ...)), and (2) when you handle
createTableMatch (the CREATE TABLE branch), inspect the full CREATE TABLE
statement body for inline CONSTRAINT definitions (or presence of ALTER ... ADD
CONSTRAINT in the same migration) and emit the same semantic constraint-change
risk (use the same risk kind used elsewhere, e.g.,
'semantic_constraint_change_effect' or a matching guided/destructive risk) so
rebuiltTables later will include constraint changes; use
normalizeQualifiedTarget and normalizeIdentifier to form targets.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1a2d2d1e-8647-4a04-b077-7caf1ff057b3
⛔ Files ignored due to path filters (1)
packages/ztd-cli/tests/__snapshots__/describe.cli.test.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (13)
.changeset/kind-pets-shout.mddocs/dogfooding/ztd-migration-lifecycle.mddocs/guide/sql-first-end-to-end-tutorial.mddocs/guide/ztd-cli-agent-interface.mdpackages/ztd-cli/src/commands/ddl.tspackages/ztd-cli/src/commands/ddlDiffContracts.tspackages/ztd-cli/src/commands/ddlRiskEvaluator.tspackages/ztd-cli/src/commands/describe.tspackages/ztd-cli/src/commands/diff.tspackages/ztd-cli/tests/cliCommands.test.tspackages/ztd-cli/tests/diff.unit.test.tspackages/ztd-cli/tests/furtherReading.docs.test.tspackages/ztd-cli/tests/sqlFirstTutorial.docs.test.ts
Summary
This PR follows up on the structured
ddl diffrisks work by making the risk evaluator independently reusable and by adding a user-facing CLI entry for post-hoc migration risk analysis.It keeps
ddl diffas the generator-and-review flow, but separates the evaluator responsibility so we can analyze:What changed
analyzeMigrationPlanRisks(plan, summary?)analyzeMigrationSqlRisks(sql)ddl diffto use the extracted evaluatorztd ddl risk --file <migration.sql>so users can re-evaluate hand-edited migration SQL through the same risk contractdescribe commandsupport for the newddl riskentryddl diffgenerates and evaluates from a diffddl riskevaluates migration SQL itself, including hand-edited scriptsWhy
Structured risks improved reviewability, but the evaluator still lived inside
diff.ts, which made it harder to:This PR makes that separation explicit and gives users a discoverable CLI path for post-hoc risk analysis.
Verification
pnpm --filter @rawsql-ts/ztd-cli buildpnpm --filter @rawsql-ts/ztd-cli test -- diff.unit.test.tspnpm --filter @rawsql-ts/ztd-cli test -- cliCommands.test.tspnpm --filter @rawsql-ts/ztd-cli test -- describe.cli.test.ts -upnpm --filter @rawsql-ts/ztd-cli test -- sqlFirstTutorial.docs.test.ts furtherReading.docs.test.tsmain:pnpm --filter @rawsql-ts/ztd-cli test -- diff.unit.test.ts cliCommands.test.ts describe.cli.test.tspnpm --filter @rawsql-ts/ztd-cli test -- furtherReading.docs.test.tsNotes
maininto this branch before opening the PR and resolved the one conflict inpackages/ztd-cli/tests/diff.unit.test.ts.package.jsonmodification in the worktree, but it is not part of this PR.Summary by CodeRabbit
New Features
Documentation
Refactor
Tests