Commit 54603b7
feat(graph): sql_refs — SQL string-literal call sites as queryable graph data
Extracts SQL string-literal references to tables (FROM users,
INSERT INTO orders, UPDATE x SET, DELETE FROM y, CREATE TABLE z)
from app code into a new sql_refs table. Surfaces via a new
codegraph_sql MCP tool: with no `table` arg, top-N tables with
read/write/ddl counts; with `table`, all call sites with enclosing
function.
## Pairs with #95 (SQL extractor) — standalone v1 here
This PR is the call-site half of the original #1 from the spike
list. Once #95 lands upstream and SQL DDL nodes (tables, columns)
are indexed, table_name can be joined against those nodes for full
cross-language navigation ("callers of the users table" returns
both DDL definition + every app-code call site). Until #95 merges,
sql_refs is queryable on its own as a "what code touches table X"
index — already useful, just not yet linked to the schema graph.
## Why a separate table, not graph nodes/edges
Tables aren't declared in code that the existing extractors parse —
they live in `.sql` migration files, which #95 will own. Treating
SQL refs as a side-table (mirror of config_refs from #114) keeps
the core graph clean while still enabling the agent-useful queries.
## Empirical signal on this repo
After the false-positive filter is tightened: 117 refs, 31 distinct
tables. Top results match codegraph's actual SQLite schema:
nodes 16r / 3w
unresolved_refs 10r / 5w
files 10r / 2w
edges 9r / 2w
sql_refs 4r / 6w / 1 ddl
schema_versions 3r / 1w
project_metadata 2r / 1w
Plus `users` from test fixtures. Beats grep because the regex
requires a SQL keyword prefix (FROM/JOIN/INTO/UPDATE/DELETE),
filtering out `const users = ...` style false matches.
## What's added
**Parser** (src/sql-refs/index.ts)
- Per-language regex catalogue. Captures backtick (MySQL),
double-quoted (Postgres), and unquoted identifiers, drops
schema prefixes (public.users → users).
- Pre-filter: line must contain a quote AND a SQL verb.
- Per-match: requires a quote before the matched position
(heuristic for "inside a string literal").
- Comment-stripping per language (// for C-family, # for
Python/Ruby) PLUS same-line block comments (/* */) BEFORE the
prefilter — eliminates the `// "SELECT FROM the docs"` class
of false positives.
- RESERVED_TABLE_NAMES drops SQL keywords that the regex over-
matches on (WHERE, ON, AS, etc.).
**Schema migration v4** with `CREATE TABLE IF NOT EXISTS sql_refs
(id, table_name, op, source_node_id, file_path, line)` and a
case-insensitive index on table_name. FK cascade-deletes on node
removal. Defensive ensureSqlRefsTable() guard at every persistence
path.
**Wiring** (CodeGraph.runSqlRefsPass)
- indexAll: atomic `replaceAllSqlRefs` (clear + reinsert in one
transaction so a mid-flight failure can't leave the table empty).
- sync: incremental — invalidate rows for changed files via
deleteSqlRefsForPaths, then re-extract. Also runs on deletions
(sweeps orphaned rows).
- Per-pass enclosing-function resolver cache (same pattern as #114).
- enableSqlRefs config flag (default true).
**MCP tool** codegraph_sql
- No `table` arg → top-N tables with read/write/ddl counts.
- With `table` → all call sites + enclosing function. Optional
`op` filter.
## Tests
407 pass total, **27 new**:
- Parser core (12): FROM/JOIN/INSERT/UPDATE/DELETE/CREATE/ALTER/
DROP, backtick + double-quote idents, schema-qualified, per-line
multi-op, no-keyword-prefix rejection, line-number 1-indexed,
resolver threading, missing file
- **Comment-stripping (4)**: rejects line-comment-with-quoted-SQL
('// example: SELECT FROM the docs'), rejects block-comment-
with-quoted-SQL, keeps real SQL with trailing comment, Python `#`
comments
- End-to-end CodeGraph (8): basic + enclosing function, reverse
view (getSqlTablesForNode), case-insensitive lookup,
enableSqlRefs:false, incremental sync replace, **regression for
empty-rows data-corruption** (file edited to remove last SQL
ref), deletion sweep, **v4-collision defense** (drop table after
init)
## Reviewer pass
Independent reviewer ran once. Three REQUEST_CHANGES + two INFO
addressed:
- **False positive class** (`// "SELECT FROM the docs"`): added
per-language comment stripping (// + /* */ for C-family, # for
Python/Ruby) BEFORE the pre-filter. Comment-internal quotes no
longer leak through. Tests assert the rejection.
- Atomic clear+reinsert for indexAll path: `replaceAllSqlRefs`
wraps both ops in one transaction.
- Test gap for the false-positive class: explicit test cases added
for line-comment, block-comment, and Python `#` comment shapes.
- Removed MERGE/REPLACE from pre-filter (no matching PATTERNS
entries — pure cosmetic).
- Documented the DELETE-produces-read+write behavior on getSqlTables.
## Coexistence with #112 / #113 / #114
All four PRs target migration v4. Whichever lands second/third/
fourth renumbers in the migrations array — standard rebase.
ensureSqlRefsTable() at every persistence path creates the table
on demand so a botched merge doesn't silently swallow data.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 2dc4bc3 commit 54603b7
10 files changed
Lines changed: 1053 additions & 3 deletions
File tree
- __tests__
- src
- db
- mcp
- sql-refs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
305 | 305 | | |
306 | 306 | | |
307 | 307 | | |
308 | | - | |
| 308 | + | |
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
302 | | - | |
| 302 | + | |
303 | 303 | | |
304 | 304 | | |
305 | 305 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
0 commit comments