1. 3
    Set up Planetscale Main and Dev Branches and Create Tables through Prisma
    5m 15s

Set up Planetscale Main and Dev Branches and Create Tables through Prisma

Ben Patton
InstructorBen Patton
Share this video with your friends

Social Share Links

Send Tweet

Next up is creating a Planetscale database for our application. Planetscale works a lot like GitHub in that it uses branches for development and production.

We will create a new database and then create a dev branch to separate the data we create in dev from what is in production.

When the database is created, we'll set the DATABASE_URL in our .env file for the main and dev branches.

The last thing we will need to do is push the prisma schema to planetscale so that it can create the necessary tables for our application to function.

Explore the prisma schema and tables created to get more familiar with what the application is doing.

Instructor: [0:00] Now, let's focus on setting up PlanetScale. Let's add a new tab and go to PlanetScale.com. We are going to get started.

[0:16] It logged me in. If you don't have an account, it'll just do try to authenticate with GitHub. That's what I'm using is GitHub to authenticate with PlanetScale.

[0:26] I don't have any databases yet, so let's go to Create. We're going to say next migration invoices app. We want to use the free tier hobby. Then let's click Create Database. Make sure you don't have spaces.

[0:51] We're going to get a main branch. This works how you would imagine if you use GitHub. You have a main branch and you can create branches off of it.

[0:58] What we're going to do is create a new branch. Once it's finished initializing, that is our dev branch so that we can have a main branch for production with our environment variables for our main branch.

[1:16] Then we'll get the dev branch connection strings and put it here. First, let's get the connection string. We're using Prisma. When we open this, you'll see that this is main. We'll need to actually create a password.

[1:31] When you create the password, it'll generate it for you. Then it may or may not open up to Prisma directly. If you want to connect with, make sure you select Prisma. Then you can grab this environment variable. We're actually just going to grab the string. Copy that, and then we'll just paste that here.

[1:56] That is the main. We're going to go ahead and just put, label this main for now, just as a placeholder. Then we'll create another one, and we'll just label this one database URL.

[2:17] Now we can exit out of this. Then let's come back up, click Create New Branch. We're going to call it dev, and it's going to be based off of main. Then dev, lowercase, create branch.

[2:31] This is going to go through the same process of setting up a dev branch for you. Then once that's finished, we'll paste that environment variable here.

[2:42] We'll remove this main database URL. We're keeping it here just so that when we set up our application in Vercel to deploy that we can grab this quickly and not have to come back into PlanetScale and create a new password for main.

[2:59] Once we paste that in our Vercel app, then we'll remove it from our environment variables file here. Now we want to go here to connect. We need to create a new password for our dev branch.

[3:13] On branch dev, now we want to create a password. Now we get our developer environment variable. Let's just paste that back over this.

[3:28] Now, we can save that and let's exit out of this window. The last thing we need to do is stop the server and we have Prisma set up already. We need to push this schema to our database.

[3:43] We need to run npx prisma db push. We're pushing our Prisma schema to our database. Then if we look over in our PlanetScale application, now it has all of our tables created, and we can see that there are eight created tables.

[4:03] Now, if we run our server again, let's close this window a bit. Let's come back to localhost://3000. We are currently not signed in, so let's just try to access the page.

[4:24] Let's go to sales. It's going to make us sign in. If we sign in, let's authorize this user. We are now redirected to our home page. If we now try to go back to this route, we are now able to see our application.

[4:41] In this lesson, we set up PlanetScale by going to planetscale.com and we created our first database. Then once that was set up, we got the connection string, which we are currently storing here before we move it out of here and put it into Vercel.

[4:57] Then we also created a dev branch. Then we created a new password for that dev branch, which is stored in our local database URL environment variable. Then we pushed our Prisma schema to our database. Then we started our server again and we were able to log in and authenticate with NextAuth and Prisma.