#Prisma Migrate: How to create a baseline from an existing production DB without resetting local data

1 messages · Page 1 of 1 (latest)

low micaBOT
#

You chose to compile with the bot. One moment while I piece together your answer…

low micaBOT
# low mica You chose to compile with the bot. One moment while I piece together your answer...

To baseline your existing production database and avoid resetting or losing data, follow these steps:

  1. Delete or archive your local prisma/migrations folder to remove any old migration history that may conflict with your current database state.

  2. Create a new migration directory for your baseline migration:

    mkdir -p prisma/migrations/0_init
    
  3. 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.sql
    

    This command creates a migration SQL file that, if applied to an empty database, would recreate your current schema. It does not modify your database.

  4. Mark the baseline migration as applied so Prisma knows your database already matches this migration:

    npx prisma migrate resolve --applied 0_init
    

    This updates the _prisma_migrations table to reflect that the baseline migration has already been applied, without running any destructive operations.

  5. Verify everything is in sync by running:

    npx prisma migrate dev
    

    Prisma 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.

low micaBOT
tawdry ruin
#

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