GresBoxGresBox
← All articles

Running Prisma migrations on deploy

A container that starts before its migrations have run will happily serve traffic against a table that does not exist yet. The fix is small; the failure is not.

8 min read

  • Prisma
  • DevOps
  • PostgreSQL
  • Docker

There is a specific kind of outage that only happens on the second deploy after a schema change. The build passes, the image is fine, the container starts, and then every request that touches one table returns a 500 because the column in the code does not exist in the database.

It happens because nothing in the pipeline ever ran the migration. Locally, prisma migrate dev applies it as a side effect of generating it, so the developer never notices the gap. In production there is no equivalent unless you build one.

Check whether you have this problem

Search the whole repository for the command that applies migrations in production:

grep -rn "migrate deploy" \
  Dockerfile* docker-compose* .github/ scripts/

No results means migrations are being applied by hand, or not at all. That is worth knowing before the next schema change rather than after it.

migrate deploy, not migrate dev

These are different commands and only one belongs in a pipeline.

  • prisma migrate dev compares your schema to the database, generates a new migration, and will offer to reset the database if it finds drift. Never run it against production.
  • prisma migrate deploy applies migration files that already exist, in order, and does nothing else. It does not generate, it does not prompt, and it does not reset. This is the production command.

Prisma tracks what has already run in a _prisma_migrations table, so migrate deploy is safe to run on every start. On a deploy with no schema change it applies nothing and exits.

Where to run it

Not in the Dockerfile. A build runs at image build time, on a machine that has no business holding production database credentials, and the image should be deployable to more than one environment. Baking a migration into the build ties one image to one database.

Run it at container start, in an entrypoint, before the application process begins:

#!/bin/sh
set -e

echo "Applying migrations..."
npx prisma migrate deploy

echo "Starting server..."
exec node server.js

Two details in that script matter more than they look. set -e stops the container if the migration fails, so a failed migration is a failed deploy rather than an application running against a half-migrated schema. And exec replaces the shell with the Node process, so it becomes PID 1 and receives the stop signal directly; without exec, the shell swallows SIGTERM and your container gets killed instead of shutting down.

The race nobody plans for

If you run more than one container of the same application, they all start at once and they all run the entrypoint. Prisma takes an advisory lock on the database, so the migrations themselves will not interleave, but the second container waits for the first to finish, which makes the lock a startup dependency you did not design.

For a blue and green deploy on a single host this is usually harmless. Beyond that, run migrations as a distinct step that finishes before the new containers start:

docker run --rm --env-file .env myapp:new \
  npx prisma migrate deploy

docker compose up -d app_green

One migration run, an explicit failure point, and application containers that start knowing the schema is already correct.

Write migrations that survive a rollback

Migrations run forward automatically. Rollbacks do not. If you deploy a migration that drops a column and then roll the application back to the previous image, the old code goes looking for a column that is gone.

The way out is to make destructive changes in two deploys rather than one. To rename a column:

  1. 1.Deploy one: add the new column, write to both, read from the old one. Backfill existing rows.
  2. 2.Deploy two: read from the new column. The old one is now unused but still there, so a rollback to deploy one still works.
  3. 3.Deploy three, later: drop the old column, once you are confident you will not roll back that far.

It is three deploys instead of one, and it means a rollback is always available. The alternative is discovering at the worst possible moment that going back is not an option.

A checklist

  • migrate deploy runs automatically, at container start or as its own pipeline step
  • The entrypoint uses set -e, so a failed migration fails the deploy
  • The application process is exec'd, so it receives stop signals
  • Database credentials are injected at run time, never copied into an image layer
  • Destructive changes are split across deploys so rollback stays possible
  • You have restored a backup at least once, on purpose, to confirm the backup works
A deployment process that has never been reversed is an assumption. Production is a poor place to find out which kind it was.

Working on something like this?

If this article describes a problem you are living with, we can probably help you fix it. Tell us what you are running into.