N
Neon16mo ago
like-gold

Setting up automated deploys for GitHub to Vercel to Neon

I setup a production environment in my app and followed the instructions to use the Neon integration within Vercel. Everything was working fine until my dev build required a Prisma migration. I ran the migration locally and everything is working in the dev environment, but when I attempted to run the new code in a preview (and in the main build) the app breaks because it appears that the migration does not apply to remote branches. I had an issue with migrations in the past, due to missing environment variables, so I created a local script to initiate a migration in my dev environment. Thus the migration runs and updates my dev instance of Neon without issue.
// migrate-dev.mjs
import { spawn } from "child_process"
import { config } from "dotenv"

// Load environment variables from .env.local
config({ path: ".env.local" })

// Generate a unique migration name based on the current timestamp
const migrationName = `migration-${new Date()
.toISOString()
.replace(/[:.]/g, "-")}`

// Run the Prisma migrate dev command interactively
const migrate = spawn(
"npx",
["prisma", "migrate", "dev", "--name", migrationName],
{ stdio: "inherit" }
)

migrate.on("close", (code) => {
if (code !== 0) {
console.error(`Migration process exited with code ${code}`)
process.exit(1)
}
})
// migrate-dev.mjs
import { spawn } from "child_process"
import { config } from "dotenv"

// Load environment variables from .env.local
config({ path: ".env.local" })

// Generate a unique migration name based on the current timestamp
const migrationName = `migration-${new Date()
.toISOString()
.replace(/[:.]/g, "-")}`

// Run the Prisma migrate dev command interactively
const migrate = spawn(
"npx",
["prisma", "migrate", "dev", "--name", migrationName],
{ stdio: "inherit" }
)

migrate.on("close", (code) => {
if (code !== 0) {
console.error(`Migration process exited with code ${code}`)
process.exit(1)
}
})
I have this script defined in my package.json file as, "migrate-dev": "node migrate-dev.mjs", in my scripts. In my local terminal I run npm run migrate-dev to manually initiate a migration. The problem I'm facing is how do I take that migration and apply it to main (production) or to any of my preview deploys from Vercel to Neon? In my Vercel setup I followed instructions I saw online to add a custom build command, so my Vercel project runs npx prisma generate && npm run build at build. Everything seemed to be working, but as I mentioned apparently a migration is not being initiated at build. I'd appreciate any help I can get to help me fix my deploy process. Thanks!
4 Replies
rival-black
rival-black16mo ago
@Mahmoud or @Rishi Raj Jain can you take a look here
like-gold
like-goldOP16mo ago
I think I figured it out. I added migrate to my build command. npx prisma generate && npx prisma migrate deploy && npm run build If you have any recommendations for improvement, I'm all ears. I appreciate your help! The first time I deployed (with a change to the Prisma schema) the deploy built without a hitch. I initiated a new deploy and it's failing during the build process—something I had seen locally that caused me to create a migration script that I manually run in dev. Here is the error log:
[14:59:59.593] Prisma schema loaded from prisma/schema.prisma
[14:59:59.604] Datasource "db": PostgreSQL database
[14:59:59.607]
[14:59:59.607] Error: Prisma schema validation - (get-config wasm)
[14:59:59.608] Error code: P1012
[14:59:59.608] error: Environment variable not found: DATABASE_URL.
[14:59:59.608] --> prisma/schema.prisma:7
[14:59:59.608] |
[14:59:59.608] 6 | provider = "postgresql"
[14:59:59.608] 7 | url = env("DATABASE_URL")
[14:59:59.608] |
[14:59:59.608]
[14:59:59.608] Validation Error Count: 1
[14:59:59.608] [Context: getConfig]
[14:59:59.608]
[14:59:59.608] Prisma CLI Version : 5.17.0
[14:59:59.636] Error: Command "npx prisma generate && npx prisma migrate deploy && npm run build" exited with 1
[14:59:59.977]
[14:59:59.593] Prisma schema loaded from prisma/schema.prisma
[14:59:59.604] Datasource "db": PostgreSQL database
[14:59:59.607]
[14:59:59.607] Error: Prisma schema validation - (get-config wasm)
[14:59:59.608] Error code: P1012
[14:59:59.608] error: Environment variable not found: DATABASE_URL.
[14:59:59.608] --> prisma/schema.prisma:7
[14:59:59.608] |
[14:59:59.608] 6 | provider = "postgresql"
[14:59:59.608] 7 | url = env("DATABASE_URL")
[14:59:59.608] |
[14:59:59.608]
[14:59:59.608] Validation Error Count: 1
[14:59:59.608] [Context: getConfig]
[14:59:59.608]
[14:59:59.608] Prisma CLI Version : 5.17.0
[14:59:59.636] Error: Command "npx prisma generate && npx prisma migrate deploy && npm run build" exited with 1
[14:59:59.977]
rival-black
rival-black16mo ago
The error says your environment variable is not available
like-gold
like-goldOP16mo ago
Yes I understood that part. I'm not sure what happened but when I checked back in later my deploy had rebuilt and was working. Maybe it was a Vercel thing. 🤷‍♂️ Thanks.

Did you find this page helpful?