electric-sql / electric

Sync little subsets of your Postgres data into local apps and services.
https://electric-sql.com
Apache License 2.0
6.41k stars 154 forks source link

electric-sql generate `PROXY_URL` always the same #911

Closed digoburigo closed 6 months ago

digoburigo commented 9 months ago

When trying the tutorial for manually installing ElectricSQL when I try to use npx electric-sql generate it's always pointing to this URL postgresql://prisma:********@localhost:65432/electric-app.

Tried to use the --proxy flag on the CLI and setting the ELECTRIC_PROXY env and none of them work.

The only one thing that I was able to change was the database name that is linked to the package.json name conifiguration.

Is there a bug with this or maybe I'm missing something. Can someone help me with this?

using watch mode

npx electric-sql generate -w -p postgresql://postgres:proxy_password@localhost:65432/postgres
Generating Electric client...
Service URL: http://localhost:5133
Proxy URL: postgresql://prisma:********@localhost:65432/electric-app
linear[bot] commented 9 months ago

VAX-1612 electric-sql generate `PROXY_URL` always the same

kevin-dp commented 9 months ago

Hi @digoburigo, i have encountered a similar issue and flagged it up. There is a workaround that works on my machine which is to define the environment variables in a file called .env in the root folder of your project (i.e. in the same directory as where your package.json resides). And then calling npx electric-sql generate -w should take into account the environment variables.

samwillis commented 9 months ago

@digoburigo, I've just been looking into this. There are two things happening in the copied command and output (also copied below)

npx electric-sql generate -w -p postgresql://postgres:proxy_password@localhost:65432/postgres 
Generating Electric client...
Service URL: http://localhost:5133
Proxy URL: postgresql://prisma:********@localhost:65432/electric-app
  1. The username is hard coded to prisma in the generator, as it puts the proxy it into a special introspection mode. So this is correct.

  2. The database name is incorrect, and from some initial testing does seem to not be carried through correctly.

Were those the two things you noticed?

I have tested changing the hostname and port, they are correctly handled.

I need to check with @magnetised, but I believe the database name is ignored by the proxy as electric is only connecting to a single database. With those two things, I think the generator command should have worked as copied.

The bug that @kevin-dp found is related to the start command with the --image or ELECTRIC_IMAGE env var and I believe is unrelated.

kevin-dp commented 9 months ago

@samwillis indeed, a quick test confirms that the host and port are correctly fetched from the argument and it always uses the project's name as the database name but as you said, i believe that the proxy ignores the database name anyway.

alco commented 9 months ago

the proxy ignores the database name anyway

I can confirm that.

digoburigo commented 9 months ago

With this env ELECTRIC_DATABASE_NAME I was able to configure the database name correctly. But still I'm not understanding the prisma user (tried to create the user in the database but with no success too) The error is this:

Generating Electric client...
Service URL: http://localhost:5133
Proxy URL: postgresql://prisma:********@localhost:65432/postgres
Error: Command failed: node /Users/rodrigoburigo/Documents/Projects/eTopocart/electric-app/node_modules/.pnpm/prisma@5.2.0/node_modules/prisma/build/index.js db pull --schema=".electric_migrations_tmp_70wJ7K/prisma/schema.prisma"
Error: 
P4001 The introspected database was empty: 
alco commented 9 months ago

@digoburigo Don't worry about the prisma username. The Proxy URL points at port 65432 which Electric's migrations proxy is listening on. The migration proxy handles connections from the prisma user in a special way to enable introspection of the electrified part of your SQL schema. Behind the scenes, the proxy connects to your database using the connection string from DATABASE_URL.

The error message "The introspected database was empty" indicates that your database does not have electrified tables.

CallMeLaNN commented 7 months ago

Good to know. I just came across this with different issue. I was going trough getting started with "Install yourself" at first for the existing repo, it wasn't clear about the manual migration step so I follow Prisma migration page. I was getting some errors when running electric-sql generate due to my schema, accidentally install newer prisma v5, use different migration and pnpm, but I managed to fix by comparing with the create-electric-app.

I was confusing too for trying to relate any error below the Proxy URL and looking to fix the prisma username 😅 Maybe having a note stating prisma user is special in the proxy would help.