Deploying database changes with Prisma Migrate
To apply pending migrations to staging, testing, or production environments, run the migrate deploy
command as part of your CI/CD pipeline:
$npx prisma migrate deploy
This guide does not apply for MongoDB.
Instead of migrate deploy
, db push
is used for MongoDB).
Exactly when to run prisma migrate deploy
depends on your platform. For example, a simplified Heroku workflow includes:
- Ensuring the
./prisma/migration
folder is in source control - Running
prisma migrate deploy
during the release phase
Ideally, migrate deploy
should be part of an automated CI/CD pipeline, and we do not generally recommend running this command locally to deploy changes to a production database (for example, by temporarily changing the DATABASE_URL
environment variable). It is not generally considered good practice to store the production database URL locally.
Beware that in order to run the prisma migrate deploy
command, you need access to the prisma
dependency that is typically added to the devDependencies
. Some platforms like Vercel, prune development dependencies during the build, thereby preventing you from calling the command. This can be worked around by making the prisma
a production dependency, by moving it to dependencies
in your package.json
.
For more information about the migrate deploy
command, see:
Deploying database changes using GitHub Actions
As part of your CI/CD, you can run prisma migrate deploy
as part of your pipeline to apply pending migrations to your production database.
Here is an example action that will run your migrations against your database:
deploy.yml
1name: Deploy2on:3 push:4 branches:5 - main67jobs:8 deploy:9 runs-on: ubuntu-latest10 steps:11 - name: Checkout repo12 uses: actions/checkout@v313 - name: Setup Node14 uses: actions/setup-node@v315 - name: Install dependencies16 run: npm install17 run: npm run build18 name: Apply all pending migrations to the database19 run: npx prisma migrate deploy20 env:21 DATABASE_URL: ${{ secrets.DATABASE_URL }}
Ensure you have the DATABASE_URL
variable set as a secret in your repository, without quotes around the connection string.