Repository navigation
test(api): fail when a route reads a table that does not exist - #651
Merged
Merged
Conversation
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]>
This was referenced Aug 27, 2026
Closed
This was referenced Aug 27, 2026
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]>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The pattern
This defect has been found one endpoint at a time, from 500s in a browser, for months:
core.inquiriesdoes not existcore.organizations— five columns that do not existcore.employers— a table in no schemacore.*tablesEach 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.skills.tswebhooks.tsccpa.tsinquiries.tsoffice-users.tsoffice-certifications.tsonet.tsoffice-communities.tsprofile-import.tsoffice-universities.tsteams.tsorganizations.ts/profile-completion.ts/profiles.tsA 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_BROKENrecords 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 sixcoredeletes: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.tscannot be judged.logs.user_feedbackis real in the database butlogsis 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.tscannot 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