Skip to content

test(web): fail on an embed between twice-joined tables that doesn't name its foreign key - #1714

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/ambiguous-embed-guard
Oct 29, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:fix/ambiguous-embed-guard

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

A guard so the outage fixed in 62eb7b7 can't come back unnoticed.

The read-only check before every write embedded organizations(…) from profiles without naming the relationship. Two foreign keys join those tables (profiles.organization_id, and organizations.anthropic_api_key_set_by back 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:

  • reads the foreign keys between public tables from supabase/schema.sql. It follows the migrations in order, so dropped columns, constraints and tables don't count;
  • finds every table pair joined by more than one foreign key, in either direction;
  • reads every .from('…').select('…') in web/src and server/src with the TypeScript parser, walking the select's embeds with their nesting, so a nested embed is checked against its own parent table;
  • fails on an embed between such a pair that doesn't name its foreign key (organizations!profiles_organization_id_fkey(…)). !inner / !left only 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:

web/src/lib/actions/organization.ts:197 profiles → organizations
web/src/lib/subscription-guard.ts:79 profiles → organizations

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 — Tests

How to test

  1. cd web && npx vitest run src/lib/supabase/embed-hints.test.ts: 3 tests pass.
  2. In web/src/lib/subscription-guard.ts, change organizations!profiles_organization_id_fkey( back to organizations( and run it again: the test fails and points at that line.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: eslint + prettier on the new file, tsc --noEmit passes; the new test passes (3/3) and fails on the reverted bug
  • Full yarn test / yarn build: not run; the change is one new test file, and CI runs both

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 28, 2026 23:08
…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]>
This was referenced Oct 28, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 → organizations
    • web/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/src or server/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 | profiles
    • content_drafts | profiles
    • content_authors | profiles
    • content_authors | content_drafts

    Keys to auth.users are skipped, as they should be. No migration renames a table, column or constraint, so not handling rename doesn't matter today.

  • The select walk. Aliases (org:organizations!…(…)), spreads, nested embeds checked against their own parent table, and !inner/!left not 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.yml runs yarn test in web/, and the schema check rebuilds schema.sql first, so the guard reads the current schema. typescript is 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. tsc is clean on the branch merged with main.

Follow-ups

  1. A select string held in a constant is skipped: .select(DRAFT_COLUMNS) in web/src/lib/mcp/content.ts (reads content_drafts) and .select(AUTHOR_COLUMNS) in web/src/lib/actions/content-authors.ts (reads content_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-file const X = '…' identifier to its string would close the gap cheaply. There are about 15 such selects today.
  2. Embeds through views aren't covered (PostgREST infers relationships through them too). Worth a line next to the many-to-many note.

@gkhngyk
gkhngyk merged commit b77c9c1 into ansvisor:main Oct 29, 2026
4 of 5 checks passed
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.

2 participants