supabase / cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
https://supabase.com/docs/reference/cli/about
MIT License
1.02k stars 201 forks source link

The --db-url arg is not respected by `supabase db reset` when host is localhost #1956

Closed djbutler closed 7 months ago

djbutler commented 7 months ago

Describe the bug

The rest of the --db-url connection string is ignored if the host is localhost. In particular, the port and database name are ignored. This is a problem if you need to direct the CLI to a Postgres connection string that is local but not the default connection string. This is the case for services like Electric SQL (https://electric-sql.com/docs/quickstart), which runs a local Postgres proxy with a different connection string.

To Reproduce Steps to reproduce the behavior:

  1. Run npx create-electric-app@latest my-app
  2. Run cd my-app && npx electric-sql start --with-postgres
  3. Run cd my-app && supabase init
  4. Run supabase migration new my_new_migration.
  5. Run supabase db reset --db-url postgresql://postgres:proxy_password@localhost:65432/postgres
  6. Run supabase db reset --db-url postgresql://postgres:proxy_password@localhost:65432/foobar

Expected behavior Step 5 should connect to the --db-url string, but it connects to the default local database. Step 6 should fail entirely, but it doesn't fail. This shows that the connection string is being ignored.

Screenshots N/A

System information Rerun the failing command with --create-ticket flag (preferred).

djbutler commented 7 months ago

This is related to #480 .

sweatybridge commented 7 months ago

Resetting the local database requires recreating the database container because some extensions like pg_cron need to be restarted that way. If you are only interested in running migrations, perhaps you can try supabase migration up --db-url?

sweatybridge commented 7 months ago

This should be fixed in beta release, ie. npx supabase@beta db reset --db-url postgresql://postgres:proxy_password@localhost:65432/postgres

djbutler commented 7 months ago

@sweatybridge Thanks for getting to this so quickly. Now the CLI correctly recognizes the --db-url as a remote Supabase instance, but the migrations themselves are failing. This is what I get:


➜  desktop-sync git:(main) npx supabase@beta db reset --db-url postgresql://postgres:proxy_password@localhost:65432/postgres
Need to install the following packages:
  supabase@1.145.2
Ok to proceed? (y) y
Confirm resetting the remote database? [Y/n] Y
Resetting remote database...
Applying migration 20240210010157_create_phenotypers_table.sql...
unexpected EOF                                          
At statement 0: CREATE TABLE IF NOT EXISTS phenotypers (
  id      UUID PRIMARY KEY NOT NULL,                    
  name    TEXT,                                         
  email   TEXT                                          
)                                                       
Try rerunning the command with --debug to troubleshoot the error.
➜  desktop-sync git:(main) npx supabase@beta db reset --db-url postgresql://postgres:proxy_password@localhost:65432/postgres
Confirm resetting the remote database? [Y/n] Y
Resetting remote database...
failed to list schemas: unexpected EOF
Try rerunning the command with --debug to troubleshoot the error.```

Should I open a new issue?
sweatybridge commented 7 months ago

Should I open a new issue?

Yup, please also share the output with --debug flag to help diagnose.