cli icon indicating copy to clipboard operation
cli copied to clipboard

fix: automatically set default schema env var for type generation

Open bug-author opened this issue 9 months ago • 2 comments

What kind of change does this PR introduce?

This PR lets the CLI automatically detect when exactly one non‑public schema is provided and sets the environment variable PG_META_GENERATE_TYPES_DEFAULT_SCHEMA accordingly.

What is the current behavior?

Currently, when running supabase gen types, the CLI passes the schema flags directly to postgres‑meta without automatically setting a default schema. This results in the generated TypeScript file always defaulting to the "public" alias:

export type PublicSchema = Database[Extract<keyof Database, "public">]

Even if a non‑public schema is specified (for example, --schema robo), helper types (like Tables<"test_table">) incorrectly reference the public schema.

Relevant issues: #3061 #3146

What is the new behavior?

The CLI now automatically checks if exactly one schema is provided (and if that schema is not "public"). If so, it sets the environment variable PG_META_GENERATE_TYPES_DEFAULT_SCHEMA to that schema's name. This allows postgres‑meta to generate a default alias based on the provided schema (for example, for "robo", the generated file will include):

export type DefaultSchema = Database[Extract<keyof Database, "robo">]
export type DefaultSchemaOrPublic = DefaultSchema

In single‑schema mode, shorthand type references (like Tables<"test_table">) correctly resolve to the non‑public schema. In multi‑schema scenarios, the variable is not set, and users must explicitly specify the schema for non‑public tables.

Additional context

Corresponding postgres-meta PR

bug-author avatar Mar 03 '25 21:03 bug-author

Pull Request Test Coverage Report for Build 15540005669

Details

  • 44 of 48 (91.67%) changed or added relevant lines in 3 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.1%) to 55.168%

Changes Missing Coverage Covered Lines Changed/Added Lines %
cmd/gen.go 0 2 0.0%
internal/testing/apitest/docker.go 26 28 92.86%
<!-- Total: 44 48
Files with Coverage Reduction New Missed Lines %
internal/gen/keys/keys.go 5 12.9%
<!-- Total: 5
Totals Coverage Status
Change from base Build 15538409387: 0.1%
Covered Lines: 6005
Relevant Lines: 10885

💛 - Coveralls

coveralls avatar Mar 03 '25 21:03 coveralls

I've added 4 tests as well to test the core feature, backward compatibility and edge cases.

bug-author avatar Jun 09 '25 13:06 bug-author