Skip to content

test(api): fail when a route reads a table that does not exist - #651

Merged
thinkclay merged 1 commit into
mainfrom
claude/audit-sdk-gaps
Aug 27, 2026
Merged

thinkclay merged 1 commit into
mainfrom
claude/audit-sdk-gaps

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

The pattern

This defect has been found one endpoint at a time, from 500s in a browser, for months:

issue what it was
#476 core.inquiries does not exist
#482 core.organizations — five columns that do not exist
#618 core.employers — a table in no schema
#481 grants missing on 8 core.* tables

Each looked like an isolated typo. A sweep of every .from() in every route file finds 45 such references across 24 table names and 14 files.

file refs file refs
skills.ts 8 webhooks.ts 3
ccpa.ts 6 inquiries.ts 2
office-users.ts 6 office-certifications.ts 2
onet.ts 5 office-communities.ts 1
profile-import.ts 5 office-universities.ts 1
teams.ts 3 organizations.ts / profile-completion.ts / profiles.ts 1 each

A wrong table name is indistinguishable from an empty result unless somebody happens to look, so finding these by running the app does not scale.

How it works

The schema comes from packages/supabase/types.ts — generated from the database and checked in — so the test needs no database.

BASELINE_BROKEN records what already existed. One test fails on anything new; a second fails when an entry is fixed but left behind. The list can only shrink, and cannot rot into fiction.

Two things its own unit tests caught

Both had made my hand audit wrong, which is why the unit cases are there.

.schema() scopes to its statement, not to nearby lines. A backward line-scan reads this as six core deletes:

await supabaseAdmin.schema("core").from("user_skills").delete().eq("user_id", id);
await supabaseAdmin.from("user_certifications").delete().eq("user_id", id);
await supabaseAdmin.from("work_experience").delete().eq("user_id", id);

They are separate statements, so everything after the first addresses public. That is the bug being hunted, and the naive heuristic hides it. References are now grouped by statement.

A schema absent from types.ts cannot be judged. logs.user_feedback is real in the database but logs is not in the generated types — a naive check reports a working route as broken. Unknown schemas are skipped; unknown tables within a known schema are failures.

A third test asserts the catalogue actually parsed, so a shape change in types.ts cannot make this pass vacuously by finding nothing to check.

Scope

Test only — no route behaviour changes. The 45 references it records are filed separately.

🤖 Generated with Claude Code

This defect has been found one endpoint at a time, from 500s in a browser, for
months: #476 (core.inquiries), #482 (core.organizations columns), #618
(core.employers), #481 (grants). Each looked like an isolated typo.

A sweep of every `.from()` in every route file finds 45 such references across
24 table names and 14 files. It is not a handful of typos, and finding them by
running the app does not scale — a wrong table name is indistinguishable from
an empty result unless somebody happens to look.

The schema comes from packages/supabase/types.ts, which is generated from the
database and checked in, so this runs with no database.

BASELINE_BROKEN records what already existed. The test fails on anything new,
and a second test fails when an entry is fixed but left behind, so the list can
only shrink and cannot rot.

Two things the test's own unit cases caught while it was being written, both of
which had made my hand audit wrong:

  - `.schema()` scopes to its statement, not to nearby lines. A backward
    line-scan reads the six unscoped deletes in office-users.ts as `core`,
    which is exactly the bug being hunted — they address `public`. Refs are
    now grouped by statement.
  - A schema absent from types.ts cannot be judged. `logs.user_feedback` is
    real in the database but `logs` is not in the generated types, so a naive
    check reports a working route as broken.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit fb275cf into main Aug 27, 2026
6 of 12 checks passed
@thinkclay
thinkclay deleted the claude/audit-sdk-gaps branch August 27, 2026 17:48
thinkclay added a commit that referenced this pull request Sep 2, 2026
…st entry was a phantom

Part of #660. The sweep behind #651 left a machine-checkable list —
`BASELINE_BROKEN` in table-references-exist.test.ts, which reads the generated
types.ts and needs no database. It counted 32 broken references across 12 table
names. This takes it to 17 across 5.

