Repository navigation
test(web): fail on an embed between twice-joined tables that doesn't name its foreign key - #1714
Conversation
…name its foreign key The plan check before every write embedded organizations from profiles without naming the relationship; with a second foreign key between the tables PostgREST refused it, and every write failed (62eb7b7). This reads the foreign keys from schema.sql and every .from().select() in the web app and the API server, and names each embed that would be refused. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good, with follow-ups
Reviewed 3f17586 (two commits on top of 62eb7b7). One new test file, no migration, no app code.
What I checked
-
It catches the bug it targets. With 62eb7b7 reverted locally, the test fails and names exactly the two lines that broke:
web/src/lib/actions/organization.ts:197 profiles → organizationsweb/src/lib/subscription-guard.ts:79 profiles → organizations
-
It passes on main. I merged ce9a3dc (the Framer CMS work included) into the branch: 3/3 pass. No other unnamed embed between twice-joined tables exists today, in
web/srcorserver/src. -
The foreign-key reading. It finds 211 public foreign keys in
supabase/schema.sql. I counted them separately with a plain script and got the same 211 and the same four twice-joined pairs:organizations | profilescontent_drafts | profilescontent_authors | profilescontent_authors | content_drafts
Keys to
auth.usersare skipped, as they should be. No migration renames a table, column or constraint, so not handlingrenamedoesn't matter today. -
The select walk. Aliases (
org:organizations!…(…)), spreads, nested embeds checked against their own parent table, and!inner/!leftnot counted as naming the key all behave as described. A column hint (profiles!created_by(…)) and an embed by column name (author:created_by(…)) count as named, which matches what PostgREST accepts. -
It runs in CI.
ci.ymlrunsyarn testinweb/, and the schema check rebuildsschema.sqlfirst, so the guard reads the current schema.typescriptis already a web devDependency. -
Time. About 2 s on an idle machine and 13.5 s under heavy load. The 30 s timeout is enough.
-
eslint and prettier are clean on the file.
tscis clean on the branch merged with main.
Follow-ups
- A select string held in a constant is skipped:
.select(DRAFT_COLUMNS)inweb/src/lib/mcp/content.ts(readscontent_drafts) and.select(AUTHOR_COLUMNS)inweb/src/lib/actions/content-authors.ts(readscontent_authors). Both tables are in a twice-joined pair. Neither constant embeds across one today, so the test isn't wrong now. Resolving a same-fileconst X = '…'identifier to its string would close the gap cheaply. There are about 15 such selects today. - Embeds through views aren't covered (PostgREST infers relationships through them too). Worth a line next to the many-to-many note.
Summary
A guard so the outage fixed in 62eb7b7 can't come back unnoticed.
The read-only check before every write embedded
organizations(…)fromprofileswithout naming the relationship. Two foreign keys join those tables (profiles.organization_id, andorganizations.anthropic_api_key_set_byback to profiles), so PostgREST refused the embed as ambiguous. The check then threw, and every write failed for every workspace, active plans included.web/src/lib/supabase/embed-hints.test.ts:supabase/schema.sql. It follows the migrations in order, so dropped columns, constraints and tables don't count;.from('…').select('…')inweb/srcandserver/srcwith the TypeScript parser, walking the select's embeds with their nesting, so a nested embed is checked against its own parent table;organizations!profiles_organization_id_fkey(…)).!inner/!leftonly pick the join type, so they don't count as naming it.Checked against the bug: with 62eb7b7 reverted locally, the test fails and names exactly the two lines that broke:
On main it passes: no other ambiguous embed exists today.
Not covered: many-to-many embeds through a junction table (PostgREST's own rules there are narrower), and select strings built at runtime (template literals with substitutions). Neither has caused this so far.
Related issue
None (follow-up to 62eb7b7).
Type of change
test— TestsHow to test
cd web && npx vitest run src/lib/supabase/embed-hints.test.ts: 3 tests pass.web/src/lib/subscription-guard.ts, changeorganizations!profiles_organization_id_fkey(back toorganizations(and run it again: the test fails and points at that line.Checklist
eslint+prettieron the new file,tsc --noEmitpasses; the new test passes (3/3) and fails on the reverted bugyarn test/yarn build: not run; the change is one new test file, and CI runs both🤖 Generated with Claude Code