Prisma Migration on Heroku and NodeJS

Prisma migrations are simple and clear for local development, but what about staging and production on Heroku? There are several ways to actually run Prisma migrations.

1. Heroku Console

prisma migrations

To run a one-time Prisma migration, start up the Heroku console and run bash. From there, you can run yarn prisma migrate deploy to run the migration on that specific environment.

2. Local Environment

Heroku offers some instructions on how to connect to Postgres database from outside. My personal favorite for my Postgres table needs is connecting via TablePlus. You can do so by adding all the credentials (can be found under Resource -> Postgres -> Settings -> Database Credentials) when creating a new postgres connection in TablePlus.

The goal when connecting via local environment is to fetch the dynamically generated database url through Heroku CLI. You can download the CLI with a simple brew tap heroku/brew && brew install heorku on Mac. Once you have Heroku CLI installed, you can run prisma migrations via the command below. In fact, you can run any other necessary processes by replacing prisma migrate deploy.

yarn DATABASE_URL=$(heroku config:get DATABASE_URL -a your-app) prisma migrate deploy

3. Build Script/Package.json

The previous two steps are great for one-off migrations and resets. But what about the automated migrations that need to happen for dynamically spun-up PR, staging, and production environments? You can actually build the migration commands into the build process.

For our project, we’re using NodeJS, and Heroku allows for customization of the build process via heroku-prebuild, heroku-postbuild, and heroku-cleanup. As stated in Heroku documentation, if a heroku-postbuild script exists, the build
script will not run. So be sure to include your build commands.
"heroku-postbuild": "yarn prisma migrate dev && yarn build:production"

Prisma Migrations 3 Ways

There you have it! Three ways to run Prisma migrations on Heroku. And if all else fails, you can always reset the database inside the Postgres plugin setting, right? Happy migration!