Skip to content

Proof-Carrying AI SQL Auditing

Writing SQL against unfamiliar schemas invites silent join fan-outs, key normalization mismatches, and accidental row drops. Combine mapkeel, statkeel, and auditkeel to ground every query against real database structure before trusting a single number.

1. Map Schemas & Discover Implicit Joins (mapkeel)

Section titled “1. Map Schemas & Discover Implicit Joins (mapkeel)”

Replace Java + Graphviz SchemaSpy with a single native binary that catalogs schemas, emits inline SVG/Mermaid ER diagrams, detects schema anomalies, and discovers implicit cross-schema joins:

Terminal window
mapkeel map "$DATABASE_URL" --schema populi,canvas --format agent

2. Profile Tables & Check Join Health (statkeel)

Section titled “2. Profile Tables & Check Join Health (statkeel)”

Profile unfamiliar tables to catch NEAR_UNIQUE_KEY fan-out traps and PII columns, then check cross-source join cardinality and orphans before writing the query:

Terminal window
# Profile a table and generate an offline interactive HTML report
statkeel profile "${DATABASE_URL}#canvas.pseudonyms" --html profile.html
# Check cross-source join cardinality, orphans & key normalization fixes
statkeel join "${DATABASE_URL}#populi.students" "${DATABASE_URL}#canvas.pseudonyms" \
--left-key person_id --right-key sis_user_id

3. Audit Read-Only SQL & Prove Refactor Parity (statkeel + auditkeel)

Section titled “3. Audit Read-Only SQL & Prove Refactor Parity (statkeel + auditkeel)”

Run strictly read-only invariant checks, CTE row waterfalls, grain spot-checks, and live A/B query comparisons with offline HTML receipts:

Terminal window
# Audit an AI-written SQL query against accounting invariants (read-only)
statkeel audit query.sql --db "$DATABASE_URL" --rules rules.toml --html trust-receipt.html
# Prove a refactored query returns identical rows
statkeel compare --db "$DATABASE_URL" \
--baseline-query old.sql --candidate-query new.sql \
--key person_id --fail-on-row-diff
# Trace CTE waterfalls, join fan-outs, and grain checks with auditkeel
auditkeel run analysis.sql --dsn "$DATABASE_URL" --key student_id,term_id --format agent