NeonN
Neon2y ago
6 replies
worthy-azure

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)
  }
})

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!
Was this page helpful?