Hi team, so after I use the prisma to migrate, it ...
# orm-help
j
Hi team, so after I use the prisma to migrate, it will remove the data from the database, so what is the best way to retain the data from postgres after migration is done? Do I need to export the data out of the database first (pg-dump dbname > outfile), do the prisma migrate, then import the data back to the database (psql dbname < infile)?
k
does
migrate
removes the data? i don’t think so. For me, It only removes the data if i use
--reset
.
j
It might have been me using it wrong. I run the prisma migrate dev and is ask me to reset the database…
What are the steps that you do if you have changes to your schema such as adding new column, changing data type of the column?
k
i do
npx prisma migrate dev --name "migration name"
to create migrations whenever i have changes to the db structure. Here’s a sample i did just now, i tested creating a new column called
testColumn
and did the command above. Here’s the full result from cli:
Copy code
/usr/app # npx prisma migrate dev --name add test column
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "test-db", schema "public" at "db:5432"

The following migration(s) have been created and applied from new schema changes:

migrations/
  └─ 20210531022200_add/
    └─ migration.sql

Your database is now in sync with your schema.

✔ Generated Prisma Client (2.23.0) to ./node_modules/@prisma/client in 59ms
I never encountered
migrate
asking to reset the database. I think there’s a conflict on your database structure with your migrations, hence it asks you to reset it to resolve it. can you try
prisma migrate reset --force --preview-feature
? it clears the DB and applies all migrations, then try normal
migrate
see if still asks you to reset it. Doing
reset
also seeds data if you have one.
In general, you should not edit or delete a migration that has already been applied. Doing so can lead to inconsistencies between development and production environment migration histories, which may have unforeseen consequences - even if the change does not appear to break anything at first.
and then
Run
prisma migrate dev
- Prisma Migrate detects that a migration has changed, and asks to
reset
the database:
so it seems like i was right, you must have deleted a migration file yourself or you made changes to the db manually without using migration. Doing that would lead to inconsistencies hence it asks you to reset.
👍 1
j
It probably it. So from second migration it should not saying that then... thanks buddy
k
yea, after the first reset, it should not ask to reset again. Could be bug or there’s something external that keeps changing your db structure. Anyways if it haven’t been deployed to production yet, i would personally just delete all migration files and start over incase a migration file was just bugged. other command to apply migration is
prisma migrate deploy
, this is more suited for production though as it says on doc.
💯 1
b
You will be using Prisma dB push and pull to retain your data until when you're done and you want to migrate then you can finally do it. When you want to apply changes from Prisma schema to database use Prisma db push When you want to pull from the database and apply in the schema use Prisma db pull
👍 1
j
@Keanu Gargar confirm that subsequence migrate doesn’t have issues that prisma ask to reset the data. So what do you do to make sure that the database will always be in sync with the prisma schema? What happen when you do migrate and you run into issue that ask you to reset the database, as there are conflicts with your database and the prisma schema? You don’t want to do the reset database right, as you already have data in your database.
e
In my experience, Prisma will reset a database if the existing data cannot conform to the new schema. 🙂 For example, something that happens regularly for me is that a new non-nullable column is added to a table. Without declaring a default value for this new column, the existing data violates the schema and will not work. Note that this happens at the database level. Prisma just detects this and prompts you to reset the database. In this case, I have 2 options: • Apply a migration to add the new column with a default value • Apply a migration to the new column as nullable, backfill existing records, then apply another migration to make the new column non-nullable Without seeing your schema, this is all I could share, hope that helps! 🙂
👍 1
j
Thanks Eddy 🙂