Repository navigation
fix(sqlite): keep triggers whose declaration spells the table in another case - #2807
Conversation
…her case SQLite stores a trigger's target in sqlite_schema.tbl_name exactly as written in CREATE TRIGGER and resolves it by ASCII case folding, while an index stores the table's own name. The selected_table scope filtered tbl_name with COLLATE BINARY, so a trigger declared ON users for table Users was omitted while the schema still reported complete. Match dependents with COLLATE NOCASE, and report each trigger's table as the stored table or view name so the selected-table contract still holds.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3d6250c1c4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // match exactly before rows are read. | ||
| const selection = | ||
| scope === "selected_table" ? " WHERE tbl_name = ? COLLATE BINARY" : ""; | ||
| scope === "selected_table" ? " WHERE tbl_name = ? COLLATE NOCASE" : ""; |
There was a problem hiding this comment.
Check the exact table before the NOCASE expansion
When the caller supplies a case-mismatched name (for example users for Users, with the triggers declared on Users), this predicate now expands the real table and all of its declarations before readRows can discover that selectedTable is undefined. On a valid database whose individually valid trigger definitions exceed 16 MiB in aggregate, OutputBudget.retain therefore throws output-limit, which is projected as AnalysisResourceConstraintError instead of the exact-table AnalysisInputError; the previous BINARY filter returned no rows and reached the selection check. Establish the exact metadata.has(selectedName) match before the NOCASE scan so the caller's invalid selection keeps its meaningful failure reason.
AGENTS.md reference: AGENTS.md:L29-L29
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed. In 6f369b7 the exact name is checked against PRAGMA table_list before the NOCASE scan, sharing the existing selection failure with readRows. A new boundary test selects users for Users with a trigger whose escaped declaration exceeds the reply budget; it reported output-limit before this commit and now reports the exact-name selection error.
With the NOCASE expansion, a selection such as users for table Users expanded the stored table's declarations before readRows reported the missing exact name, so large trigger definitions could surface as an output-limit failure instead of the selection error. Check the exact table name against PRAGMA table_list first and share the selection failure with readRows.
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6f369b7701
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (selectedName !== undefined && !metadata.has(selectedName)) | ||
| throw missingSelectedTable(selectedName); |
There was a problem hiding this comment.
Reject exact views before the case-folded scan
The new exact-name guard still accepts an exact view because PRAGMA main.table_list places views in metadata. With table: "Recent" and an INSTEAD OF trigger declared ON recent whose individually valid SQL JSON-expands past 16 MiB (for example, about 3 MiB of control characters), the NOCASE scan retains that trigger and throws output-limit before readRows can reject the non-ordinary table; before this change the BINARY scan skipped the differently cased trigger and returned the table-selection error. Reject definitive views before broadening the scan so the invalid caller selection keeps its meaningful failure reason.
AGENTS.md reference: AGENTS.md:L29-L29
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, and the same applied to virtual and possible shadow tables. In 8fae046 the scan folds case only when the selection is an ordinary table that is not a possible shadow table; any other selection keeps the exact BINARY filter, so a refused selection fails as it did on main. The boundary test now also selects an exact view Recent with an INSTEAD OF trigger declared ON recent whose escaped SQL exceeds the reply budget: it reported output-limit on the previous head and now reports the selection error.
An exact view, virtual or possible shadow table passed the exact-name guard, so the NOCASE scan could still expand a differently cased trigger past the reply budget before readRows refused the selection. Fold case only when the selection is an ordinary table that is not a possible shadow table, and keep the exact filter otherwise so every refused selection fails as it did before the case-folding change.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8fae0466fd
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // A trigger stores its target as written in CREATE TRIGGER, which SQLite | ||
| // resolves by ASCII case folding; report the stored table or view name. | ||
| const storedNames = new Map( | ||
| [...metadata.keys()].map((name) => [sqliteIdentifierKey(name), name]), |
There was a problem hiding this comment.
Avoid folding unrelated schema names
When a selected-table inspection opens a database with large unrelated uppercase table/view names, this eagerly case-folds every PRAGMA table_list name even though the following query filters those objects out. In a --max-old-space-size=256 process, three valid 5.2 MiB view identifiers (15.6 MiB combined, with every schema record accepted by the configured 16 MiB SQLite limit) made this step add about 5.4 seconds, raise heap use from about 37 MiB to 217 MiB, and raise RSS from about 96 MiB to 358 MiB while selecting a tiny unrelated table. Build the canonical lookup lazily from encountered trigger rows, or limit it to names relevant to the requested scope, so unrelated schema cannot consume most worker headroom.
AGENTS.md reference: AGENTS.md:L49-L50
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed. In 8523492 a selected-table scan reports the selected name directly, because it only returns that table's dependents. Other scopes use the exact stored name when one matches and fold the stored names once, lazily, only when a trigger spells its target differently. With your scenario (a tiny selected table plus three 5.2 MiB view names, --max-old-space-size=256), inspectSqliteDatabaseSnapshot took about 760 ms with a 197 MiB heap increase on the previous head. It now takes 143 ms with a 22 MiB increase, the same as main (144 ms, 22 MiB).
The stored-name lookup case-folded every PRAGMA table_list name up front, so a selected-table inspection of a tiny table paid for large unrelated identifiers. A selected-table scan only returns the selected table's dependents, so report the selected name directly; other scopes use an exact stored name when one matches and fold the stored names once, only when a trigger spells its target differently.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8523492740
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| ? unresolvedVirtualTables.find((parent) => | ||
| sqliteIdentifierKey(name).startsWith(parent.prefix), |
There was a problem hiding this comment.
Hoist identifier folding out of the parent scan
When PRAGMA table_list contains multiple virtual tables whose modules are unavailable and a selected ordinary table has a large uppercase name that matches none of their prefixes, this callback executes sqliteIdentifierKey(name) once per unresolved parent; the selected-table flow then calls shadowParentOf both before and during the schema scan. On Node 24.15, a 1 MiB name with 20 nonmatching parents took about 1.9 seconds here versus 0.09 seconds when folded once, so a multi-MiB identifier plus a few dozen unresolved parents can exhaust the documented 30-second worker deadline. Compute the folded identifier once before .find, as the pre-change implementation did.
AGENTS.md reference: AGENTS.md:L49-L50
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, that was a regression from extracting the helper. In 62b4c3f the name is folded once per call, before .find, as on main, and the selected table's result from the pre-scan check is reused in the scan, so the selected name is folded only once. Measured with a 1 MiB selected table name and 20 fts5 tables made unresolvable (the same byte patch as the boundary test), via inspectSqliteDatabaseSnapshot on Node 24.19: previous head about 1,130 ms, main 58 ms, this head 66–69 ms.
The extracted shadow-parent helper folded the table name inside the find callback, once per unresolved virtual table, and the selected table was checked both before and during the schema scan. Fold the name once per call as before, and reuse the selected table's result in the scan.
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
Thank you for #2807, @Dragolone. Matching trigger targets with SQLite's ASCII case-folding rule ensures selected-table inspection includes triggers declared with a different case and attributes them to the stored table name. The tests also protect the exact-name refusal behavior for selections that cannot be read. |
Summary
In
selected_tablescope,inspect_sqlite_databaseomits triggers whoseCREATE TRIGGER … ON <name>spells the selected table in a different ASCII case, while still reportingschema.completeness: "complete".SQLite stores a trigger's target in
sqlite_schema.tbl_nameexactly as written in the declaration and resolves it by ASCII case folding; an index stores the table's own name. The selected-table query filteredtbl_name = ? COLLATE BINARY, so such triggers never reached the result.No issue is linked: I searched issues and PRs for "sqlite trigger", "tbl_name", "selected_table trigger", "sqlite case-insensitive" and "inspect_sqlite_database" and found no report. Open PRs #2774 and #2701 touch nearby code (a column-selection error reason, and moving the
trigger.table === input.tablecheck) without changing this behavior.Problem and expected behavior
Smallest trigger:
rea inspect-sqlite-database case.db --table Usersreports one trigger (audit_delete) withcompleteness: "complete".audit_insertfires on inserts intoUsersbut is missing.Violated contract:
docs/sqlite-databases.mdsaysselected_table"expands that table and its indexes and triggers", andschema.completenessrefers to the reported scope.Expected: both triggers, each attributed to
Users.Change and scope
selected_tablescope the filter usesCOLLATE NOCASEonly when the selection is an exact ordinary table that is not a possible shadow table, which are the only selections whose rows can be read. NOCASE folds only ASCII letters, the same rule SQLite applies when it resolves the table named by an index or trigger. Every other selection keeps the exactBINARYfilter, so a selection that is refused later fails exactly as it does on main, whatever its dependents declare: a view, a virtual table, a possible shadow table, or a name that is not stored. A name absent fromPRAGMA main.table_listis refused before the scan with the existing "does not exist with that exact name" selection failure, now shared withreadRows. (Both refinements come from the Codex review.)trigger.tablenow reports the stored table or view name. Inselected_tablescope that is the selected name, since the scan returns only its dependents. In other scopes it is the exact stored name when one matches. Otherwise thePRAGMA main.table_listnames are folded once, lazily, with the existingsqliteIdentifierKey, so unrelated large identifiers are never folded for a selected-table inspection (Codex review). This is needed, not cosmetic:SqliteDatabaseServicerequirestrigger.table === input.tableinselected_tablescope, so widening the filter alone would fail that check. It applies in every scope, so a trigger on a view declaredON Recentfor viewrecentnow reportsrecent. This matches how indexes are already reported.Contribution scope: an ordinary format-inspection correctness fix within existing boundaries; a maintainer scope decision is not applicable.
Contract and boundary impact
readSchemainsrc/sqlite/SqliteDatabaseInspection.ts(schema selection and trigger projection).triggers[].tablevalues change only when a declaration spelled the target in another ASCII case; they now name the stored object.trigger.tableis a resolved name rather than the declaration's spelling; the declaration text itself is still retained insqloutsideinventoryscope.docs/public/product-catalog.json), package, or installation impact: none.Expected effects (check known effects; use documentation-only on its own).
Unchecked items do not establish absence; leave uncertainties visible.
Evidence and regression coverage
Tests added or updated:
tests/boundary/sqlite/sqliteDatabaseInspection.test.ts— "attributes triggers declared with another ASCII case to the stored table name". It coversselected_table(both triggers,completeness: "complete", index unchanged) andinventory(triggers on a table and anINSTEAD OFtrigger on a view report the stored names). A second, parameterized test, "refuses $selection before expanding declarations under another spelling", covers a differently cased table (usersforUsers) and an exact view (Recentwith anINSTEAD OFtrigger declaredON recent). In both cases the trigger's JSON-escaped SQL exceeds the 16 MiB reply budget, and the test expects the exact-nameselectionfailure. Both cases pass on main, and both reportedoutput-limitat intermediate heads of this PR.Base reproduction: on
91830f1e0the new test fails withAssertionError: expected [] to deeply equal [ Array(2) ](no trigger is selected when the declarations spellusers/USERS).User-visible CLI output (
rea inspect-sqlite-database case.db --table Users --format json, schema fields only):Before (
91830f1e0):{"scope": "selected_table", "completeness": "complete", "indexes": [["users_email", "Users"]], "triggers": [["audit_delete", "Users"]]}After:
{"scope": "selected_table", "completeness": "complete", "indexes": [["users_email", "Users"]], "triggers": [["audit_insert", "Users"], ["audit_delete", "Users"]]}Remaining proof gaps: non-ASCII case variants are not separately tested (both SQLite and the new filter treat them as distinct names).
For evidence-bearing changes:
Validation performed
macOS (Apple silicon), Node 24.19.0, SQLite 3.53.3 via
node:sqlite.npx vitest run tests/boundary/sqlite/sqliteDatabaseInspection.test.ts -t "another ASCII case"on the base — fails as quoted above; passes with the fix.npm run build:cached && npx vitest runon all 8 SQLite test files (git ls-files | grep -i sqlite | grep '\.test\.ts$') — 101 passed, 2 skipped.rea inspect-sqlite-databasebefore/after as shown above.node --max-old-space-size=256, directinspectSqliteDatabaseSnapshotcall). Main: 144 ms, +22 MiB heap. An intermediate head of this PR that folded every name eagerly: 765 ms, +197 MiB. Final head: 145 ms, +22 MiB.npm run typecheck— passed.npx oxfmt --checkon both changed files — passed.npm run verify:module-boundaries— passed.npm run lint— the only error istests/boundary/providers/ghidra/ghidraLauncher.test.ts:431(max-lines-per-function), which this PR does not touch; no errors in the changed files.Compatibility, safety, and release
triggers[].tableagainst the declaration's spelling will now see the stored name, consistent withindexes[].table.Before merge
type(scope): outcome.