John Egan
06/02/2021, 8:31 PMprisma 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)?
datasource db {
provider = "postgresql"
url = env("CI") ? env("DATABASE_URL_NOPGBOUNCER") : env("DATABASE_URL")
}
Thanks!Omar Hamdan
06/02/2021, 9:08 PMJohn Egan
06/02/2021, 9:16 PMJohn Egan
06/02/2021, 10:06 PMJohn Egan
06/03/2021, 2:05 AMusername: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:
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:
"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.Omar Hamdan
06/03/2021, 2:16 AMJohn Egan
06/03/2021, 2:26 AMJohn Egan
06/11/2021, 2:08 PMGiuseppe
06/13/2021, 5:05 PMnext build fails?Giuseppe
06/24/2021, 6:30 PM