Skip to content

simplify: reuse what the repo already had, and fix two defects the review found - #390

Merged
huyplb merged 1 commit into
mainfrom
simplify/reuse-and-review-fixes
Sep 9, 2026
Merged

simplify: reuse what the repo already had, and fix two defects the review found#390
huyplb merged 1 commit into
mainfrom
simplify/reuse-and-review-fixes

Conversation

@huyplb

@huyplb huyplb commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Four review angles over the last three merged PRs. Two findings were defects, not untidiness.

Two things were wrong

SQL Server reported size and rows multiplied by the column count.
packages/sql/src/providers/sqlServer/sqlserver.table-insight.ts

The scalars were SUM(...) OVER () beside a LEFT JOIN sys.columns, which fans the result to one row per column — the window then summed the fanned set. A 20-column table reported 20× its rows and 20× its size. The row count had this already; the new size_bytes line copied the broken shape.

Now a CROSS APPLY that aggregates on its own row set. Size counts every partition (the hint promises table and indexes); rows count only index_id 0/1, or every index would multiply them again.

My test asserted sql.toContain('used_page_count') — it could never have caught this. Still unverified live: that container will not start here.

The orphan query was invalid on Oracle. It aliased tables with AS. Verified both ways on Oracle 23:

FROM demo_a.orders AS fox_c   ->  ORA-03048: SQL reserved word 'AS' is not syntactically valid
FROM demo_a.orders fox_c      ->  runs

A bare correlation name is accepted on every engine here, so no dialect branch — one spelling, tested across five dialects.

Reuse — the point of the exercise

  • formatBytes already existed in @foxschema/sql, imported by three sibling components. I wrote a fourth copy, worse than the original: it took number where the shared one takes number | null | undefined, so I also added a call-site null guard the real one makes unnecessary, plus tests re-covering dba-utilities.test.ts. All deleted.
  • Row counts used bare .toLocaleString() — host-locale dependent — two clicks from a schema explorer that uses formatRowCount, which pins en-US precisely so UI and tests do not vary by machine.
  • The FK key was spelled three times, once with ', ' against ','. The two Peek copies had to stay byte-identical or a count written under one key was never found under the other. Now fkKey.
  • foreignKeys re-implemented findCachedTable from a module the file already imports — and more weakly, matching a bare name across schemas.
  • The new FK row wrote out panelCls plus its exact padding, in the file that imports Panel. The clearest evidence the primitive was not yet the easy path.

Efficiency

The orphan check ran one request per foreign key, serially — and /sql/execute opens and closes a connection per request, so six keys meant six handshakes back to back, with one failure discarding every scan already paid for. executeSql already accepts an array and runs it on one connection with per-statement isolation. Now a single request that keeps whatever succeeded. Verified live: two orphan statements in one request return 1 and 1 over planted data.

Unbounded Promise.all would have been worse than either, since each call creates its own connection.

Separately, pg_total_relation_size sat in the target list of a query returning one row per column, and each evaluation stats every fork, the TOAST relation and every index. Now an uncorrelated scalar subquery, evaluated once.

Smaller

Narrowed two whole-store selectors that re-ran the catalog probe on unrelated connections; memoised AccountStage's derivations (they re-ran on every keystroke in the principal filter); made its props required and deleted an unreachable branch; dropped Panel's padded, which had no caller outside its own test; removed the sql.raw hole from the orphan builder in favour of a closed shape parameter.

Suite 3655 passed · eslint 0 errors · typecheck clean.

Deliberately not fixed here

Four findings are real but larger than this change, and I would rather they be seen than buried:

  1. shared/lib/provider-settings.ts is a 330-line hand copy of the @foxschema/sql provider layer, and has already driftedpostgresSettings.schemaRequired is false in the frontend and true in the package. It already delegates for 5 of 16 dialects, so the boundary is proven crossable.
  2. surfaces.tsx is barely adopted — 4 files import it; ~27 exact copies of sectionLabelCls remain across 15 files, and the label survives in 16 spellings. Extraction without migration risks being a ninth variant. Worth a codemod plus a naming.test.ts-style guard, which is how this repo already enforces conventions.
  3. ResultGridLike is declared twice in files that already import from each other.
  4. The table-insight size expressions duplicate the per-dialect ones in dba-utilities — including Redshift, where the repo already has a working svv_table_info query, so Peek Insight shows for a size Utilities prints correctly.

🤖 Generated with Claude Code


Note