**#656 — the O*NET router addressed the wrong schema entirely.** `core
.onet_occupations` and `core.onet_skills` exist in no schema; the data lives in
`onet`, whose key column is `onetsoc_code`, not `onet_code`. Four occupation
reads now select from `onet.occupation_data`, aliasing `onet_code:onetsoc_code`
so the response shape the SDK consumes does not change.

The skills endpoint needed more than a rename. `onet.skills` is raw O*NET
measure data — one row per (onetsoc_code, element_id, scale_id), scale "IM"
being importance and "LV" level — with no skill name and no `category` column,
where the handler assumed a flat table with `importance` and `category`. It now
reads the IM rows, resolves names from `onet.content_model_reference` in a
second query (there is no declared FK, so PostgREST cannot embed it), and
derives `category` from the element-id prefix, which is the actual O*NET
taxonomy. The wire fields `importance` and `category` are preserved.

**#657 — four wrong-schema references,** each verified against the running
database rather than only against types.ts:

    public.webhooks          exists;  core.webhooks              does not
    core.job_team_assignments exists; core.team_job_assignments  does not
    core.certifications      exists;  public.certifications      does not
    core.user_education      exists;  public.user_education      does not

The last two carried no `.schema()` call at all, so they defaulted to public
and returned `count: null` rather than erroring — which is why the university
student count silently read 0 for every university instead of failing.

**#657's fifth row was not a real defect.** office-communities.ts:146 already
read `.schema("core").from("users")`. The test reported it as `community.users`
because its parser buffers to the next semicolon and takes the *first*
`.schema()` in the statement — and that line sits in a `Promise.all([...])`
whose first element is `.schema("community")`. One statement, two chains, one
schema. The parser now takes the nearest `.schema()` *preceding* each
`.from()`, which still catches the office-users.ts delete-cascade bug it was
written for (those are separate statements, so each lands in its own buffer). A
regression test covers the multi-chain case, and the phantom entry is gone.

**#659 — a 500 where a 404 was already declared.** `GET /v1/user-profiles/
{userId}` used `.single()`, and PostgREST answers that with "Cannot coerce the
result to a single JSON object" on zero rows, which arrived as an error and was
reported as a server fault. A profile that does not exist is an ordinary
outcome, and a 500 makes the client retry something that can never succeed.
Now `.maybeSingle()` with an explicit 404, query errors still separate.

Verified: the reference test passes with 6 entries removed and the new
regression case; `deno check` over functions/api stays at 411 errors, so none
of this adds any.

Not verified: the O*NET tables are empty in the local seed, so the row-level
behaviour of the rewritten skills endpoint is unexercised. The shape is checked
against information_schema and the generated types; the scale ids come from the
O*NET content model. #656's "returns real rows for a seeded code" cannot be met
until that data is loaded.

Closes #656
Closes #657
Closes #659

Co-Authored-By: Claude Opus 5 <[email protected]>
thinkclay added a commit that referenced this pull request Sep 2, 2026
…st entry was a phantom

Part of #660. The sweep behind #651 left a machine-checkable list —
`BASELINE_BROKEN` in table-references-exist.test.ts, which reads the generated
types.ts and needs no database. It counted 32 broken references across 12 table
names. This takes it to 17 across 5.

**#656 — the O*NET router addressed the wrong schema entirely.** `core
.onet_occupations` and `core.onet_skills` exist in no schema; the data lives in
`onet`, whose key column is `onetsoc_code`, not `onet_code`. Four occupation
reads now select from `onet.occupation_data`, aliasing `onet_code:onetsoc_code`
so the response shape the SDK consumes does not change.

The skills endpoint needed more than a rename. `onet.skills` is raw O*NET
measure data — one row per (onetsoc_code, element_id, scale_id), scale "IM"
being importance and "LV" level — with no skill name and no `category` column,
where the handler assumed a flat table with `importance` and `category`. It now
reads the IM rows, resolves names from `onet.content_model_reference` in a
second query (there is no declared FK, so PostgREST cannot embed it), and
derives `category` from the element-id prefix, which is the actual O*NET
taxonomy. The wire fields `importance` and `category` are preserved.

**#657 — four wrong-schema references,** each verified against the running
database rather than only against types.ts:

    public.webhooks          exists;  core.webhooks              does not
    core.job_team_assignments exists; core.team_job_assignments  does not
    core.certifications      exists;  public.certifications      does not
    core.user_education      exists;  public.user_education      does not

The last two carried no `.schema()` call at all, so they defaulted to public
and returned `count: null` rather than erroring — which is why the university
student count silently read 0 for every university instead of failing.

**#657's fifth row was not a real defect.** office-communities.ts:146 already
read `.schema("core").from("users")`. The test reported it as `community.users`
because its parser buffers to the next semicolon and takes the *first*
`.schema()` in the statement — and that line sits in a `Promise.all([...])`
whose first element is `.schema("community")`. One statement, two chains, one
schema. The parser now takes the nearest `.schema()` *preceding* each
`.from()`, which still catches the office-users.ts delete-cascade bug it was
written for (those are separate statements, so each lands in its own buffer). A
regression test covers the multi-chain case, and the phantom entry is gone.

**#659 — a 500 where a 404 was already declared.** `GET /v1/user-profiles/
{userId}` used `.single()`, and PostgREST answers that with "Cannot coerce the
result to a single JSON object" on zero rows, which arrived as an error and was
reported as a server fault. A profile that does not exist is an ordinary
outcome, and a 500 makes the client retry something that can never succeed.
Now `.maybeSingle()` with an explicit 404, query errors still separate.

Verified: the reference test passes with 6 entries removed and the new
regression case; `deno check` over functions/api stays at 411 errors, so none
of this adds any.

Not verified: the O*NET tables are empty in the local seed, so the row-level
behaviour of the rewritten skills endpoint is unexercised. The shape is checked
against information_schema and the generated types; the scale ids come from the
O*NET content model. #656's "returns real rows for a seeded code" cannot be met
until that data is loaded.

Closes #656
Closes #657
Closes #659

Co-Authored-By: Claude Opus 5 <[email protected]>
thinkclay added a commit that referenced this pull request Sep 2, 2026
…st entry was a phantom

Part of #660. The sweep behind #651 left a machine-checkable list —
`BASELINE_BROKEN` in table-references-exist.test.ts, which reads the generated
types.ts and needs no database. It counted 32 broken references across 12 table
names. This takes it to 17 across 5.

**#656 — the O*NET router addressed the wrong schema entirely.** `core
.onet_occupations` and `core.onet_skills` exist in no schema; the data lives in
`onet`, whose key column is `onetsoc_code`, not `onet_code`. Four occupation
reads now select from `onet.occupation_data`, aliasing `onet_code:onetsoc_code`
so the response shape the SDK consumes does not change.

The skills endpoint needed more than a rename. `onet.skills` is raw O*NET
measure data — one row per (onetsoc_code, element_id, scale_id), scale "IM"
being importance and "LV" level — with no skill name and no `category` column,
where the handler assumed a flat table with `importance` and `category`. It now
reads the IM rows, resolves names from `onet.content_model_reference` in a
second query (there is no declared FK, so PostgREST cannot embed it), and
derives `category` from the element-id prefix, which is the actual O*NET
taxonomy. The wire fields `importance` and `category` are preserved.

**#657 — four wrong-schema references,** each verified against the running
database rather than only against types.ts:

    public.webhooks          exists;  core.webhooks              does not
    core.job_team_assignments exists; core.team_job_assignments  does not
    core.certifications      exists;  public.certifications      does not
    core.user_education      exists;  public.user_education      does not

The last two carried no `.schema()` call at all, so they defaulted to public
and returned `count: null` rather than erroring — which is why the university
student count silently read 0 for every university instead of failing.

**#657's fifth row was not a real defect.** office-communities.ts:146 already
read `.schema("core").from("users")`. The test reported it as `community.users`
because its parser buffers to the next semicolon and takes the *first*
`.schema()` in the statement — and that line sits in a `Promise.all([...])`
whose first element is `.schema("community")`. One statement, two chains, one
schema. The parser now takes the nearest `.schema()` *preceding* each
`.from()`, which still catches the office-users.ts delete-cascade bug it was
written for (those are separate statements, so each lands in its own buffer). A
regression test covers the multi-chain case, and the phantom entry is gone.

**#659 — a 500 where a 404 was already declared.** `GET /v1/user-profiles/
{userId}` used `.single()`, and PostgREST answers that with "Cannot coerce the
result to a single JSON object" on zero rows, which arrived as an error and was
reported as a server fault. A profile that does not exist is an ordinary
outcome, and a 500 makes the client retry something that can never succeed.
Now `.maybeSingle()` with an explicit 404, query errors still separate.

Verified: the reference test passes with 6 entries removed and the new
regression case; `deno check` over functions/api stays at 411 errors, so none
of this adds any.

Not verified: the O*NET tables are empty in the local seed, so the row-level
behaviour of the rewritten skills endpoint is unexercised. The shape is checked
against information_schema and the generated types; the scale ids come from the
O*NET content model. #656's "returns real rows for a seeded code" cannot be met
until that data is loaded.

Closes #656
Closes #657
Closes #659

Co-Authored-By: Claude Opus 5 <[email protected]>
thinkclay added a commit that referenced this pull request Sep 2, 2026
…st entry was a phantom (#711)

* fix(api): five routers addressed tables that do not exist, and one test entry was a phantom

Part of #660. The sweep behind #651 left a machine-checkable list —
`BASELINE_BROKEN` in table-references-exist.test.ts, which reads the generated
types.ts and needs no database. It counted 32 broken references across 12 table
names. This takes it to 17 across 5.

**#656 — the O*NET router addressed the wrong schema entirely.** `core
.onet_occupations` and `core.onet_skills` exist in no schema; the data lives in
`onet`, whose key column is `onetsoc_code`, not `onet_code`. Four occupation
reads now select from `onet.occupation_data`, aliasing `onet_code:onetsoc_code`
so the response shape the SDK consumes does not change.

The skills endpoint needed more than a rename. `onet.skills` is raw O*NET
measure data — one row per (onetsoc_code, element_id, scale_id), scale "IM"
being importance and "LV" level — with no skill name and no `category` column,
where the handler assumed a flat table with `importance` and `category`. It now
reads the IM rows, resolves names from `onet.content_model_reference` in a
second query (there is no declared FK, so PostgREST cannot embed it), and
derives `category` from the element-id prefix, which is the actual O*NET
taxonomy. The wire fields `importance` and `category` are preserved.

**#657 — four wrong-schema references,** each verified against the running
database rather than only against types.ts:

    public.webhooks          exists;  core.webhooks              does not
    core.job_team_assignments exists; core.team_job_assignments  does not
    core.certifications      exists;  public.certifications      does not
    core.user_education      exists;  public.user_education      does not

The last two carried no `.schema()` call at all, so they defaulted to public
and returned `count: null` rather than erroring — which is why the university
student count silently read 0 for every university instead of failing.

**#657's fifth row was not a real defect.** office-communities.ts:146 already
read `.schema("core").from("users")`. The test reported it as `community.users`
because its parser buffers to the next semicolon and takes the *first*
`.schema()` in the statement — and that line sits in a `Promise.all([...])`
whose first element is `.schema("community")`. One statement, two chains, one
schema. The parser now takes the nearest `.schema()` *preceding* each
`.from()`, which still catches the office-users.ts delete-cascade bug it was
written for (those are separate statements, so each lands in its own buffer). A
regression test covers the multi-chain case, and the phantom entry is gone.

**#659 — a 500 where a 404 was already declared.** `GET /v1/user-profiles/
{userId}` used `.single()`, and PostgREST answers that with "Cannot coerce the
result to a single JSON object" on zero rows, which arrived as an error and was
reported as a server fault. A profile that does not exist is an ordinary
outcome, and a 500 makes the client retry something that can never succeed.
Now `.maybeSingle()` with an explicit 404, query errors still separate.

Verified: the reference test passes with 6 entries removed and the new
regression case; `deno check` over functions/api stays at 411 errors, so none
of this adds any.

Not verified: the O*NET tables are empty in the local seed, so the row-level
behaviour of the rewritten skills endpoint is unexercised. The shape is checked
against information_schema and the generated types; the scale ids come from the
O*NET content model. #656's "returns real rows for a seeded code" cannot be met
until that data is loaded.

Closes #656
Closes #657
Closes #659

Co-Authored-By: Claude Opus 5 <[email protected]>

* test(api): record #703's moderation refs as types-staleness, not broken routes

The reference test fails on unmodified main: routes/moderation.ts reads
core.content_reports and core.user_blocks, and neither is in types.ts. Both
tables exist in the database — #703 shipped their migrations without
regenerating the checked-in types, so the routes are right and the catalogue is
stale.

Regenerating is not the fix and was tried: `supa:generate` against the local
stack adds the three expected objects and also deletes the application_status
enum, which the local database does not have and types.ts does. See #707.

Listed rather than excused, so the count still only goes down — once types.ts is
regenerated these go stale and the test demands their removal.

Refs #707

Co-Authored-By: Claude Opus 5 <[email protected]>

---------

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