Database schema drift recovery after partial migration failure in Prisma
0 reputation · 29 Jul 2022, 02:57 UTC
0 reputation · 29 Jul 2022, 02:57 UTC
Prisma Migrate maintains a _prisma_migrations table that tracks which migrations have been applied to the database. This table includes an applied column that indicates whether a migration has completed successfully.
When a migration fails partway through execution—particularly one that includes multiple DDL statements—the database may end up in an inconsistent state. Some schema changes may have applied while others did not, yet the migration record may incorrectly reflect the state. The prisma migrate dev command may detect this drift and propose a reset, but the actual behavior depends on the database engine's transaction support and how Prisma handles partial failures.
The core question centers on how Prisma determines and reports migration state when a failure occurs mid-migration. Specifically, when the applied column in _prisma_migrations is set to false but some DDL changes have already committed to the database, what is the recommended path to reconcile this state without data loss?
prisma migrate dev detects drift between schema.prisma and the database, what is the exact condition that triggers a destructive reset versus a migration replay?prisma migrate resolve --rolled-back correctly revert the internal tracking state, and what manual steps are required to ensure the database schema matches the resolved state?_prisma_migrations table to align with a known database state after a failed migration, without triggering an automatic reset?33775 reputation · 29 Jul 2022, 03:29 UTC
prisma migrate devPrisma compares the current database schema with the expected schema derived from all migrations that have a non-null finished_at and a null rolled_back_at. The comparison is performed by the prisma migrate status logic, which produces three categories:
migrations folder but have no finished record.When prisma migrate dev runs, it follows these rules:
prisma migrate reset, which drops all user tables and rebuilds the database from scratch.--create-only flag is used, Prisma will only create the migration history table and will never perform a reset, even if drift is present.prisma migrate resolve --rolled-backRunning prisma migrate resolve --rolled-back --migration-name <name> changes the migration record as follows:
rolled_back_at to the current timestamp.finished_at as NULL.Because the command does not revert the DDL, you must manually ensure the database matches the state that would exist if the migration had never run. Typical manual steps are:
down statements manually (or drop the affected tables/columns).schema.prisma and any earlier migrations.prisma migrate dev again to apply any remaining pending migrations.Failure to fully revert the schema before resolving will cause a drift that Prisma will detect on the next dev run, potentially triggering another reset.
_prisma_migrationsOfficial Prisma documentation does not provide a step-by-step guide for editing _prisma_migrations directly. The supported workflow is:
prisma migrate resolve --applied if the migration's DDL is now present but the record is missing, or --rolled-back if you have undone the changes.prisma migrate status to confirm that the history matches the database.prisma migrate dev to apply the rest of the migrations.Direct SQL edits (e.g., changing finished_at or checksum) are possible but bypass Prisma's integrity checks and can lead to checksum mismatches or future drift detection. Use them only as a last resort, and always backup the _prisma_migrations table first.
These recommendations assume:
Because the behavior can differ between engines, please confirm your database type before proceeding. If you are on a non-transactional engine, the steps below are the safest path.
prisma migrate status to see the drift report.\dt in psql, SHOW TABLES in MySQL).prisma migrate resolve --rolled-back --migration-name <name> (or --applied if you completed the migration).prisma migrate dev to apply any remaining migrations.prisma migrate status shows no pending or failed migrations.Follow this workflow and you should avoid a destructive reset while keeping the database and migration history in sync.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.