-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Bug description
- Backend: Node.js
- ORM: Prisma
- Database: PostgreSQL
π Description
We are using Node.js and Prisma in our project. Recently, we attempted a migration that resulted in all data being reset after the following prompt:
All data will be reset
Unfortunately, I mistakenly accepted it, and the data was wiped. We had a backup, so I restored the data using the backup file. However, even after restoring, we encountered several migration-related errors.
π What I Did to Fix It
After researching, I tried to sync the migration history with the existing database using the migrate diff
approach:
npx prisma migrate diff \
--from-empty \
--to-schema-datamodel prisma/schema.prisma \
--script > prisma/migrations/sync_db_state/migration.sql
Then applied it manually:
psql -h <host> -U <user> -d <database> -f prisma/migrations/sync_db_state/migration.sql
Then resolved the migration manually:
npx prisma migrate resolve --applied sync_db_state
Finally, I tried to create a new migration with:
npx prisma migrate dev --create-only
β Error Faced
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "mydb", schema "public" at "myip:5432"
Error: P3006
Migration `sync_db_state` failed to apply cleanly to the shadow database.
Error:
ERROR: type "UploadStatus" already exists
0: schema_core::state::DevDiagnostic
at schema-engine/core/src/state.rs:276
π‘ What I Need Help With
- How can I resolve this
type "UploadStatus" already exists
error? - Is my approach using
migrate diff
andresolve --applied
the correct way to sync Prisma with an existing database? - What's the best practice to recover Prisma migrations after restoring a backup?
Severity
π¨ Critical: Data loss, app crash, security issue
Reproduction
Expected vs. Actual Behavior
Environment
- Backend: Node.js
- ORM: Prisma
- Database: PostgreSQL
π Description
We are using Node.js and Prisma in our project. Recently, we attempted a migration that resulted in all data being reset after the following prompt:
All data will be reset
Unfortunately, I mistakenly accepted it, and the data was wiped. We had a backup, so I restored the data using the backup file. However, even after restoring, we encountered several migration-related errors.
π What I Did to Fix It
After researching, I tried to sync the migration history with the existing database using the migrate diff
approach:
npx prisma migrate diff \
--from-empty \
--to-schema-datamodel prisma/schema.prisma \
--script > prisma/migrations/sync_db_state/migration.sql
Then applied it manually:
psql -h <host> -U <user> -d <database> -f prisma/migrations/sync_db_state/migration.sql
Then resolved the migration manually:
npx prisma migrate resolve --applied sync_db_state
Finally, I tried to create a new migration with:
npx prisma migrate dev --create-only
β Error Faced
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "mydb", schema "public" at "myip:5432"
Error: P3006
Migration `sync_db_state` failed to apply cleanly to the shadow database.
Error:
ERROR: type "UploadStatus" already exists
0: schema_core::state::DevDiagnostic
at schema-engine/core/src/state.rs:276
- How can I resolve this
type "UploadStatus" already exists
error? - Is my approach using
migrate diff
andresolve --applied
the correct way to sync Prisma with an existing database? - What's the best practice to recover Prisma migrations after restoring a backup?
Frequency
Consistently reproducible
Does this occur in development or production?
Only in development (e.g., CLI tools, migrations, Prisma Studio)
Is this a regression?
Workaround
Prisma Schema & Queries
// Add relevant schema.prisma snippet
// Add relevant Prisma Client query
Prisma Config
// Add your `prisma.config.ts`
Logs & Debug Info
// Debug logs here
Environment & Setup
- OS:
- Database:
- Node.js version:
Prisma Version
// Prisma version output