P
Prisma•3w ago
jermahs

Migration Modified After Applied (but checksums are the same)

I have not ad any issues with my procedures yet, but today when I went to modify my chema and generate a new migration, I got the following
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": SQL Server database

- The migration `20241016154659_feature_flags_uq_fk` was modified after it was applied.
- The migration `20241024155043_fk_survey_request_resends` was modified after it was applied.

We need to reset the database schema

You may use prisma migrate reset to drop the development database.
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": SQL Server database

- The migration `20241016154659_feature_flags_uq_fk` was modified after it was applied.
- The migration `20241024155043_fk_survey_request_resends` was modified after it was applied.

We need to reset the database schema

You may use prisma migrate reset to drop the development database.
However, these are migrations that have been run a long time ago. I verified the checksum of my migration.sql files and the value stored in the db. They are the same. How do I get past this?
7 Replies
Prisma AI Help
Prisma AI Help•3w ago
Skipping coffee for instant knowledge! Let's brew that AI answer now. This issue with migrations being reported as modified despite having matching checksums is a known problem in Prisma Migrate. Let me explain what might be happening and how to resolve it. Why This Happens Based on the knowledge sources, this error can occur for several reasons: - Line ending differences (CRLF vs LF) between different operating systems - Git configuration settings that transform line endings - Issues with rolled-back migrations that were later modified and reapplied - Discrepancies in how Prisma tracks migration history Solutions Here are some approaches to resolve this issue: 1. Use the migrate resolve command You can try using the prisma migrate resolve command to tell Prisma that these migrations have been properly applied:
npx prisma migrate resolve --applied 20241016154659_feature_flags_uq_fk
npx prisma migrate resolve --applied 20241024155043_fk_survey_request_resends
npx prisma migrate resolve --applied 20241016154659_feature_flags_uq_fk
npx prisma migrate resolve --applied 20241024155043_fk_survey_request_resends
2. Manual database fix If the above doesn't work, you can manually update the migration records in the database: 1. Connect to your SQL Server database 2. Update the checksums in the _prisma_migrations table to match the current files 3. Generate and update checksums You can generate the correct checksums and update them in the database:
const { createHash } = require('crypto');
const { readFileSync, readdirSync } = require('fs');
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function fixChecksums() {
for (const name of readdirSync("prisma/migrations").filter((e) =>
e.startsWith("20")
)) {
const checksum = createHash("sha256")
.update(readFileSync(`prisma/migrations/${name}/migration.sql`))
.digest("hex");
await prisma.$executeRaw`UPDATE _prisma_migrations SET checksum = ${checksum}, applied_steps_count = 1 WHERE migration_name = ${name}`;
}
}

fixChecksums()
.then(() => console.log('Checksums updated'))
.catch(console.error)
.finally(() => pris
ma.$disconnect());
const { createHash } = require('crypto');
const { readFileSync, readdirSync } = require('fs');
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function fixChecksums() {
for (const name of readdirSync("prisma/migrations").filter((e) =>
e.startsWith("20")
)) {
const checksum = createHash("sha256")
.update(readFileSync(`prisma/migrations/${name}/migration.sql`))
.digest("hex");
await prisma.$executeRaw`UPDATE _prisma_migrations SET checksum = ${checksum}, applied_steps_count = 1 WHERE migration_name = ${name}`;
}
}

fixChecksums()
.then(() => console.log('Checksums updated'))
.catch(console.error)
.finally(() => pris
ma.$disconnect());
This script will recalculate checksums for all migrations and update them in the database Github Issue #12666. 4. Ensure consistent line endings To prevent this issue in the future, consider adding a .gitattributes file to your project with:
* text=auto eol=lf
*.{png,jpg,jpeg,gif,webp,woff,woff2} binary
* text=auto eol=lf
*.{png,jpg,jpeg,gif,webp,woff,woff2} binary
This will ensure consistent line endings across different operating systems Github Issue #7077. Important Note Prisma's documentation strongly advises against editing or deleting migrations that have already been applied, as this can lead to inconsistencies between environments. If you need to make changes, it's generally better to create new migrations rather than modifying existing ones Prisma Docs.
jermahs
jermahsOP•3w ago
wouldn't different line endings cause a different checksum? In this case, my checksums are the same, not different.
Prisma AI Help
Prisma AI Help•3w ago
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Nurul
Nurul•3w ago
Hey 👋 Did this error start occurring randomly, or after a prisma version upgrade? Do you get this error when you are trying to create a new migration with npx prisma migrate dev command?
jermahs
jermahsOP•3w ago
yes I did upgrad prisma @Nurul sorry I misread your comment/question. I got the error randomly, I then tried to upgrade prisma in order to see if that would fix it; it did not. And yes, I am getting the error when running pnpm prisma migrate dev and with --create-only
hari_narayan
hari_narayan•3w ago
First just backup u r db ....... And just do like below Yes ... To solve this just generate new checksum copy and paste it in db checksum..... But now whatever that file will be skip..... For example u have generate checksum for J file as y Then j files migration will be skipped automatically..... And new migration will be created https://discord.com/channels/937751382725886062/1342542831247560848
jermahs
jermahsOP•2w ago
@hari_narayan the checksum already appears to be the same. @Nurul any follow up on this?

Did you find this page helpful?