Skip to content

Latest commit

 

History

History
79 lines (65 loc) · 4.83 KB

sql.md

File metadata and controls

79 lines (65 loc) · 4.83 KB

SQL gates (sql extra)

ci_kit.sql holds gates that read SQL embedded in python source parsed (sqlglot, postgres dialect), so a docstring, a -- comment or a string literal that merely mentions SQL is never read as SQL, and a SQL value that will not parse is exit 2, never a clean pass.

Install with the sql extra (ci-kit[sql]): sqlglot is the only dependency the kit's gates take at their core, and only here. The composite action installs it for these tools on its own.

command module finds
ci-sql-casts ci_kit/sql/casts.py asyncpg bind/cast footguns, rules A-D
ci-pit-reads ci_kit/sql/pit_reads.py SQL wall-clock reads (now(), current_date, ...), a per-file count ratchet
ci-decimal-reads ci_kit/sql/decimal_reads.py a NUMERIC column projected without a cast
ci-phantom-columns ci_kit/sql/phantom_columns.py a single-table statement naming a column its table lacks
ci-column-comments ci_kit/sql/column_comments.py a migration column comment that declares a domain with no CHECK enforcing it
ci-sql-placement ci_kit/sql/placement.py SQL outside the repository layer (a ratchet, see ratchets.md)

The gates

  • ci-sql-casts: :x::type (A), CAST(:x AS timestamptz) on a str (B, ratchet), now() - :age (C), :x + interval (D, ratchet).
  • ci-pit-reads: SQL wall-clock reads give plausible numbers from the WRONG day under a replay clock; a per-file count ratchet.
  • ci-decimal-reads: a NUMERIC column projected without a cast decodes as Decimal. Ground truth is a --cols file of schema.table.column; a missing or empty one is exit 2.
  • ci-phantom-columns: checked against a --schema JSON built from a migrated database. Precision over recall.
  • ci-column-comments: a comment that declares a domain (a magnitude, 0..1, an enum, >= 0) with no CHECK enforcing it.

Shared readers and the Config voice

sqlast (python module to parsed SQL Sites) and sqllit (python module to SQL string literals, tokenizer-based, no parse) are the shared readers. Statement heads: SELECT, INSERT, UPDATE, DELETE, WITH, CREATE ... AS, since v1.9.0 TRUNCATE, and since v1.93.0 MERGE INTO (upper-case only) and a CTE whose name sqlglot tokenizes as a keyword (WITH ref AS). sqlast.sites(..., on_unparsed=) (v1.93.0) is for a reader that COUNTS what it could not parse instead of stopping: each unparseable value goes to the callback and the module is still read. A gate leaves it unset, so its first unparseable value is still exit 2.

The code-edge reader (code_edges, v1.93.0)

Not a gate: a map. python -m ci_kit.sql.code_edges ROOT --dirs DIR [DIR ...] --out FILE.json reads every SQL value in DIR/*.py (a * in DIR is one path segment, expanded sorted) and writes {edges: [{src, dst, kind}], files, parsed, failed, holes, unparsed}: one edge per (module, schema.table, reads|writes). writes is the TARGET of an INSERT / UPDATE / DELETE / MERGE / TRUNCATE (a CTE's included), a CREATE TABLE .. AS or a SELECT .. INTO; the SELECT feeding an INSERT, an UPDATE's FROM and a DELETE's USING are reads, and a table both written and read in one statement is writes. Names fold the Postgres way (unquoted to lower case). Unqualified names are not edges (a CTE alias looks the same), nor is a name with an unresolved f-string hole (s.{t}): those are counted in holes. Coverage is part of the answer: an unparseable value or module is counted in failed and named in unparsed, never dropped. Exit 0 written, 2 did not look (a DIR that does not exist, a pattern that matches nothing, a DIR with no python file, an unwritable --out). Built to run at image build time, so the map ships as a file and the kit stays out of the runtime image.

Each gate keeps its detection here and takes its voice (gate name, re-run command, doc pointer, baseline paths) from a Config, so a repo that already had the gate keeps its output byte-identical. Since v1.5.0 every gate's main is a site_lint.check CLI: --selftest works under a wrapper's Config, a crash mid-scan is exit 2 (did not finish looking) instead of a traceback, and --write-baseline / --list are its mode flags. So a repo wrapper that hands its argv to ci_kit.sql.<gate>.main gets all of that too.

Using them

As a dev dependency (their baselines live in your repo):

[dependency-groups]
dev = ["ci-kit[sql] @ git+https://github.com/mjdevaccount/ci-kit@v1.51.0"]
uv run ci-sql-casts --scan-dir app --baseline ci/sql_casts_baseline.txt --d-baseline ci/sql_bind_arith_baseline.txt
uv run ci-pit-reads --scan-dir app --baseline ci/pit_reads_baseline.txt
uv run ci-decimal-reads --scan-dir app --cols ci/numeric_columns.txt --baseline ci/decimal_reads_baseline.txt
uv run ci-column-comments --migrations migrations --legacy-max 7