Repository navigation
fix(postgrest-typegen): resolve composite args on foreign tables and matviews - #143
Merged
Merged
Conversation
…matviews pgTypeToTsType resolved a relation-typed value against tables and views only, so a value typed as a foreign table or as a materialized view fell through to unknown. Both are generated (foreign tables under Tables, materialized views under Views) and have a Row to point at, so the lookups now include them. The option bag gains foreignTables and materializedViews as optional fields, keeping the exported signature backwards compatible, and the eight identical inline context literals inside generateTypescript collapse into one shared object so the collections cannot drift apart again.
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Team Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…re kind Review of the previous commit turned up a regression it introduced. The resolver settled the relation kind before the schema, so once foreign tables joined the table-like bucket a foreign table in an unrelated schema outranked the view in the schema the caller actually meant: before: Database["public"]['Views']["foo"]['Row'] after: Database["other"]['Tables']["foo"]['Row'] Match on the preferred schema across every relation kind first, then fall back to kind order. This also fixes the pre-existing table-versus-view form of the same bug, which was unreachable in practice until now. Two smaller things from the same pass: pgTypeToTsType takes the context as one value rather than destructuring it in the signature, so the array-element recursion forwards it directly instead of rebuilding a literal that a later edit could silently drop a field from, and the relation lookup no longer spreads both collections into a throwaway array on every call.
…schema A second review pass on the previous two commits turned up a worse case of the same root cause. Making the preferred schema decide before the relation kind only helps if the preferred schema is right, and function argument and return types never passed one, so the resolver fell back to guessing the schema being generated. That guess now actively misresolved an argument genuinely typed other.foo to a same-named relation in public, where the old kind-first order had got it right by accident. Those six call sites already hold the resolved PostgresType, which carries the schema that owns it, so they pass it. The guess becomes an exact match and the preferred-schema rule is sound rather than amplifying a bad signal. No effect on the parity goldens, which have no cross-schema name collisions. Also export TypeResolutionContext. Naming the parameter type in the previous commit left the exported pgTypeToTsType signature referring to a type consumers cannot name, where it used to be an inline literal; GenerateTypescriptOptions is exported for the same reason.
mandarini
approved these changes
Sep 2, 2026
spydon
added a commit
that referenced
this pull request
Sep 3, 2026
…backs (#144) Closes SDK-1720. ## What Four branches of the TypeScript generator produced real, user-visible output that no test asserted. Tests only, no source or golden changes. **Three are one theme: a type owned by a schema that is not being generated.** A column can reference an enum, a composite type, or a relation living in a schema the caller did not ask for. Emitting `Database["other"][...]` there would dangle, since no `other` key is written, so each kind degrades instead: | Referenced kind | Fallback | Why | | --- | --- | --- | | enum | inlines its variants, `"happy" \| "sad"` | the values travel with the type, so nothing is lost | | composite type | `unknown` | its shape lives in the schema that was skipped | | relation | `unknown` | same | **The fourth is unrelated but found the same way:** a non-updatable column on a writable view has to be spelled `?: never` in `Update`, so writing one is a type error rather than a runtime rejection. The `Insert` side and both table equivalents were already covered; only the view `Update` path was not. ## Every assertion is mutation-verified Each test was checked by inverting the branch it targets and confirming the test fails. That was not paranoia here: > Bun's coverage stopped reporting line 897 as uncovered **merely because an unrelated test was added**, while mutating that line still broke nothing. It attributes the four identical `?: never` lines unreliably. So the coverage delta below is reported for context, not as the evidence. Mutation testing is the evidence. ``` before src/generation/typescript.ts 97.50% funcs 98.14% lines after src/generation/typescript.ts 98.75% funcs 98.88% lines ``` Of the three lines still listed as uncovered, `55-59` is the `oxfmt` failure throw, `304-305` is one arm of the PostgREST-resolvability filter, and `469` is the `unknown` fallback when a function's return type id misses. Those are follow-up material, not part of this change. ## Testing 131 pass, 0 fail against a real database, parity gate included. `check-types`, `oxfmt`, `oxlint`, `knip`, and `build` all clean. Goldens untouched, as expected for a test-only change. ## Context Came out of an audit prompted by #143, which shipped a resolver whose `unknown` branch had no test. Sibling ticket SDK-1721 covers the Swift generator's scalar mapping table, where `Bool`, `Int16`, `Float`, and `Double` are similarly unexercised.
spydon
pushed a commit
that referenced
this pull request
Sep 15, 2026
🤖 I have created a release *beep* *boop* --- ## [0.2.2](postgrest-typegen-v0.2.1...postgrest-typegen-v0.2.2) (2026-09-15) ### Bug Fixes * **postgrest-typegen:** order generated output with a fixed collation ([#181](#181)) ([af7f8e8](af7f8e8)) * **postgrest-typegen:** resolve composite args on foreign tables and matviews ([#143](#143)) ([e422eb7](e422eb7)) * **postgrest-typegen:** update-only trigger views, PG18 virtual generated columns, legacy metadata compatibility ([#128](#128)) ([50661bd](50661bd)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
7 tasks done
1 task done
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.
What
pgTypeToTsTyperesolved a relation-typed value againsttablesandviewsonly, neverforeignTablesormaterializedViews. Both of those are generated (foreign tables underTables, materialized views underViews) and have aRowto point at, so an argument or column typed as one fell through tounknown:Follow-up to #137, where this was surfaced during review and deliberately left out to keep that change focused.
How
The
pgTypeToTsTypecontext gainsforeignTablesandmaterializedViews. Both are optional, so the exported signature stays backwards compatible (postgres-meta calls it directly). The context type is named and exported asTypeResolutionContext, matching howGenerateTypescriptOptionsis exported.generateTypescripthad the same{ types, schemas, tables, views }literal repeated at thirteen call sites. Those collapse into onetypeContextbuilt once, which is what keeps the two new collections from being threaded through some call sites and not others.Resolution order (behaviour change)
Adding two collections exposed a latent ordering bug, so this also changes how same-named relations are disambiguated. Two linked fixes:
1. Schema before kind. The resolver settled the relation kind before the schema: it took the first table-like match by name and only consulted
preferredSchemawithin that bucket. Harmless while just tables and views were consulted, but once foreign tables joined the table-like bucket, a foreign table in an unrelated schema outranked the view the caller actually meant:It now matches on the preferred schema across every relation kind first, then falls back to kind order. This also fixes the pre-existing table-versus-view form of the same bug.
2. The preferred schema has to be right. Fix 1 only helps if
preferredSchemais accurate, and function argument and return types never passed one, so the resolver fell back to guessing the schema being generated. That guess then actively misresolved an argument genuinely typedother.footo a same-named relation inpublic, where the old kind-first order had got it right by accident. Those six call sites already hold the resolvedPostgresType, which carries the schema that owns it, so they now pass it. Columns already did this viatype_schema.Both only affect databases with the same relation or type name in more than one generated schema; the parity goldens are unchanged by either.
Scope of the output change
Only TypeScript is affected. Go, Python, and Swift emit no functions at all, and their goldens are byte-identical.
The parity golden moves by 10 lines, and notably
get_todos_by_matviewwas already in the fixture and already wrong, so this is not only about newly reachable cases.Testing
Each test below was verified to fail without its corresponding fix:
Rowrather thanunknown.generateTypescriptso the call-site wiring is covered, not just the resolver.todos_matview_label(mv public.todos_matview)pins the materialized view path through the parity golden. The foreign table path was already pinned byforeign_table_label, added in fix(postgrest-typegen): match computed fields by argument type, not name #137.baseForeignTablebuilder totest/generation/fixtures.ts, matching the existingbaseView/baseMaterializedViewpattern.Full suite against a real database: 129 pass, 0 fail, parity gate included.
check-types,oxfmt,oxlint,knip, andbuildall clean.Note for the release
This changes generated output, so the nightly
Parity vs. real postgres-metajob stays red until a release ships and postgres-meta bumps its lockfile and regenerates the inline snapshots intest/server/typegen.ts. Same situation as #137, and the two can be handled in one postgres-meta pull request.