Skip to content

Conversation

@7ttp
Copy link
Contributor

@7ttp 7ttp commented Nov 25, 2025

Problem

When using extensions like ltree in the extensions schema, migrations can fail with cryptic errors:

ERROR: type "ltree" does not exist (SQLSTATE 42704)

This error is confusing because:

  • It doesn't explain why the type isn't found
  • Users don't know that schema-qualified references are needed
  • Local and remote databases may have different search_path settings

Solution

Improve the CLI error message by inspecting the pgconn.PgError object and providing a helpful hint when extension types are not found.

When SQLSTATE 42704 (undefined_object) is returned for a missing type, the CLI now displays:

Hint: This type may be defined in a schema that's not in your search_path. Use schema-qualified type references to avoid this error: CREATE TABLE example (col extensions.ltree); Learn more: supabase migration new --help

Changes

  • Modified pkg/migration/file.go to detect extension type errors via pgx error inspection
  • Added extractTypeName() helper to parse type name from error message
  • Provides dynamic hint with the actual type name (e.g., extensions.ltree)

Testing

  • Inspects pgconn.PgError.Code for SQLSTATE 42704
  • Checks error message contains "type" and "does not exist"
  • Extracts type name using regex for accurate hint

Related

Addresses feedback from supabase/supabase#40781

@7ttp 7ttp requested a review from a team as a code owner November 25, 2025 20:46
@7ttp 7ttp changed the base branch from main to develop November 25, 2025 20:51
@7ttp 7ttp marked this pull request as draft November 25, 2025 21:01
@7ttp
Copy link
Contributor Author

7ttp commented Nov 26, 2025

@sweatybridge can you confirm if adding extensions to the search_path after each RESET ALL is the right approach to keep types like ltree accessible during migrations? Just want to make sure this aligns with expected behavior before taking the PR out of draft. If you have any suggestions, I’m happy to adjust.

@sweatybridge
Copy link
Contributor

Thanks for checking. Our current recommendation is to always use schema qualified references in your migration file. For eg.

CREATE TABLE test (path extensions.ltree NOT NULL);

This avoids ambiguity in default search path resolution which can be configured per session, per role, or per cluster.

The default search path for Supabase postgres role is currently set to $user, public, extensions. We can't guarantee that it won't be changed in the future. Therefore, I think it's not ideal to make such assumptions in the CLI migration tool.

@7ttp
Copy link
Contributor Author

7ttp commented Nov 26, 2025

Thanks for checking. Our current recommendation is to always use schema qualified references in your migration file. For eg.

CREATE TABLE test (path extensions.ltree NOT NULL);

This avoids ambiguity in default search path resolution which can be configured per session, per role, or per cluster.

The default search path for Supabase postgres role is currently set to $user, public, extensions. We can't guarantee that it won't be changed in the future. Therefore, I think it's not ideal to make such assumptions in the CLI migration tool.

@sweatybridge Thanks for the elaboration! I get that relying on a fixed search_path isn’t future-proof.
That said, the current type "ltree" does not exist errors are pretty opaque especially since local and remote migrations behave differently.
instead I can:
update docs to recommend schema qualified types
improve the error message when an extension type isn’t found
optionally add a CLI hint suggesting extensions.ltree
Let me know if that approach is fine and frameable for a pr, I can revise this patch accordingly.

@sweatybridge
Copy link
Contributor

improve the error message when an extension type isn’t found
optionally add a CLI hint suggesting extensions.ltree

Do you plan to do this by inspecting the error object returned from pgx? If so, that sounds good to me.

Thanks for your help.

@7ttp
Copy link
Contributor Author

7ttp commented Nov 26, 2025

improve the error message when an extension type isn’t found
optionally add a CLI hint suggesting extensions.ltree

Do you plan to do this by inspecting the error object returned from pgx? If so, that sounds good to me.

Thanks for your help.

Yep, I’ll inspect the pgx error and add the clearer message + schema qualification hint. On it

@7ttp 7ttp force-pushed the fix/ltree-extension-search-path branch from b68f5bb to 1b972d2 Compare November 26, 2025 10:40
@coveralls
Copy link

coveralls commented Nov 26, 2025

Pull Request Test Coverage Report for Build 19741472306

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.03%) to 55.54%

Files with Coverage Reduction New Missed Lines %
internal/gen/keys/keys.go 5 12.9%
Totals Coverage Status
Change from base Build 19732396170: -0.03%
Covered Lines: 6722
Relevant Lines: 12103

💛 - Coveralls

Inspect pgx errors for SQLSTATE 42704 (undefined_object) and provide
helpful hints when extension types are not found. The error message now:
- Detects 'type does not exist' errors
- Extracts the type name from the error message
- Suggests using schema-qualified references (e.g., extensions.ltree)
- Provides a concrete example in the error output

This addresses the issue where migrations work locally but fail remotely
with opaque 'type does not exist' errors, making it clear to users that
they should use schema-qualified type references instead of relying on
search_path settings.
@7ttp 7ttp force-pushed the fix/ltree-extension-search-path branch from 1b972d2 to 711463f Compare November 26, 2025 11:29
@7ttp 7ttp changed the title fix: ensure extensions schema is accessible during migration execution feat: improve error messages for undefined extension types Nov 26, 2025
@7ttp 7ttp changed the title feat: improve error messages for undefined extension types feat: add schema qualification hints for extension type errors Nov 26, 2025
@7ttp 7ttp marked this pull request as ready for review November 26, 2025 13:14
@7ttp
Copy link
Contributor Author

7ttp commented Nov 27, 2025

@sweatybridge whenever you have a moment, a quick review would be appreciated. 💚

Copy link
Contributor

@sweatybridge sweatybridge left a comment

Choose a reason for hiding this comment

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

Just a few nitpicks

@7ttp
Copy link
Contributor Author

7ttp commented Nov 27, 2025

@sweatybridge All requested changes have been applied. Ready for rereview. 💚

@7ttp 7ttp requested a review from sweatybridge November 27, 2025 15:49
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