Skip to content

fix: automatically set default schema env var for type generation #3243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

bug-author
Copy link

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

@coveralls
Copy link

coveralls commented Mar 3, 2025

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%
Files with Coverage Reduction New Missed Lines %
internal/gen/keys/keys.go 5 12.9%
Totals Coverage Status
Change from base Build 15538409387: 0.1%
Covered Lines: 6005
Relevant Lines: 10885

💛 - Coveralls

var defaultSchemaEnv string
if len(schemas) == 1 && schemas[0] != "public" {
defaultSchemaEnv = schemas[0]
}
Copy link
Contributor

@sweatybridge sweatybridge May 16, 2025

Choose a reason for hiding this comment

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

We should add a new flag so it doesn't break existing users that rely on the generated default schema. For eg. supabase gen types --schema private --set-default

Copy link
Author

Choose a reason for hiding this comment

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

@sweatybridge Thanks. I've implemented a set-default flag for backwards compatibility that makes the behavior now explicit and opt-in. The new behavior is now:

supabase gen types --schema private --set-default

@bug-author
Copy link
Author

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

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.

3 participants