Skip to content

feat(rules): add SQL review rules - #1941

Merged
lizhengfeng101 merged 3 commits into
alibaba:mainfrom
mvanhorn:feat/sql-review-rules
Nov 19, 2026
Merged

lizhengfeng101 merged 3 commits into
alibaba:mainfrom
mvanhorn:feat/sql-review-rules

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Description

.sql is already on the review allowlist, but system_rules.json had no SQL mapping, so schema migrations, stored procedures, and hand-written queries all received default.md. This change adds a built-in SQL rule document and maps **/*.sql to it.

internal/config/rules/rule_docs/sql.md is a precision-first, dialect-aware review prompt (PostgreSQL, MySQL, SQLite, SQL Server). It defers formatting to linters such as sqlfluff and covers destructive or irreversible migrations, locking and online-migration hazards, NULL semantics, join/filter correctness, constraints and integrity, non-sargable predicates and SELECT *, and dynamic SQL / SECURITY DEFINER privilege issues.

Only **/*.sql is mapped. The allowlist admits .sql and does not admit .psql, .pgsql, or .ddl; those extensions were not added. The MyBatis mapper XML rule (**/*{mapper,dao}*.xml → mapper_dao_xml.md) is unchanged and still wins for mapper XML.

The **/*.sql entry is last in path_rule_map. It does not overlap a more specific glob. Removing that single mapping makes Resolve fall back to default.md for db/migrate/20260101_add_users.sql and sql/reports/monthly.sql, TestResolveDetail_SystemSQLPatternMatch reports Pattern: default, TestSystemRulesIntegrity/no_orphan_files flags sql.md, and all five review-rules locales fail because they still list **/*.sql. Restoring the mapping makes those tests pass again.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

  • make test passes locally

  • Manual testing (describe below)

  • go test ./internal/config/... — 5 packages ok (allowlist, rules, template, testconnection, toolsconfig)

  • go test ./internal/config/rules -count=1 -run 'TestResolve_DefaultRules|TestResolveDetail_SystemSQLPatternMatch|TestSystemRulesIntegrity|TestReviewRulesGuideLists' — PASS, including db/migrate/20260101_add_users.sql, sql/reports/monthly.sql, SQL/REPORTS/MONTHLY.SQL, **/*.sql allowlisted, and all five locale tables

  • Fail-before (mapping removed in a worktree): the SQL ResolveDetail rows, no_orphan_files, and TestReviewRulesGuideListsEverySystemRulePattern fail with Pattern: default / orphan sql.md / missing glob; restore then PASS

  • make build — PASS (dist/opencodereview)

  • make test — PASS, 2736 --- PASS: lines, 0 --- FAIL:, 26 packages ok

  • make check — PASS (license headers, english-check, go mod tidy, gofmt -s -w, go vet)

  • gofmt -l . — empty

  • go vet ./internal/config/... — PASS

  • Manual: /tmp/ocr-main (upstream d77d70b) rules check on db/migrate/20260101_add_index.sql and sql/reports/monthly.sql prints Pattern: default. /tmp/ocr-sql (6cc1fa1) prints Pattern: **/*.sql and the SQL rule. mapper/UserMapper.xml stays Pattern: **/*{mapper,dao}*.xml on both binaries. ocr review --preview (no model call) lists the three files as will-review and does not print the per-file rule body.

Not verified: a live review that calls a model, CI on this branch, and files with extensions other than .sql.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

AI disclosure: written with AI assistance (Cursor, grok-4.6); the need, scope and review are mine.

Related Issues

No existing issue covers SQL review rules.

Demo

demo

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@Walternater

Copy link
Copy Markdown
Contributor

Verified locally on 6cc1fa1 (macOS arm64, Go 1.26.10):

  • TMPDIR=/private/tmp go test ./internal/config/rules passes, including rule_docs_table_test (all five locale tables match the new **/*.sql mapping).
  • Built the CLI and ran ocr rules check m.sql: resolves to system built-in sql.md, with the dialect-aware prompt text.
  • Mapping is inserted in the documented matching order.

No issues from my side.

AI disclosure: verification drafted with DeepSeek Harness (deepseek-v4.1-flash); commands were run locally.

@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks for the second look on macOS arm64, @Walternater, appreciated.

@mvanhorn

Copy link
Copy Markdown
Contributor Author

Merged the latest main into this branch to clear the conflict in internal/config/rules/system_rules_test.go. Both the new SQL test cases and the Move cases from main are kept. go build, go vet, and go test ./internal/config/... pass on 7743d35.

@wu21-web wu21-web left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Queued to merge.

There are 3 pull requests ahead of this submission.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 55b4b31 into alibaba:main Nov 19, 2026
15 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks for merging this, @lizhengfeng101. Schema migrations and hand-written queries now get a dialect-aware SQL review prompt, with checks for destructive migrations and locking hazards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants