N
Neon15mo ago
genetic-orange

Migration Workflows

We are using Vercel, Neon, Prisma and GitHub. I've set up the Neon Vercel integration, as well as the Github Vercel integration, and everything's working well. Our workflow right now is: 1. Create a branch off of main. This causes Vercel and Neon to spin up a branch specific environment. 2. Push changes to the branch. Pull the env vars with vercel env pull --environment=preview --git-branch=$branch_name for local dev. 3. Once the merged into main, CI/CD pushes everything to production. What's a good flow for managing migrations in this setup? For branch specific migrations, we've just been running them ourselves locally with npx prisma migrate dev, and then a manual npx prisma migrate deploy for production. Have any of you set up good Github Actions/Workflows for this? One challenge is it's not clear to me how to programmatically get the database URL for a branch. I found https://neon.tech/docs/guides/neon-github-integration and https://github.com/neondatabase/preview-branches-with-vercel/tree/main but since the integration already creates the branch, it's not clear how to fetch that info.
Neon
The Neon GitHub integration - Neon Docs
The Neon GitHub integration connects your Neon projects to corresponding GitHub repositories, helping you incorporate your database into your developer workflow. For example, create a database branch ...
GitHub
GitHub - neondatabase/preview-branches-with-vercel: Example project...
Example project that shows how you can create a branch for every preview deployment on Vercel using GitHub actions - neondatabase/preview-branches-with-vercel
17 Replies
flat-fuchsia
flat-fuchsia15mo ago
You can run npx prisma migrate deploy before your project's build step So whenever a preview deployment gets created, the environment variable that the Neon integration on Vercel sets on your preview deployment will be used by prisma migrate deploy When you merge your PR, you will trigger a production deployment on Vercel, and the DATABASE_URL that's used for production will be used by prisma migrate deploy before your project builds and deploys You don't need to use to GitHub Actions if you're using the Neon integration on Vercel
genetic-orange
genetic-orangeOP15mo ago
Ah got it - so override the build command in Vercel to include an npx prisma migrate deploy for merges to main?
flat-fuchsia
flat-fuchsia15mo ago
yup That's all you need to do no more running migrations manually
genetic-orange
genetic-orangeOP15mo ago
ok I'm a little embarassed I didn't think of that myself 😆 Thank you so much, works like a charm.
flat-fuchsia
flat-fuchsia15mo ago
this override would also work for previews
genetic-orange
genetic-orangeOP15mo ago
ya solves all of my problems. Just changed build to "build": "prisma migrate deploy && next build" and magic.
flat-fuchsia
flat-fuchsia15mo ago
Nothing to be embarrassed about! We should add a note in the docs cc @Daniel
flat-fuchsia
flat-fuchsia15mo ago
I think you might need to also include npx prisma generate at the beginning. https://www.prisma.io/docs/orm/more/help-and-troubleshooting/help-articles/vercel-caching-issue
Learn to configure your build process on Vercel to avoid caching-re...
Learn to configure your build process on Vercel to avoid caching-related problems
genetic-orange
genetic-orangeOP15mo ago
btw I'm loving the vercel + neon integration - feels like magic. I wrote a little env script to pull down vars for local development and it's as smooth as can be:
#!/bin/bash -e

branch_name="$(git symbolic-ref HEAD 2>/dev/null)"
branch_name=${branch_name##refs/heads/}

if [ "$branch_name" == "" ]; then
echo "You are on an unnamed branch (detached HEAD). Please switch to a named branch."
exit 1
fi
if [ "$branch_name" == "main" ]; then
echo "You are on the main branch. Please switch to a different branch."
exit 1
fi

echo "Pulling env vars from $branch_name"
vercel env pull .env --environment=preview --git-branch=$branch_name
#!/bin/bash -e

branch_name="$(git symbolic-ref HEAD 2>/dev/null)"
branch_name=${branch_name##refs/heads/}

if [ "$branch_name" == "" ]; then
echo "You are on an unnamed branch (detached HEAD). Please switch to a named branch."
exit 1
fi
if [ "$branch_name" == "main" ]; then
echo "You are on the main branch. Please switch to a different branch."
exit 1
fi

echo "Pulling env vars from $branch_name"
vercel env pull .env --environment=preview --git-branch=$branch_name
flat-fuchsia
flat-fuchsia15mo ago
are you using this in combination with the Neon CLI?
genetic-orange
genetic-orangeOP15mo ago
ya already have a postinstall running. I'm not using the neon CLI at all.
flat-fuchsia
flat-fuchsia15mo ago
aha, that makes sense. how are you using Neon as part of your local development workflow? Are you using the dev branch that gets created while setting up the Neon integration on Vercel?
genetic-orange
genetic-orangeOP15mo ago
Yeah - though my team's growing, so I've mostly been just creating a PR and working off of the env that gets spun up off of that (pulling the env vars to run things locally)
flat-fuchsia
flat-fuchsia15mo ago
That's an interesting workflow. Thank you for sharing
genetic-orange
genetic-orangeOP15mo ago
it's pretty nice - the one awkward bit is just you can't create an empty PR, so I need to push something before vercel and neon spin up the branch env. Otherwise though really easy.
flat-fuchsia
flat-fuchsia15mo ago
because typically what we see is: 1. Create a git branch locally 2. Use your own Neon branch for development 3. generate migrations 4. push your changes and preview 5. merge we don't see using the DATABASE_URL that's part of the preview deployment as the branch that's used for development yea I get what you mean
genetic-orange
genetic-orangeOP15mo ago
oh that's good too - and just using the CLI to create/destroy the branches as needed?

Did you find this page helpful?