[PR #8051] [MERGED] Fix: Handle escaped $user in Supabase PostgreSQL search_path #24612

Closed
opened 2026-04-15 22:28:14 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8051
Author: @Bekacru
Created: 2/18/2026
Status: Merged
Merged: 2/18/2026
Merged by: @Bekacru

Base: canaryHead: fix/issue-8049


📝 Commits (2)

  • 6196736 fix: handle escaped $user in Supabase PostgreSQL search_path
  • 7b71cc2 fix: restore full file with escaped $user fix for Supabase

📊 Changes

1 file changed (+3 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/db/get-migration.ts (+3 -1)

📄 Description

Summary

Fixes #8049

The CLI's generate and migrate commands failed to correctly identify the "public" schema in Supabase PostgreSQL databases due to how Supabase returns the search_path.

Problem

When querying SHOW search_path;:

  • Standard PostgreSQL returns: "$user", public, extensions
  • Supabase returns: "\$user", public, extensions (note the escaped backslash)

The existing code at packages/better-auth/src/db/get-migration.ts filtered out variable references by checking if the schema name started with $, but didn't account for the escaped \$ prefix that Supabase returns.

This caused:

  • The schema detection to incorrectly use $user as the target schema
  • Debug logs showing Schema '$user' does not exist
  • Migration files generating CREATE TABLE statements instead of ALTER TABLE statements for existing tables

Fix

Updated the filter in getPostgresSchema() to also check for the escaped \$ pattern:

.filter((s) => !s.startsWith("$") && !s.startsWith("\\$"));

This ensures that both "$user" and "\$user" are correctly identified as variable references and skipped, allowing the "public" schema to be selected as intended.

Testing

Tested with Supabase PostgreSQL connection strings using the default postgres user without modified search_path. The CLI now correctly identifies the "public" schema and generates appropriate migration files.

Fixes #8049


Summary by cubic

Fix Supabase Postgres search_path handling by skipping both "$user" and "$user" and restoring get-migration.ts with only this change, ensuring the CLI selects the public schema and generates ALTER statements instead of CREATE for existing tables.

Written for commit 7b71cc2945. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8051 **Author:** [@Bekacru](https://github.com/Bekacru) **Created:** 2/18/2026 **Status:** ✅ Merged **Merged:** 2/18/2026 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/issue-8049` --- ### 📝 Commits (2) - [`6196736`](https://github.com/better-auth/better-auth/commit/6196736a6270ec908340651e2ec37938716769cd) fix: handle escaped $user in Supabase PostgreSQL search_path - [`7b71cc2`](https://github.com/better-auth/better-auth/commit/7b71cc2945bcc928a562c30416165eb4f386c333) fix: restore full file with escaped $user fix for Supabase ### 📊 Changes **1 file changed** (+3 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/get-migration.ts` (+3 -1) </details> ### 📄 Description ## Summary Fixes #8049 The CLI's `generate` and `migrate` commands failed to correctly identify the "public" schema in Supabase PostgreSQL databases due to how Supabase returns the `search_path`. ## Problem When querying `SHOW search_path;`: - **Standard PostgreSQL** returns: `"$user", public, extensions` - **Supabase returns**: `"\$user", public, extensions` (note the escaped backslash) The existing code at [`packages/better-auth/src/db/get-migration.ts`](../blob/fix/issue-8049/packages/better-auth/src/db/get-migration.ts#L109-L110) filtered out variable references by checking if the schema name started with `$`, but didn't account for the escaped `\$` prefix that Supabase returns. This caused: - The schema detection to incorrectly use `$user` as the target schema - Debug logs showing `Schema '$user' does not exist` - Migration files generating `CREATE TABLE` statements instead of `ALTER TABLE` statements for existing tables ## Fix Updated the filter in `getPostgresSchema()` to also check for the escaped `\$` pattern: ```typescript .filter((s) => !s.startsWith("$") && !s.startsWith("\\$")); ``` This ensures that both `"$user"` and `"\$user"` are correctly identified as variable references and skipped, allowing the "public" schema to be selected as intended. ## Testing Tested with Supabase PostgreSQL connection strings using the default `postgres` user without modified `search_path`. The CLI now correctly identifies the "public" schema and generates appropriate migration files. Fixes #8049 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fix Supabase Postgres search_path handling by skipping both "$user" and "\$user" and restoring get-migration.ts with only this change, ensuring the CLI selects the public schema and generates ALTER statements instead of CREATE for existing tables. <sup>Written for commit 7b71cc2945bcc928a562c30416165eb4f386c333. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:28:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#24612