@culpritsql/ci 0.0.0-stage → 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cristian Pufu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,250 @@
1
- # Temporary Holding Version
1
+ <div align="center">
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/culpritsql/culprit-ci/main/assets/hero-dark.png">
5
+ <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/culpritsql/culprit-ci/main/assets/hero-light.png">
6
+ <img alt="Culprit SQL CI: SQL migration linter, execution plan regression gate, and SARIF 2.1.0 GitHub Action" src="https://raw.githubusercontent.com/culpritsql/culprit-ci/main/assets/hero-light.png" width="100%">
7
+ </picture>
8
+
9
+ <br />
10
+
11
+ [![npm version](https://img.shields.io/npm/v/@culpritsql/ci?style=flat-square&color=ffcf33&labelColor=181715)](https://www.npmjs.com/package/@culpritsql/ci)
12
+ [![GitHub Action](https://img.shields.io/badge/GitHub_Action-v0.5.6-4cbb84?style=flat-square&labelColor=181715)](https://github.com/culpritsql/culprit-ci)
13
+ [![SARIF 2.1.0](https://img.shields.io/badge/SARIF-2.1.0-6cb6ff?style=flat-square&labelColor=181715)](https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning)
14
+ [![Wasm Engine](https://img.shields.io/badge/Wasm-@culpritsql/plans-ffcf33?style=flat-square&labelColor=181715)](https://www.npmjs.com/package/@culpritsql/plans)
15
+ [![License: MIT](https://img.shields.io/badge/License-MIT-9e9b94?style=flat-square&labelColor=181715)](./LICENSE)
16
+
17
+ **Catch lock-amplifying SQL migrations, unindexed foreign keys, non-sargable queries, and execution plan regressions before they reach production.**
18
+
19
+ [Culprit Explain](https://explain.culpritsql.com) · [GitHub Action Quickstart](#github-action-quickstart) · [CLI Usage](#cli-quickstart) · [Rule Reference](#rule-reference) · [Programmatic API](#programmatic-api)
20
+
21
+ </div>
22
+
23
+ ---
24
+
25
+ ## Why `@culpritsql/ci`?
26
+
27
+ - **Lexer-Accurate SQL Migration & Query Linter:** Tracks `--` line comments, nested `/* ... */` block comments, `'...'` literals, and `$tag$...$tag$` dollar-quoted PostgreSQL bodies with exact 1-based line and column spans.
28
+ - **Zero Same-File False Positives:** Tracks `CREATE TABLE <name>` inside each migration file so indexing or altering a brand-new empty table never triggers false blocking-DDL alarms (`MIG001`, `MIG003`, `MIG006`).
29
+ - **Local WebAssembly Execution Plan Gate:** Powered by [`@culpritsql/plans`](https://www.npmjs.com/package/@culpritsql/plans) (Rust compiled to WebAssembly). Parses and structurally diffs PostgreSQL, SQL Server (`.sqlplan`), MySQL, MariaDB, YugabyteDB, and SQLite plans locally with zero network calls.
30
+ - **One-Click Redacted Diff Links:** Generates client-side `https://explain.culpritsql.com/#s=z1...` deep links (`deflate-raw` + base64url) with SQL string and numeric literals scrubbed automatically.
31
+ - **Native GitHub PR Workflow:** Emits valid **SARIF 2.1.0** for inline PR diff annotations via `upload-sarif@v3` and maintains a single sticky Pull Request summary comment.
32
+
33
+ ---
34
+
35
+ ## GitHub Action Quickstart
36
+
37
+ Add `.github/workflows/sql-ci.yml` to lint migrations, compare baseline vs candidate plans, upload inline SARIF annotations, and post a sticky Pull Request report:
38
+
39
+ ```yaml
40
+ name: SQL CI
41
+
42
+ on:
43
+ pull_request:
44
+ paths:
45
+ - "migrations/**"
46
+ - "src/**/*.sql"
47
+ - "plans/**"
48
+
49
+ permissions:
50
+ contents: read
51
+ pull-requests: write
52
+ security-events: write
53
+
54
+ jobs:
55
+ culprit:
56
+ runs-on: ubuntu-latest
57
+ steps:
58
+ - uses: actions/checkout@v4
59
+
60
+ - name: Run Culprit SQL CI
61
+ id: sql-gate
62
+ uses: culpritsql/culprit-ci@v0.5.6
63
+ continue-on-error: true
64
+ with:
65
+ sql: "migrations/*.sql"
66
+ queries: "src/**/*.sql"
67
+ engine: "postgres"
68
+ before-plan: "plans/before.json"
69
+ after-plan: "plans/after.json"
70
+ fail-on: "high"
71
+ sarif-file: "culprit.sarif"
72
+ post-comment: "true"
73
+
74
+ - name: Upload SARIF inline annotations
75
+ uses: github/codeql-action/upload-sarif@v3
76
+ with:
77
+ sarif_file: culprit.sarif
78
+
79
+ - name: Enforce SQL gate
80
+ if: steps.sql-gate.outputs.passed != 'true'
81
+ run: exit 1
82
+ ```
83
+
84
+ ---
85
+
86
+ ## CLI Quickstart
87
+
88
+ Run directly with `npx`, `bunx`, or install in `devDependencies`:
89
+
90
+ ```bash
91
+ # Lint PostgreSQL migrations + application queries and write SARIF + PR Markdown
92
+ npx @culpritsql/ci \
93
+ --sql "migrations/*.sql" \
94
+ --queries "src/**/*.sql" \
95
+ --engine postgres \
96
+ --fail-on high \
97
+ --sarif culprit.sarif \
98
+ --markdown-out comment.md
99
+
100
+ # Gate a single execution plan and compare Before vs After plans
101
+ npx @culpritsql/ci \
102
+ --plan plans/checkout.json \
103
+ --compare plans/before.json plans/after.json \
104
+ --max-cost 5000 \
105
+ --format markdown
106
+ ```
107
+
108
+ ### Exit Codes
109
+
110
+ | Code | Meaning |
111
+ | :--- | :--- |
112
+ | `0` | Gate passed (no findings at or above `--fail-on` and no plan regression) |
113
+ | `1` | Gate failed (`findings >= --fail-on` or execution plan regressed) |
114
+ | `2` | Invalid CLI arguments or unreadable input file |
115
+
116
+ ---
117
+
118
+ ## Rule Reference
119
+
120
+ ### Migration Rules (`MIG*`)
121
+
122
+ | Rule | Severity | Engines | What It Catches & Recommended Fix |
123
+ | :--- | :--- | :--- | :--- |
124
+ | `MIG001` | `high` | Postgres, MSSQL, MySQL | `CREATE [UNIQUE] INDEX` on an existing table without `CONCURRENTLY` (Postgres), `WITH (ONLINE = ON)` (SQL Server), or `ALGORITHM=INPLACE, LOCK=NONE` (MySQL). Automatically skips tables created via `CREATE TABLE` in the same migration file. |
125
+ | `MIG002` | `high` | Postgres | `CREATE/DROP INDEX CONCURRENTLY` placed inside an explicit `BEGIN` / `START TRANSACTION` block (fails at runtime in PostgreSQL). |
126
+ | `MIG003` | `high` | Postgres, MSSQL, MySQL | `ALTER COLUMN ... TYPE` / `MODIFY COLUMN` table rewrite, or `ADD COLUMN ... NOT NULL` on an existing table without a safe constant `DEFAULT` (or with volatile functions like `gen_random_uuid()` / `random()`). |
127
+ | `MIG004` | `medium` | All | `FOREIGN KEY (col)` or `REFERENCES` added without a covering `CREATE INDEX`, `PRIMARY KEY`, or `UNIQUE` constraint in the migration or `--schema` snapshot. |
128
+ | `MIG005` | `high` | All | `DROP TABLE`, `ALTER TABLE ... DROP COLUMN`, or `TRUNCATE` without `-- culprit-ignore: MIG005`. Ignores safe `DROP CONSTRAINT` and `DROP NOT NULL` clauses. |
129
+ | `MIG006` | `low` | Postgres, MSSQL | DDL modifying an existing table without `SET lock_timeout` (Postgres) or `SET LOCK_TIMEOUT` (SQL Server) earlier in the migration. |
130
+
131
+ ### Query Rules (`QRY*`)
132
+
133
+ | Rule | Severity | Engines | What It Catches & Recommended Fix |
134
+ | :--- | :--- | :--- | :--- |
135
+ | `QRY001` | `high` | All | `UPDATE` or `DELETE` statement with no top-level `WHERE` clause (accurately flags statements whose only `WHERE` lives inside a scalar subquery). |
136
+ | `QRY002` | `medium` | All | Non-sargable function wrapping a column in `WHERE` (`LOWER(col) =`, `UPPER(col) =`, `DATE(col) =`, `YEAR(col) =`, `CAST(col AS ...) =`). |
137
+ | `QRY003` | `low` | All | Leading wildcard `LIKE '%...'` or `ILIKE '%...'` predicate preventing standard B-tree index range seeks. |
138
+
139
+ ### Execution Plan Rules (`PLAN*`)
140
+
141
+ | Rule | Severity | What It Catches |
142
+ | :--- | :--- | :--- |
143
+ | `PLAN001` | `medium` | Full table scan (`Seq Scan`, `Table Scan`, `Clustered Index Scan`) when `--allow-seq-scan` is not set. |
144
+ | `PLAN002` | `high` | Sort, Hash, or Exchange operator spilling temporary pages to disk. |
145
+ | `PLAN003` | `high` / `medium` | Bottleneck insights surfaced by `@culpritsql/plans` (key lookups, misestimates, implicit conversions). |
146
+ | `PLAN004` | `high` | Total optimizer plan cost exceeding `--max-cost`. |
147
+ | `PLAN005` | `high` | Structural, runtime, cost, or warning regression between `--compare <before> <after>` plans. |
148
+
149
+ ### Inline Suppression Pragmas
150
+
151
+ Suppress specific rules on an intentional statement or across a whole file:
152
+
153
+ ```sql
154
+ -- Suppress on the next statement:
155
+ -- culprit-ignore: MIG005
156
+ DROP TABLE legacy_sessions_2024;
157
+
158
+ -- Or inline on the same line:
159
+ TRUNCATE TABLE staging_import; -- culprit-ignore: MIG005, MIG006
160
+
161
+ -- Suppress across an entire file:
162
+ -- culprit-ignore-file: MIG006
163
+ ```
164
+
165
+ ---
166
+
167
+ ## PR Comment & SARIF Preview
168
+
169
+ ### Sticky Pull Request Comment (`formatMarkdown`)
170
+
171
+ > ## Culprit SQL CI: Passed
172
+ >
173
+ > ![Culprit CI Passed](https://img.shields.io/badge/Culprit_CI-Passed-2ea043?style=flat-square) **High:** 0 | **Medium:** 1 | **Low:** 0 | **Files Checked:** 3 | **Fail Threshold:** `high`
174
+ >
175
+ > ### Execution Plan Diff (Verified)
176
+ > - **Comparison:** `before.json` -> `after.txt` (`postgres`)
177
+ > - **Verdict:** **3467.4x lower cost (53813.63 -> 15.52)**
178
+ >
179
+ > [Open Interactive Diff in Culprit Explain](https://explain.culpritsql.com/#s=z1...)
180
+ >
181
+ > ### Findings
182
+ > | File:Line | Rule | Severity | Issue & Fix |
183
+ > | :--- | :--- | :--- | :--- |
184
+ > | `migrations/004_fk.sql:3:3` | `MIG004` | MEDIUM | Foreign key on 'invoices (account_id)' has no covering index in this migration or schema snapshot. **Fix:** Create an index with leading column(s) (account_id) on 'invoices' to avoid lock amplification and full scans on parent updates/deletes. Suggested SQL: `CREATE INDEX CONCURRENTLY idx_invoices_account_id ON invoices (account_id);` |
185
+
186
+ ### SARIF 2.1.0 Code Scanning Output (`formatSarif`)
187
+
188
+ ```json
189
+ {
190
+ "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
191
+ "version": "2.1.0",
192
+ "runs": [
193
+ {
194
+ "tool": {
195
+ "driver": {
196
+ "name": "Culprit CI",
197
+ "version": "0.5.6",
198
+ "rules": [{ "id": "MIG001", "defaultConfiguration": { "level": "error" } }]
199
+ }
200
+ },
201
+ "results": [
202
+ {
203
+ "ruleId": "MIG001",
204
+ "level": "error",
205
+ "locations": [
206
+ {
207
+ "physicalLocation": {
208
+ "artifactLocation": { "uri": "migrations/002_idx.sql", "uriBaseId": "%SRCROOT%" },
209
+ "region": { "startLine": 2, "startColumn": 1, "endLine": 2, "endColumn": 52 }
210
+ }
211
+ }
212
+ ]
213
+ }
214
+ ]
215
+ }
216
+ ]
217
+ }
218
+ ```
219
+
220
+ ---
221
+
222
+ ## Programmatic API
223
+
224
+ ```ts
225
+ import {
226
+ lintSql,
227
+ analyzePlanGate,
228
+ comparePlanGate,
229
+ runCiGate,
230
+ formatSarif,
231
+ formatMarkdown,
232
+ } from "@culpritsql/ci";
233
+
234
+ const findings = lintSql(
235
+ "CREATE INDEX idx_orders_email ON orders (email);",
236
+ { file: "migrations/002_idx.sql", engine: "postgres" },
237
+ );
238
+
239
+ const diffGate = await comparePlanGate(beforePlanJson, afterPlanJson, {
240
+ beforeName: "main",
241
+ afterName: "PR #42",
242
+ });
243
+ console.log(diffGate.verdictSummary, diffGate.explainUrl);
244
+ ```
245
+
246
+ ---
247
+
248
+ ## License
249
+
250
+ MIT. See [LICENSE](./LICENSE).
package/dist/cli.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+ import { type FailOnThreshold, type ReportFormat, type SqlEngine } from "./index.js";
3
+ interface ParsedCliArgs {
4
+ help: boolean;
5
+ version: boolean;
6
+ sqlPatterns: string[];
7
+ queryPatterns: string[];
8
+ schemaFile?: string;
9
+ engine: SqlEngine;
10
+ planFiles: string[];
11
+ comparePlans: {
12
+ beforeFile: string;
13
+ afterFile: string;
14
+ } | null;
15
+ failOn: FailOnThreshold;
16
+ format: ReportFormat;
17
+ sarifOut?: string;
18
+ markdownOut?: string;
19
+ jsonOut?: string;
20
+ allowSeqScan: boolean;
21
+ maxCost?: number;
22
+ disabledRules: string[];
23
+ }
24
+ export declare function formatHelpText(): string;
25
+ export declare function parseCliArgs(argv: string[]): ParsedCliArgs;
26
+ export declare function runCli(argv: string[], io?: {
27
+ cwd?: string;
28
+ stdout?: (text: string) => void;
29
+ stderr?: (text: string) => void;
30
+ }): Promise<number>;
31
+ export {};
32
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAGA,OAAO,EACL,KAAK,eAAe,E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