# Run Prisma migrate deploy before your Node.js API takes traffic

Gregale · Updated 2026-12-01

Use Prisma ORM v7 with a pooled runtime URL and a direct migration URL. Apply committed PostgreSQL migrations as a release step before the new container serves.

**Run `prisma migrate deploy` as a release command, before the new Node.js
revision takes traffic.** Serve requests using the pooled `DATABASE_URL`.
Give the release command a separate, direct `MIGRATION_DATABASE_URL` with
schema permissions. In Prisma ORM v7, the CLI reads that URL from
`prisma.config.ts`, not from a `directUrl` entry in the schema.

[View the source on GitHub](https://github.com/gregale-dev/prisma-release-migrations)
or [download the runnable Prisma v7 example](https://gregale.dev/examples/prisma-release-migrations.zip).
It uses Node.js, a committed npm lockfile, Docker, and a disposable managed
PostgreSQL database. The sample writes one fixture row. Do not run this
example against a production database.

## Keep the two connections separate

| What runs                         | URL                      | Why                                                                                                                         |
| --------------------------------- | ------------------------ | --------------------------------------------------------------------------------------------------------------------------- |
| Node.js API                       | `DATABASE_URL`           | A pooled application login for reads and writes. It should not change the schema.                                           |
| Prisma CLI in the release command | `MIGRATION_DATABASE_URL` | A direct connection with schema permissions. Migrations should not depend on a transaction pooler preserving session state. |

The application creates its Prisma client with `PrismaPg` and the runtime
connection. It refuses to start if the migration credential is present.
The migration CLI uses its separate configuration:

```ts
import { defineConfig } from 'prisma/config';

export default defineConfig({
  schema: 'prisma/schema.prisma',
  migrations: { path: 'prisma/migrations' },
  datasource: { url: process.env.MIGRATION_DATABASE_URL },
});
```

There is no fallback to the pooled URL. `prisma generate` runs during
the Docker build and does not need a database URL. The image keeps the
Prisma CLI installed for the release command. `prisma migrate deploy`
applies committed migrations; it does not generate the client.

## Deploy the sample

From the extracted sample directory, sign in with the [Gregale CLI](https://gregale.dev/docs/install.md).
Use fresh app and database names. Managed PostgreSQL and release commands are
plan-dependent; see the [current plans](https://gregale.dev/docs/plans.md).

```bash
gregale login
gregale deploy --path . --name prisma-release-sample --create-only
gregale postgres create prisma-release-db --class development --scale-to-zero
gregale postgres get prisma-release-db
```

Wait until PostgreSQL is ready, then attach the two bindings and deploy.

```bash
gregale postgres attach prisma-release-db prisma-release-sample \
  --env DATABASE_URL --access read_write
gregale postgres attach prisma-release-db prisma-release-sample \
  --env MIGRATION_DATABASE_URL --access migration
gregale deploy --path . --name prisma-release-sample
```

The included `gregale.yaml` runs the migration from the built image before
the candidate receives traffic. Its readiness endpoint exercises the new
schema with the serving credential.

```yaml
hosting:
  health: /ready
release:
  command: npm run migrate
```

Use the exact deployment UUID from the release page or `gregale --json deploy`
to check the app response without changing its access policy:

```bash
gregale release verify DEPLOYMENT_UUID --app prisma-release-sample --path /notes
```

The baseline response includes `revision: "baseline"` and the seeded note.

## Prove the release gate with a disposable database

The example includes a script to prepare the two follow-up source directories.
After the baseline is serving, generate the two directories. These commands
are for the disposable app only. Do not continue if the valid migration
does not pass verification.

```bash
bash prepare-drill.sh ../prisma-drill
gregale deploy --path ../prisma-drill/valid --name prisma-release-sample
# Use the valid deployment UUID printed by the CLI.
gregale release verify VALID_DEPLOYMENT_UUID --app prisma-release-sample --path /notes
gregale rollouts status prisma-release-sample --release VALID_DEPLOYMENT_UUID --wait
# Proceed only when the valid release has full traffic.
gregale deploy --path ../prisma-drill/intentional-failure --name prisma-release-sample
```

The additive migration runs first. It adds
`published` to the table, and the new API expects that field and returns
`revision: "migrated"`. Existing code can still read the original columns.

Then deploy the deliberately failing revision **only on the disposable app
and database**. Its last migration requests division by zero inside a
transaction. Prisma should report a migration failure, the candidate should
receive no traffic, and the last good deployment should still return the
same migrated note. The bad migration can leave a failed migration record
even though it made no schema changes. Discard the test database after the
drill. Do not use this as a production recovery script.

## What passed on Gregale

On December 1, 2026, we ran this drill on a disposable Scale app with a
separate managed PostgreSQL database and Prisma ORM 7.10.1. The baseline
returned the seeded note. The additive migration shipped and the new revision
returned `published: true`. That revision reached 100% traffic.

For the last deploy, the release task ran the deliberate failing migration,
exited with code 1, and Gregale recorded `release_command_failed` and 0%
candidate traffic. The last good revision stayed at 100%; an owner-authorized
GET on that exact revision returned the same HTTP 200 JSON after the failure.
This did not test database rollback or anonymous access.

## What happens when a migration fails?

Rejecting the new release is **not** the same as rolling the database back.
The old revision may keep serving after statements have changed the shared
database. Use additive, backward-compatible migrations during rolling
deploys. If a failing migration partially applied, inspect what committed
before using a [Prisma production recovery workflow](https://www.prisma.io/docs/orm/prisma-migrate/workflows/patching-and-hotfixing).
Do not rerun blindly or run `prisma migrate dev` against production.

Sources: [Prisma v7 migrate deploy](https://www.prisma.io/docs/cli/v7/migrate/deploy),
[Prisma v7 config](https://www.prisma.io/docs/orm/v7/reference/prisma-config-reference),
[Gregale release commands](https://gregale.dev/docs/releases.md), and [Managed PostgreSQL](https://gregale.dev/docs/postgres.md).

## Questions

### Where should I run prisma migrate deploy in production?

Run it once in a release step before the candidate takes traffic. Do not run it in the application entrypoint, where it could run on every replica or restart.

### How do I set a direct URL for Prisma migrations in v7?

Set datasource.url in prisma.config.ts to the direct migration credential. Configure the application driver adapter independently with the pooled DATABASE_URL.

### Does blocking a failed release roll the database back?

No. The candidate can be blocked while earlier statements have already changed the database. Use backward-compatible migrations and inspect schema state before any retry.

Canonical URL: https://gregale.dev/articles/prisma-migrate-deploy-before-traffic

