Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Prisma schema update without erasing the PostgreSQL data?

I have a PostgreSQL db that is used by a Nest.Js / Prisma app.

We changed the name of a field in the Prisma schema and added a new field.

Now, when we want to update the PostreSQL structure, I'm running, as suggested by Prisma, the following commands:

npx prisma generate 

and then

npx prisma migrate dev --name textSettings-added --create-only

The idea is to use the --create-only flag to review the migration before it is actually made.

However, when I run it I get a list of the changes to be made to the DB and the following message:

 We need to reset the PostgreSQL database "my_database" at "[email protected]:5432".
Do you want to continue? All data will be lost.

Of course I choose not to continue, because I don't want to lose the data. Upon inspection I see that the migration file actually contains DROP TABLE for the tables that I simply wanted to modify. How to avoid that?

So how do I run the update without affecting the data?

UPDATE: I saw that running with --create-only creates a migration which can then be implemented on the DB level using prisma migrate dev, however, in that migration file there are still some commands that drop my previous tables because of some new parameters inside. How can I run prisma migration without deleting my PostgreSQL data?

UPDATE 2: I don't want Prisma to drop my tables when I just updated them. The migration file generated, however, drops them and then alters them. Do you know what's the best procedure to avoid this drop? I saw somewhere I could first manually update the DB with the new options and then run the migration, so Prisma can find a way to update it, but that seems too manual to me... Maybe some other ideas?

like image 906
Aerodynamika Avatar asked Dec 08 '25 05:12

Aerodynamika


2 Answers

For some cases like renaming tables or columns, Prisma's generated migration files need to be updated if they already contain data.

If that applies to your use case, Prisma's docs suggest to:

  1. Make updates to the prisma schema, e.g.:
  model Profile {
    id        Int    @id @default(autoincrement())
 -   biograpy  String
 +   biography String
    userId    Int    @unique
    user      User   @relation(fields: [userId], references: [id])
  }
  1. Create migration file without applying it:

    Run npx prisma migrate dev --create-only

    NB: the --create-only flag avoids the application

  2. Update the migration script to remove the drops and instead write your custom query, e.g.:

-- inside migrations/your_migration.sql

RENAME biograpy TO biography
  1. Save and apply the migration: npx prisma migrate dev

Note that those changes can lead to downtime (renaming a field or model), for which they have outlined the expand and contract pattern.

like image 119
p6l-richard Avatar answered Dec 11 '25 00:12

p6l-richard


It might be a Prisma bug: https://github.com/prisma/prisma/issues/8053

I also recently had this situation. It probably should not try to run migration if you only want to create migration file.

But overall it is expected with Prisma to recreate your db sometimes. If you migration is breaking then it will be required to reset the data anyway when you apply it.

I suggest you to create some seeding script so you could consistently re-create the database state, it's very useful for your development environment.

More info

like image 34
Danila Avatar answered Dec 10 '25 22:12

Danila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!