This document has been updated to include the recommended Prisma and PlanetScale workflow, specifically the recommendation to use
prisma db push instead of
prisma migrate dev with shadow branches. Also, you previously needed to turn on the ability to automatically copy the Prisma migration metadata. You no longer need to do this. Read more below.
In this tutorial, we're going to learn how to do Prisma migrations in PlanetScale as part of your deployment process using
prisma db push.
From a high level, Prisma's
db push introspects your PlanetScale database to infer and execute the changes required to make your database schema reflect the state of your Prisma schema. When
prisma db push is run, it will ensure the schema in the PlanetScale branch you are currently connected to matches your current Prisma schema.
prisma db push over
prisma migrate dev for the following reasons:
PlanetScale provides Online Schema Changes that are deployed automatically when you merge a deploy request and prevents blocking schema changes that can lead to downtime. This is different from the typical Prisma workflow which uses
prisma migrate in order to generate SQL migrations for you based on changes in your Prisma schema. When using PlanetScale with Prisma, the responsibility of applying the changes is on the PlanetScale side. Therefore, there is little value to using
prisma migrate with PlanetScale.
Also, the migrations table created when
prisma migrate runs can also be misleading since PlanetScale does the actual migration when the deploy request is merged, not when
prisma migrate is run which only updates the schema in the development database branch. You can still see the history of your schema changes in PlanetScale.
- Add Prisma to your project using
npm install prisma --save-devor
yarn add prisma --dev(depending on what package manager you prefer).
npx prisma initinside of your project to create the initial files needed for Prisma.
- Install the PlanetScale CLI.
- Authenticate the CLI with the following command:
pscale auth login
Prisma migrations follow the PlanetScale non-blocking schema change workflow. First, the schema is applied to a development branch and then the development branch is merged into the
main production database.
Let's begin with an example flow for running Prisma migrations in PlanetScale:
Create a new prisma-playground database:Terminalpscale db create prisma-playground
Connect to the database branch:Terminalpscale connect prisma-playground main --port 3309Note
This step assumes you created a new PlanetScale database and have not yet enabled Safe Migrations on the
mainbranch. You will need to create a new development branch otherwise.
prisma/schema.prismafile with the following schema:Note
relationModeand became generally available in
4.7.0. The following schema reflects this change.
In another terminal, use the
db pushcommand to push the schema defined in
prisma/schema.prisma:Terminalnpx prisma db push
prisma migrate devcommand, it will not create a migrations folder containing a SQL file with the SQL used to update the schema in your PlanetScale database. PlanetScale will be tracking your migrations in this workflow.Tip
You can learn more about the
prisma db pushcommand in the Prisma docs.
db pushis successful, you can see the table created in your terminal. For example, to see the
Posttable:Terminalpscale shell prisma-playground mainSQLdescribe Post;Tip
exitcommand to exit the MySQL shell.
Or you can see it in the PlanetScale UI under the Schema tab in your
Finally, turn on safe migrations on the
mainbranch to enable non-blocking schema changes:Terminalpscale branch safe-migrations enable prisma-playground main
Our first example migration flow went well, but what happens when you need to run further changes to your schema?
Let's take a look:
Create a new development branch from
add-subtitle-to-posts:Terminalpscale branch create prisma-playground add-subtitle-to-posts
Close the proxy connection to your
mainbranch (if still open) and connect to the new
add-subtitle-to-postsdevelopment branch:Terminalpscale connect prisma-playground add-subtitle-to-posts --port 3309
prisma/schema.prismafile, update the
Add a new
Post:subtitle String @db.VarChar(255)
db pushagain to update the schema in PlanetScale:Terminalnpx prisma db push
Open a deploy request for your
add-subtitle-to-postsbranch, so that you can deploy these changes to
You can complete the deploy request either in the web app or with the
pscale deploy-requestcommand.Terminalpscale deploy-request create prisma-playground add-subtitle-to-postsTerminalpscale deploy-request deploy prisma-playground 1
Once the deploy request is merged, you can see the results in your main branch's
Posttable:Terminalpscale shell prisma-playground mainSQLdescribe Post;
Now that you've successfully conducted your first automatic Prisma migration in PlanetScale and know how to handle future migrations, it's time to deploy your application with a PlanetScale database! Let's learn how to deploy an application with a PlanetScale database to Vercel.