Medium Risk
Touches SQL generation for orphan checks and SQL Server catalog probes used by Peek Insight; logic is tested but wrong SQL would affect multi-FK orphan scans and SQL Server row/size display.

Overview
Fixes two catalog/query bugs and consolidates Data Peek Insight behavior around shared helpers and fewer round trips.

SQL Server table insight no longer multiplies estimated rows and size by column count: table-level totals move into a CROSS APPLY aggregate instead of SUM(...) OVER () beside the sys.columns join.

Orphan FK SQL drops AS on table aliases so Oracle accepts the statement; fkKey unifies FK map keys and orphan peek titles (fixing mismatched join separators that showed every FK as “not checked”).

Peek Insight reuses formatBytes / formatRowCount from @foxschema/sql, resolves FKs with findCachedTable, reads dialect from the connection store, batches orphan counts in one executeSql call (keeping partial successes), and narrows Zustand selectors to cut unnecessary refetches. AccessPermissionPanel memoizes AccountStage derivations; Panel always includes padding (padded removed).

Reviewed by Cursor Bugbot for commit 75f1e5e. Bugbot is set up for automated code reviews on this repo. Configure here.

…wrong

A four-angle review of the last three merged PRs. Two findings were defects,
not untidiness.

**SQL Server reported size and rows multiplied by the column count.** The
probe's scalars were `SUM(...) OVER ()` beside a `LEFT JOIN sys.columns`, which
fans the result to one row per column; the window then summed the fanned set. A
20-column table reported 20x its rows and 20x its size, and `size_bytes` — the
figure a doc comment in this very diff calls "the kind of figure people act on".
The row count had the defect already and the new size line copied its shape.
Both now come from a CROSS APPLY that aggregates on its own row set. Size
counts every partition, since the hint promises table *and* indexes; rows count
only index_id 0/1, or every index would multiply them again. My test asserted
`toContain('used_page_count')`, which could never have caught this.

**The orphan query was invalid on Oracle.** It aliased tables with `AS`, and
Oracle answers ORA-03048. Verified both ways on Oracle 23: with `AS`, the error;
without it, the query runs. A bare correlation name is accepted everywhere this
ships, so no dialect branch — one spelling, and a test across five dialects.

Reuse, which is what the review was for:

- `formatBytes` already existed in @foxschema/sql, imported by three sibling
  components. I had written a fourth copy, worse than the original — it took
  `number` where the shared one takes `number | null | undefined`, so I also
  added a null guard at the call site that the real one makes unnecessary, and
  tests for behaviour dba-utilities.test.ts already covered. All deleted.
- Row counts used bare `.toLocaleString()`, which follows the host locale, two
  clicks from a schema explorer that uses `formatRowCount` and pins en-US.
- The FK identity key was spelled three times, once with `', '` against `','`.
  The two Peek copies had to stay byte-identical or a count written under one
  key was never found under the other. Now `fkKey`.
- `foreignKeys` re-implemented `findCachedTable` from a module the file already
  imports — and more weakly, matching a bare name across schemas.
- The new per-FK row wrote out `panelCls` plus its exact padding, in the file
  that imports `Panel`. That is the clearest evidence the primitive was not yet
  the easy path.

Efficiency: the orphan check ran one request per foreign key, serially, and
/sql/execute opens and closes a connection per request — so six keys meant six
handshakes paid one after another, and one failure discarded every scan already
paid for. `executeSql` already takes an array and runs it on one connection with
per-statement isolation, so it is now a single request that keeps the counts
that succeeded. Verified live: two orphan statements in one request return 1 and
1 over planted data. Unbounded Promise.all would have been worse than either,
since each call creates its own connection.

Also: `pg_total_relation_size` was in the target list of a query returning one
row per column, and each evaluation stats every fork, the TOAST relation and
every index. It is now an uncorrelated scalar subquery, evaluated once.

Smaller: narrowed two whole-store selectors that re-ran the catalog probe on
unrelated connections; memoised AccountStage's two derivations, which re-ran on
every keystroke in the principal filter; made its props required and deleted an
unreachable branch; dropped `Panel`'s `padded`, which had no caller outside the
test written for it; removed the `sql.raw` escape hatch from the orphan builder
in favour of a closed shape parameter.

Suite 3655 passed, eslint 0 errors, typecheck clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@cursor

cursor Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_b0c7de60-93b2-4f18-a2c0-8d464959605e)

@huyplb
huyplb merged commit 11240e3 into main Sep 9, 2026
12 checks passed
@huyplb
huyplb deleted the simplify/reuse-and-review-fixes branch September 9, 2026 00:43
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