Skip to content

fix(grid): write edits to the right table, addressed by the real key - #529

Merged
EVWorth merged 2 commits into
mainfrom
fix/grid-safe-save
Aug 30, 2026
Merged

EVWorth merged 2 commits into
mainfrom
fix/grid-safe-save

Conversation

@EVWorth

@EVWorth EVWorth commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Refs #387, #399, #419. Three faults on the path from editing a cell to changing a row. Together they could write the wrong value to the wrong table and report success.

The wrong table (#399)

The target came from the first FROM clause:

const TABLE_NAME_RE = /\bFROM\s+(?:`([^`]+)`|(\w+))(?:\s|;|$)/i;

For SELECT * FROM orders o JOIN users u ON ... that's orders, whatever column was actually edited. An edit to a users column either failed with "unknown column" or — where both tables have a column of that name — silently changed the wrong table's data. A CTE gave the CTE's alias; a subquery gave nothing.

Editing is now refused unless the rows can be attributed to exactly one table: no joins in either syntax, no CTE, no UNION, no subquery in FROM, no aggregation, one statement, and a SELECT. The check strips string literals and comments before matching structure, so a row whose text contains 'inner join stuff' is still editable.

It errs towards refusing. A query it wrongly declines is an inconvenience; one it wrongly accepts is a silent write to the wrong place, invisible from the grid.

The wrong row (#387)

Rows were addressed by matching on every column, because the result-set metadata reports is_primary_key: false for all of them. MySQL does send that flag — but sqlx keeps ColumnFlags pub(crate), so it can't be read where results are built.

The grid now asks the schema inspector, which reads information_schema, and matches on the real key. Where the key exists but isn't among the selected columns, no row can be addressed at all, so the save is refused rather than attempted against every column. ColumnMeta's field is now commented as the placeholder it is, so nobody starts trusting it.

Reporting success regardless (#419)

A WHERE that matches nothing isn't an error — the statement runs and affects zero rows — so the save said "Applied N change(s)" either way. Rows affected are now counted and a shortfall reported.

Tests

All three were checked against the unfixed code and fail there — I reverted each behaviour, watched the tests go red, and restored.

Two things about the test file worth flagging:

  • its sql-generator mock had grown a gap, because nothing previously reached handleSave — so the new import would have been undefined at runtime with no test noticing
  • mock call counts leak between tests in a describe that doesn't clear them, which made a "did not call executeQuery" assertion pass for the wrong reason until I added clearAllMocks

13 unit tests on the target resolver, 4 on the save path. 1674 frontend tests, 79 unit, 63 integration. clippy/tsc/dprint clean.

Not covered

#400 is largely stale — buildWhereClause already emits IS NULL rather than = NULL. What remains of it (float and blob columns comparing badly in a fallback WHERE) is now mitigated by the real-key lookup and reported by the rows-affected check.

🤖 Generated with Claude Code

Three faults on the path from editing a cell to changing a row, which
together could write the wrong value to the wrong table and report success.

The table was taken from the first FROM clause in the query. For
`SELECT * FROM orders o JOIN users u ON ...` that is orders, whatever column
the user actually edited — so an edit to a users column either failed with
"unknown column" or, where both tables have a column of that name, changed
the wrong table's data. A CTE gave the CTE's alias, a subquery gave nothing.
Editing is now refused unless the rows can be attributed to exactly one
table: no joins in either syntax, no CTE, no UNION, no subquery in FROM, no
aggregation, one statement, and a SELECT. The check reads structure with
string literals and comments stripped first, so a row whose text mentions
"inner join" is still editable. It errs towards refusing, because a query it
wrongly declines is an inconvenience and one it wrongly accepts is a silent
write to the wrong place.

Rows were addressed by matching on every column, because the result-set
metadata reports is_primary_key false for all of them. MySQL does send that
flag, but sqlx keeps ColumnFlags pub(crate), so it cannot be read where the
results are built. The grid now asks the schema inspector, which reads
information_schema, and matches on the actual key. Where the key exists but
is not among the selected columns there is no way to address a row at all, so
the save is refused rather than attempted against every column. The field in
ColumnMeta is now commented as the placeholder it is.

A WHERE that matches nothing is not an error — the statement runs and affects
zero rows — so the save reported "Applied N changes" whether or not anything
changed. Rows affected are now counted and a shortfall is reported.

The tests for all three were checked against the unfixed code and fail there.
Two things worth noting about the test file: its sql-generator mock had grown
a gap, since nothing previously reached handleSave, and mock call counts leak
between tests in a describe that does not clear them, which is what made a
refusal assertion pass for the wrong reason at first.

Refs #387, #399, #419
The browser-mode suite keeps its own mock of sql-generator, which still
listed only the old exports, so importing ResultsGrid failed outright with
"does not provide an export named 'resolveEditTarget'".

Caught by CI rather than locally, because `just test-frontend` runs the unit
suite and browser-mode tests are a separate task. Worth remembering when a
change touches a module that both suites mock.
@EVWorth
EVWorth merged commit dc1f76c into main Aug 30, 2026
9 checks passed
@EVWorth
EVWorth deleted the fix/grid-safe-save branch August 30, 2026 22:46
EVWorth added a commit that referenced this pull request Sep 4, 2026
Closes #409.

The statement was built with a literal placeholder:

  INSERT INTO your_table (`id`, `name`) VALUES (1, 'Alice');

Pasting it fails with "Table 'db.your_table' doesn't exist", which is the
harmless outcome. On a schema where that name happens to be real, it
succeeds and writes the row somewhere the user never chose — which is why
this was filed as security rather than papercut.

The table now comes from resolveEditTarget, the same resolver Save uses,
against the active editor tab's SQL. Reusing it is deliberate: a join or a
CTE selects columns belonging to more than one table, so there is no single
table an INSERT of that row could target, and it already refuses exactly
those shapes. Where it refuses, the menu item is disabled and its hover text
gives the reason rather than leaving a dead entry with no explanation. The
click handler re-checks, since disabled is a UI convention and not a lock.

Identifiers now go through quoteIdentifier instead of bare backticks, so a
table or column name containing a backtick cannot terminate the quoting
early.

MenuItem gains an optional `title`. Nothing else used it; a disabled item
with no way to say why is the reason it exists.

Three tests in the browser suite. All three fail against the previous
implementation — verified by stashing the two source files and re-running.

Also dropped `extractTableName` from that file's sql-generator mock. It was
removed from the module in #529, so the mock was describing an export that
no longer exists.

Co-authored-by: Claude Opus 5 <[email protected]>
EVWorth added a commit that referenced this pull request Sep 8, 2026
…l in the action (#573)

Closes #344, #569, #570.

Three problems on one path, all of them about the app deciding something on
the user's behalf at a moment nobody chose.

## The restart was not asked for

installUpdate ended with an unconditional relaunch(), so a download finishing
closed the app. Downloading and restarting are now separate: installUpdate
stages the update and stops, and the status bar offers "Restart to finish
update" for the user to take when they are ready. That is what VS Code and
Slack do, and for the same reason — the download is harmless, the restart is
the part that can lose work.

There was no UI for the "downloaded" state at all, so a relaunch that failed
or hung left the status bar showing nothing.

## The refusal lived in the button

The checks for a running query and unsaved tabs were in the status bar's
onClick. installUpdate itself had none. One caller today, but nothing said
the next one had to bring its own — the same shape as the grid's save path
before #529.

Worse, they ran once, at the click, before a download that can take minutes,
and the restart then happened against a state nobody had looked at since. A
query started or a tab edited during the download was destroyed by a restart
authorised before either existed.

updateBlockers lives in src/lib/update-guard.ts, so both moments can ask
without the settings store having to know about queries or editor tabs. It is
checked before the download and again before the restart. It reports every
reason rather than the first, so fixing one does not walk into the next.

## A failed install left the status bar lying

installUpdate set "downloading" and then returned without touching it if
pendingUpdate had been cleared — which any concurrent check does, including
the one the status bar fires on mount. The spinner stayed forever, and the
button that could retry only renders in the "available" state.

It now reports what happened and leaves a state the user can act from. The
same applies when relaunch fails: the update is on disk, so it says so and
says to quit and reopen, rather than implying a re-download.

## Tests

Five on the guard, four new on the store. Three fail against the previous
version.

Two existing tests changed rather than being deleted: the relaunch assertion
moved to restartToApply, and the two status-bar tests that asserted the
button refuses now assert the refusal itself, since that is where it moved.

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant