#Prisma Migrate: How to create a baseline from an existing production DB without resetting local data
1 messages · Page 1 of 1 (latest)
To baseline your existing production database and avoid resetting or losing data, follow these steps:
-
Delete or archive your local
prisma/migrationsfolder to remove any old migration history that may conflict with your current database state. -
Create a new migration directory for your baseline migration:
mkdir -p prisma/migrations/0_init -
Generate a baseline migration SQL file that represents your current database schema:
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/0_init/migration.sqlThis command creates a migration SQL file that, if applied to an empty database, would recreate your current schema. It does not modify your database.
-
Mark the baseline migration as applied so Prisma knows your database already matches this migration:
npx prisma migrate resolve --applied 0_initThis updates the
_prisma_migrationstable to reflect that the baseline migration has already been applied, without running any destructive operations. -
Verify everything is in sync by running:
npx prisma migrate devPrisma should now recognize your database as up-to-date, and you can safely generate and apply new migrations going forward.
Note: Do not use prisma migrate dev --create-only on a non-empty database, as it will prompt for a reset and may wipe your data. The above approach is the recommended way to baseline an existing database without data loss Baselining a database, Baseline with Prisma ORM, TypeScript, and PostgreSQL.
This process ensures your local and production environments are in sync and lets you continue using Prisma Migrate safely.
🤔 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.
I have done all the steps above before I came to ask the question
This is what Prisma says
➜ database git:(phase_2) ✗ npx prisma migrate resolve --applied 0_init
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "attendanceSystemFix", schema "public" at "localhost:6543"
Error: P3008
The migration 0_init is already recorded as applied in the database.
!close