docs icon indicating copy to clipboard operation
docs copied to clipboard

Docs: Add note on connection URLs for PgBouncer and Prisma Migrate

Open sauntimo opened this issue 2 years ago • 17 comments

Describe this PR

  • it might just be me being dense but I didn't realise that the datasource url in schema.prisma could be different than the datasources.db.url used when the PrismaClient is instantiated - and in fact, when using PgBouncer and Prisma Migrate, should be different.
  • My situation was slightly complicated by Google Cloud Run (see this Stack Overflow Post) but fundamentally I hadn't grasped that I needed to use two different connection URLs in these two places

Changes

  • added a note to explain the need to use postgres connection URLs for Prisma Migrate and PgBouncer connection urls for PrismaClient instantiation

What issue does this fix?

  • I don't think this is explained elsewhere in the docks and was a pain point I encountered when setting up Prisma and PgBouncer

Any other relevant information

  • n/a

@all-contributors please add @sauntimo for doc

sauntimo avatar Sep 08 '22 12:09 sauntimo

@sauntimo is attempting to deploy a commit to the Prisma Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Sep 08 '22 12:09 vercel[bot]

That is one way to do things ,but you can also just have a different DATABASE_URL values depending on if you are using Migrations, or Prisma Client.

janpio avatar Sep 08 '22 18:09 janpio

That is one way to do things ,but you can also just have a different DATABASE_URL values depending on if you are using Migrations, or Prisma Client.

@janpio absolutely, should we mention that approach too? For me, the confusion came from the assumption that both values always had to be the same. Maybe no one else assumed that! I just thought it might help to clarify that the different situations call for different values, however one might choose to manage them.

sauntimo avatar Sep 09 '22 03:09 sauntimo

Which values did you assume would need to be the same?

janpio avatar Sep 09 '22 10:09 janpio

@janpio the connection URL used in schema.prisma and the the connection URL used when instantiating the PrismaClient

sauntimo avatar Sep 09 '22 11:09 sauntimo

Ah. The URL you can provide in PrismaClient is explicitly meant to override the one from the schema file - that is the idea behind that feature.

Now you confusion makes sense to me.

Is there maybe a location where we can make this clearer?

janpio avatar Sep 09 '22 11:09 janpio

@janpio I suppose it does mention that in the PrismaClient docs already, however until I was using pgBouncer, it didn't matter that I had not read that and had no understanding of it, because I only had one database connection URL which was used for both migrations and queries. It only became an issue when I started using pgBouncer, because then I had two different connection URLs. So, I think it makes sense to clarify this in the docs which discuss using pgBouncer with Prisma, which is what I've attempted to do in this PR 😁

sauntimo avatar Sep 09 '22 11:09 sauntimo

Yep, that makes sense. I am not convinced about the exact headline and wording of it, but I now understand why it makes sense to highlight that information on this page specifically. Linking to the PrismaClient docs about being able to overwrite makes a lot of sense already - we do not really give concrete help how these two different URLs even can be handled.

janpio avatar Sep 09 '22 13:09 janpio

Thanks @sauntimo for your contribution, and @janpio for your investigation. I'll review your PR and suggest some changes.

andrew-walford-prisma avatar Sep 14 '22 10:09 andrew-walford-prisma

Thanks @andrew-walford-prisma, very happy to make any changes if you can provide some steer 😁 Excited to be able to contribute something!

sauntimo avatar Sep 14 '22 10:09 sauntimo

Please ping me for review when these changes are applied. Thanks.

janpio avatar Sep 14 '22 13:09 janpio

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
docs ✅ Ready (Inspect) Visit Preview Sep 14, 2022 at 6:29PM (UTC)

vercel[bot] avatar Sep 14 '22 13:09 vercel[bot]

@andrew-walford-prisma thanks for your suggestions, they all made sense to me and I've applied them now (GitHub's new batch commit for suggestions from code review is neat, huh!?). @janpio this should be ready for you to review now. Thank you both for your time and consideration with this 🙏

sauntimo avatar Sep 14 '22 16:09 sauntimo

@janpio that's very reasonable criticism and I totally support making this information as inclusive as possible, and applicable to all users and situations. However, I feel a bit out of my depth - I only have experience in the particular situation I have described, so I may not be the best person to resolve those concerns. I think what we've got is better than nothing; I'm happy to make any changes myself if someone can describe what those changes should be, but I am also happy for someone with a broader understanding to take this forward and improve it if that's easier. I'll leave it to your to decide how best to proceed 😄

sauntimo avatar Sep 15 '22 07:09 sauntimo

Yes, this is up to @andrew-walford-prisma to take over now.

janpio avatar Sep 15 '22 08:09 janpio

We're researching the various connection possibilities when using a connection pool and Prisma Migrate. I'll come back to this PR when we've decided on the best solution to recommend.

andrew-walford-prisma avatar Sep 16 '22 11:09 andrew-walford-prisma

Prisma's internal discussion on connection strings and connection pooling tools is here: https://www.notion.so/prismaio/How-to-use-separate-connection-strings-for-Prisma-Client-and-Prisma-Migrate-58baf70095594f8fa421104e68524a49

andrew-walford-prisma avatar Sep 20 '22 15:09 andrew-walford-prisma

Note for docs: new directUrl feature in 4.9.0 might help with this issue

andrew-walford-prisma avatar Jan 16 '23 16:01 andrew-walford-prisma

Indeed, you can then have the PgBouncer url in url, and the direct one for Migration and Introspection in directUrl. We should replace this PR with a mention of directUrl once it is out and confirmed working.

janpio avatar Jan 17 '23 00:01 janpio

This is nowobsolete with directUrl: https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#fields

janpio avatar Nov 20 '23 18:11 janpio