Prisma migration is bad state

Viewed 3839

We seem to have gotten our prisma migrations into a bad state. When we get the latest code and run

prisma migrate dev

we get

Migration 20210819161149_some_migration failed to apply cleanly to the shadow database. Error code: P3018 Error: A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve

Migration name: 20210819161149_some_migration

Database error code: 1065

All the migrations in source control do match the ones in the _prisma_migrations table so I'm not sure why it thinks 20210819161149_some_migration failed. There is nothing in the logs column in _prisma_migrations for that record. I think what happened is a developer applied the migration then changed the migration.sql for it after the fact.

Anyway, we followed the steps outlined https://pris.ly/d/migrate-resolve but they don't seem to resolve the issue. It first suggests running

prisma migrate resolve --rolled-back "20210819161149_some_migration"

but that results in

Error: P3012

Migration 20210819161149_some_migration cannot be rolled back because it is not in a failed state.

So then we tried to mark it as applied

prisma migrate resolve --applied "20210819161149_some_migration"

but that results in this error

Error: P3008

The migration 20210819161149_some_migration is already recorded as applied in the database.

We also tried running

 prisma migrate deploy

Which gives

13 migrations found in prisma/migrations WARNING The following migrations have been modified since they were applied: 20210819161149_some_migration

but you still get the same issue above when running prisma migrate dev.

Is there any way to get prisma happy again without deleting all the data?

1 Answers

A possible workaround would involve baselining your development database based on your current schema/migration history. You would need a separate backup database and you will lose your existing migration history, but it should retain your data.

Here's what the process would look like

  1. Delete all migration history from your prisma folder as well as the _prisma_migrations table in your database.
  2. Create a new backup database.
  3. Connect the project to your backup database and run prisma migrate dev --name baseline_migration . This will generate a migration matching your current prisma schema.
  4. Connect back to your main Database and baseline the generated migration by running prisma migrate resolve --applied 20210426141759_baseline_migration (The numbers at the beginning of your migration name will differ).

The reason you'd be creating a backup database and running the initial baseline migrations in that backup database is because you don't want to lose the data in your main database. I realize this is not an ideal solution, but it might work if it's very important for you to keep your data while retaining your existing dev workflow.

This article on Adding Prisma Migrate to an existing project is also worth a read.

Related