N
Neon2y ago
genetic-orange

How to separate local development on branch X from a remote preview URL for same branch X?

What's the recommended workflow to separate data between a remote preview Vercel URL (e.g. for my-feature branch) and local development on the same branch? When I push the my-feature branch, the Vercel integration would create the branch in Neon, which is great. But, while someone is testing this branch and messing with the data, I'd like to continue developing locally on this branch and make sure that I'm not touching that Neon branch that's being tested.
29 Replies
inland-turquoise
inland-turquoise2y ago
When it comes to development, we recommend setting up a dedicated branch for each developer on a team. This branch is meant to be long-lived and should be reset from the parent branch if it becomes out of sync. Does this answer your question? 😄
genetic-orange
genetic-orangeOP2y ago
Not really, I'm sorry @Mahmoud Does it mean when I push my branch, the preview URL will use the same data as local development on this branch? I usually develop on branch "new-feature" and push it to a preview URL for testing. When tested, I'd like to continue developing locally on this branch and have the data separate from the remote branch.
inland-turquoise
inland-turquoise2y ago
Does it mean when I push my branch, the preview URL will use the same data as local development on this branch?
No, each branch is completely isolated from the other. Different connection strings, and if changes are made to one of the branches, they don't affect the other You can essentially use a Neon branch as a replacement for a local Postgres instance. The difference is that your branch will have the data from the parent and you can reset this branch to update it
genetic-orange
genetic-orangeOP2y ago
No, each branch is completely isolated from the other.
I'm confused. It is the same branch, just 2 different environments, one local and one remote.
inland-turquoise
inland-turquoise2y ago
It is the same branch, just 2 different environments, one local and one remote.
I see. We recommend using a separate branch for each environment
genetic-orange
genetic-orangeOP2y ago
Do I understand correctly that Neon doesn't have a concept of an "environment" such that "branch X in development environment" is using a different connection string than "branch X in preview/prod environment"?
inland-turquoise
inland-turquoise2y ago
No description
inland-turquoise
inland-turquoise2y ago
Do I understand correctly that Neon doesn't have a concept of an "environment" such that "branch X in development environment" is using a different connection string than "branch X in preview/prod environment"?
Not really. You can think of a branch as just a copy of your database that you can use
genetic-orange
genetic-orangeOP2y ago
In terms of the recommended workflow, let's say I want to build feature X. Does the following flow make sense? 1. create local branch dev/bob off main, this will use the dev/bob Neon branch (what's the recommended way to create the Neon branch? using CLI?) 2. once the feature is ready, merge dev/bob to preview/feature/X and push preview/feature/X for testing. This will use the preview/feature/X Neon branch. 3. While the feature is being tested, I can continue working on dev/bob as it's using a separate Neon branch. 4. Once preview/feature/X is good to go, create a PR to main.
inland-turquoise
inland-turquoise2y ago
Yup!
(what's the recommended way to create the Neon branch? using CLI?)
Whatever is easiet for you. You can use the Neon Console or the CLI Are you using an ORM?
genetic-orange
genetic-orangeOP2y ago
yep, using drizzle does this also mean that: 1. If I work locally on main I'd be messing with the prod data? 2. If I need another local branch to work simultaneously on feature Y, I just create dev/bob2 ?
inland-turquoise
inland-turquoise2y ago
If I work locally on main I'd be messing with the prod data?
Nope, changes made to a child branch don't affect the parent. Each branch is completely isolated.
If I need another local branch to work simultaneously on feature Y, I just create dev/bob2 ?
Yea exactly. You can also name the branch based on the feature you're working on if you prefer Neon branches are somewhat similar to git branches
genetic-orange
genetic-orangeOP2y ago
with git, there is a concept of local branch vs remote branch. it's not the same with Neon, right? this is I think the source of my confusion
Nope, changes made to a child branch don't affect the parent. Each branch is completely isolated.
Are you saying that local main branch is a child branch? Who is the parent?
inland-turquoise
inland-turquoise2y ago
with git, there is a concept of local branch vs remote branch. it's not the same with Neon, right? this is I think the source of my confusion
Understandable. But yea the concept of local/remote doesn't apply to Neon
Are you saying that local main branch is a child branch? Who is the parent?
the Neon main branch would be the parent and the Neon dev/preview branches are the children
genetic-orange
genetic-orangeOP2y ago
Also, when switching branches locally, do I need to manually update the DATABASE_URL in .env.local or is there a better way?
inland-turquoise
inland-turquoise2y ago
do I need to manually update the DATABASE_URL in .env.local or is there a better way?
yes, but the CLI makes it easier since you get the connection string you can create a branch by running: neonctl branches create <branch-name. This will give you the connection string https://neon.tech/docs/reference/cli-branches Then you can get the connection string of an existing branch by running: neonctl cs <branch-name>
inland-turquoise
inland-turquoise2y ago
btw I did a video a while back about the workflow: https://www.youtube.com/watch?v=PX881bVAPxM This is using Prisma but the concepts are the same with Drizzle
Neon
YouTube
Prisma essentials: from development to production (Prisma Migrate w...
Learn about database migrations using Prisma Migrate and how to go from Development to Preview to Production. - Code example: https://github.com/neondatabase/preview-branches-with-fly/tree/with-prisma - Try Neon at https://neon.tech/youtube → Neon Discord: https://neon.tech/discord → Follow us on Twitter: https://twitter.com/neondatabase
→ S...
genetic-orange
genetic-orangeOP2y ago
Thanks, I'll have a look
inland-turquoise
inland-turquoise2y ago
Let me know if you have any more questions or if things aren't 100% clear. Happy to clarify things further
like-gold
like-gold2y ago
I think a prefix is best. The local one could be dev/my-feature for example
genetic-orange
genetic-orangeOP2y ago
Thanks guys, I'm still trying to wrap my head around the workflow with Vercel & Neon. @Mahmoud One thing I noticed you keep mentioning in your videos is that "preview deployment is created when you create a pull request". If I understand it correctly, Vercel creates the preview deployment when you push code (i.e. your branch). When pull request is created, there are actually no new deployments. If you keep pushing to the PR branch, then a new deployment is created again.
inland-turquoise
inland-turquoise2y ago
Vercel creates the preview deployment when you push code (i.e. your branch). When pull request is created, there are actually no new deployments. If you keep pushing to the PR branch, then a new deployment is created again.
That is correct
genetic-orange
genetic-orangeOP2y ago
Another thing I noticed in the Vercel video is that the build command was modified to run the migrations before the app is built. I'm wondering if this could introduce some downtime to my app. What I imagine could happen is: 1. App is working in prod as normal 2. New changes are pushed to main (or PR to main is merged) 3. Migrations are running first and destroying potentially something in the db 4. While the app itself is building and deploying, users interact with the old version of the app but the db already has the new changes - this could lead to what seems like a bug Is this true, or am I misunderstanding something and there will be no downtime?
inland-turquoise
inland-turquoise2y ago
Ideally, you don't introduce breaking changes to your database schema. You can leverage the expand and contract pattern https://www.prisma.io/dataguide/types/relational/expand-and-contract-pattern
Prisma's Data Guide
Using the expand and contract pattern | Prisma's Data Guide
In this article, we introduce the expand and contract pattern to help migrate data and clients to a new schema.
inland-turquoise
inland-turquoise2y ago
a breaking change would be something like renaming a column/table, dropping it, etc. You could also leverage feature flags
genetic-orange
genetic-orangeOP2y ago
thanks, I'll have a read! I also noticed that when I delete the remote branch, e.g. after the PR was merged, the branch on Neon doesn't disappear. It's only after some time, usually after I create some other branch on Neon, only then that old PR branch gets removed on Neon. Is this intentional? What does this warning mean?
Warning '@neondatabase/serverless' can only connect to remote Neon/Vercel Postgres/Supabase instances through a websocket
I'm getting it when running:
bunx drizzle-kit migrate --config ./app/lib/db/drizzle.config.ts
inland-turquoise
inland-turquoise2y ago
Oh this warning simply means that the Serverless driver won't necessarily work with all Postgres databases you're using the Neon integration on Vercel, correct?
genetic-orange
genetic-orangeOP2y ago
Is there no way to detect that I'm using it with remote Neon and hide the warning? correct
inland-turquoise
inland-turquoise2y ago
Is there no way to detect that I'm using it with remote Neon and hide the warning?
I don't think so. I'll reach out to the Drizzle team

Did you find this page helpful?