skip to content

In Prisma 7, how do you adopt Prisma Migrate on an existing production database without recreating its tables or losing data?

level: seniorimportance: nice to knowfreq 28%

answer

  1. P3005 on a non-empty database
  2. introspect, then an initial migration
  3. the 0_ prefix sorts first
  4. resolve --applied, not deploy

basics

~20 s

Baseline it: introspect with prisma db pull, write an initial migration with prisma migrate diff from empty to the schema, then mark it applied on each existing database with prisma migrate resolve --applied so deploy never runs it there.

solid answer

~40 s

Running `prisma migrate deploy` against a non-empty database with no migration history fails with `P3005`, and `prisma migrate status` reports that the database is not managed by Prisma Migrate. Baselining fixes that. Run `prisma db pull` to write models for the existing tables into the Prisma schema. Create `prisma/migrations/0_init` and fill it with `prisma migrate diff --from-empty --to-schema prisma/schema.prisma --script`; the `0_` prefix makes it sort before later timestamped migrations. Review it and add what the schema cannot express, such as triggers or views. Then, on each database that already has these tables, run `prisma migrate resolve --applied 0_init`, which records the migration without executing it. From then on `migrate deploy` skips `0_init` there and applies only newer migrations, while fresh development databases build themselves from it.

code

bash · 9 lines
bash
npx prisma db pull
mkdir -p prisma/migrations/0_init
npx prisma migrate diff \
  --from-empty \
  --to-schema prisma/schema.prisma \
  --script > prisma/migrations/0_init/migration.sql

# On each existing database (production, staging):
npx prisma migrate resolve --applied 0_init

go deeper

for a junior

Know that an existing database needs a baseline migration marked as applied before Prisma Migrate can manage it.

for a middle

List the steps (db pull, the 0_init migration from migrate diff, resolve --applied) and say what each one writes and where.

for a senior

Explain the P3005 and P3008 failure modes, how to verify each environment matches the baseline before resolving, and what the generated SQL leaves out.

for a principal

Plan the adoption across every environment of a legacy database, including who resolves which database and how unrepresentable objects stay in the history.

## The problem baselining solves Prisma Migrate assumes every database was built by its own migration history, starting from empty. A database that predates Prisma breaks that assumption: it already has tables, data and constraints, but no `_prisma_migrations` table. - `prisma migrate deploy` against it fails with `P3005`: "The database schema is not empty. Read more about how to baseline an existing production database". - `prisma migrate status` reports "The current database is not managed by Prisma Migrate" and, if local migrations exist, suggests `prisma migrate resolve --applied` with the oldest one. - `prisma migrate dev` against it would treat every existing table as drift and want a reset, which is not an option for a database whose data you need. **Baselining** means creating an initial migration that describes the database exactly as it already is, and telling each existing database that this migration has already been applied. ## The steps 1. **Introspect.** `prisma db pull` reads the database and writes matching models into the Prisma schema. Review the result before building on it: model names, relations and field types. 2. **Start a clean history.** If a `prisma/migrations` folder exists, archive it, then create `prisma/migrations/0_init/`. Prisma lists migration folders in lexicographic order, so the `0_` prefix sorts before the timestamp-named migrations created later. 3. **Generate the SQL.** `prisma migrate diff --from-empty --to-schema prisma/schema.prisma --script > prisma/migrations/0_init/migration.sql` writes the SQL that would build the current schema from nothing. 4. **Review and complete it.** `migrate diff` only knows what the Prisma schema can represent. Triggers, views, stored procedures and similar objects are missing; add them by hand so that a database built from the history really matches production. 5. **Mark it applied where it already is.** On production, staging and any other existing copy, run `prisma migrate resolve --applied 0_init`. This inserts the row into `_prisma_migrations` without running the SQL. 6. **Carry on normally.** New changes go through `prisma migrate dev`; `prisma migrate deploy` skips `0_init` wherever it is recorded and applies only what comes after. ## What each environment sees | Database | What happens to `0_init` | |---|---| | Production, staging (existing data) | recorded once with `resolve --applied`, never executed | | A new developer's local database | applied by `migrate dev` or `migrate reset`, building the schema from scratch | | A fresh CI or preview database | applied by `migrate deploy` like any other migration | ## Rolling it out in a team The baseline is a change like any other, but its order of operations matters: 1. Commit the updated Prisma schema and `prisma/migrations/0_init/` together, with the reviewed SQL. 2. Before the first pipeline run that executes `prisma migrate deploy`, someone with access runs `prisma migrate resolve --applied 0_init` on each existing database. 3. From then on, `prisma migrate status` against those databases reports the schema as up to date, and new migrations flow through the normal `migrate dev` and `migrate deploy` path. If the pipeline wins the race and deploys before step 2, nothing is damaged: deploy stops with `P3005` before running any SQL, and the release can be retried once the database is resolved. ## Pitfalls - **Forgetting an environment.** Any existing database that was not resolved still has no history, and the next deploy there fails with `P3005`. - **Resolving twice.** `resolve --applied` on a migration already recorded as applied fails with `P3008`; it is a one-time step per database. - **Environments that differ.** A baseline generated from one database is wrong for another whose schema has drifted. Before resolving, compare: `prisma migrate diff --from-migrations prisma/migrations --to-config-datasource --exit-code` exits with 2 when that database differs from what the history produces. Diffing from a migrations directory needs a shadow database. - **An incomplete baseline.** If step 4 is skipped, development databases built from the history lack the triggers or views production has, and behaviour differs between them in ways no error reports. In Prisma 7, `migrate diff` and `migrate resolve` take their connection from the Prisma config file (the `--from-url` flag was removed), so point the config at the database you are working on before resolving it.

  • Why is the baseline folder named with a `0_` prefix?
    Prisma lists migration folders in lexicographic order. Migrations created later are named with a timestamp, so a `0_` prefix guarantees the baseline sorts first and runs before them on a fresh database. Any name that sorts first works; the prefix is the conventional choice.
  • Why not just run `prisma migrate dev` against production to create the first migration?
    `migrate dev` would replay an empty history in the shadow database, see every existing table as drift, and say the database must be reset. It is a development command, it needs rights to create databases, and its way out is dropping the data you are trying to keep.

It is like starting a renovation log for a house that is already standing: page one describes the house as it is and is signed off as done, so builders only carry out the entries written after it.

  • Run migrate deploy first; Prisma skips tables that already exist
  • migrate resolve --applied executes the baseline SQL once
  • prisma db pull alone puts the database under Prisma Migrate
  • migrate diff --from-empty also captures triggers and views
  • Only production needs resolving; staging picks the baseline up

Assumes Prisma 7, where migrate diff and migrate resolve take the connection from the Prisma config file; the baselining steps themselves are older.