Hi all-- I'm working on using `prisma migrate depl...
# orm-help
j
Hi all-- I'm working on using
prisma migrate deploy
with a Vercel deployment workflow + digital ocean pooled postgresql db & pgbouncer and can't quite figure out the right way to define a different DATABASE_URL to run during build time vs. runtime (in order to point to the non-pgbouncer URL during migrations at build time). It looks like Vercel exposes the "CI" env variable during build time which I thought would be helpful but is there a way to include a conditional in the schema.prisma file to react to this? Something like the following (which does not work for obvious reasons)?
Copy code
datasource db {
  provider = "postgresql"
  url      = env("CI") ? env("DATABASE_URL_NOPGBOUNCER") : env("DATABASE_URL")
}
Thanks!
💚 1
o
I don’t think so. I think really your only option is to use a different scheme of file at the moment. I’m wondering if it’s possible to tell generate command to use a different name schema fil of course you’d have to maintain them but at least it will be an option e.
👍 1
j
Feeling like there must be some way around it. The closest I've found is the build.env configuration but I'm unclear if I can overwrite an env variable that's also set in the project settings with that since in theory those are also available during the build step and it means maintaining env variables both in the project settings and in github secrets. I'll give it a try just to see...
looks like this would work except the ability for the build.env properties to read github secrets seems to have been deprecated so the only way to do it would be to use a plaintext db string with username/password in the vercel.json file which would be bad. Shot a support request over to vercel to see if they have a solution.
I did find a workaround here that I'm not super happy with but does work: Steps: 1) In Vercel, create a DATABASE_URL env variable pointing to the pooled DB. 2) In Vercel, create a DATABASE_CREDENTIALS env variable only containing the credentials string ex:
username:password
. 3) In Vercel, create a DATABASE_UNPOOLED_URL_FRAGMENT env variable only containing the url fragment for the unpooled url ex:
<http://somehost.com:1234/unpooleddb|somehost.com:1234/unpooleddb>
4) In schema.prisma, point to the DATABASE_URL like so:
Copy code
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
5) In package.json, override the DATABASE_URL env variable with the nonpooled db url during the build steps just before the migration and reference the DATABASE_CREDENTIALS env variable:
Copy code
"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "vercel-build": "export DATABASE_URL=\"postgresql://$DATABASE_CREDENTIALS@$DATABASE_UNPOOLED_URL_FRAGMENT\" && prisma migrate deploy && next build",
    "prisma:generate": "prisma generate",
    "postinstall": "prisma generate"
  },
6) After build, Vercel automatically re-sets the DATABASE_URL to the pooled string that was set in their UI in step #1 ...this feels pretty hacky but it's the only approach that lets you dynamically change the URL during the build step and avoids needing to create an additional schema that I've been able to find. Also you this lets you define different unpooled URLs for different environments via the normal Vercel env variable UI. Will followup with Vercel's response to my earlier inquiry as well in case they have a better way to do this.
o
That’s awesome. That reminds me of the time before tailwind had it just in time compiler. Where I had to set node anv to production and then switch it back periodically to build the small CSS file because developing with it was slow because hot reload was taking forever
j
Ha! I still hit issues from time to time with tailwind not compiling needed classes in dev 😞. Never have issues in prod though. Also: cleaned up the above a little to wrap the unpooled url fragment into an env variable as well so I can use vercel's env UI going forwards to set the URLs differently for preview/production. This, combined with a custom build ignore script to avoid every pull request trying to screw with the db seems to actually make the Vercel + Prisma migration story for pooled postgresql DBs tractable!
Following up on this thread. Heard back from Vercel and their recommendation is to use a non-pgbouncer URL env variable in the schema.prisma file and then override it in the client instantiation with a pgbouncer URL env variable. It feels a little upside down to me (if you don't have a central/single instantiation of the prisma client you have to remember to override it or you'll hit the wrong db), but I do think it's probably a safer approach since what I reported above isn't exactly a documented/supported method.
👍 1
g
hi John this last approach sounds nice but in general I wonder if you have a strategy for "transactional" builds i.e. what happens if your migration completes successfully but
next build
fails?
@John Egan sorry to @ you but probably you didn't see my reply 🙂