20

what is the difference between prisma db push and prisma migrate dev ? When should I use one over the other. Docs say that prisma db push is about schema prototyping only and I don't understand what does that mean.

Ahmed Nawaz Khan
  • 1,451
  • 3
  • 20
  • 42
  • 1
    I’m voting to close this question because it explained clearly in the docs https://www.prisma.io/docs/concepts/components/prisma-migrate/db-push#choosing-db-push-or-prisma-migrate – Danila Jul 27 '21 at 08:55

4 Answers4

9

They serve two different environments. The prisma db push is not to be used in your production environment, as stated in the docs

db push uses the same engine as Prisma Migrate to synchronize your Prisma schema with your database schema, and is best suited for schema prototyping. The db push command:

  1. Introspects the database to infer and executes the changes required to make your database schema reflect the state of your Prisma schema.

  2. By default, after changes have been applied to the database schema, generators are triggered (for example, Prisma Client). You do not need to manually invoke prisma generate.

  3. If db push anticipates that the changes could result in data loss, it will:

  • Throw an error
  • Require the --accept-data-loss option if you still want to make the changes

Note: db push does not interact with or rely on migrations. The migrations table will not be updated, and no migration files will be generated.

The prisma migrate dev is used in you local environment, as explained in the docs

migrate dev is a development command and should never be used in a production environment.

This command:

  1. Replays the existing migration history in the shadow database in order to detect schema drift (edited or deleted migration file, or a manual changes to the database schema)
  2. Applies pending migrations to the shadow database (for example, new migrations created by colleagues)
  3. Generates a new migration from any changes you made to the Prisma schema before running migrate dev
  4. Applies all unapplied migrations to the development database and updates the _prisma_migrations table
  5. Triggers the generation of artifacts (for example, the Prisma Client)

The migrate dev command will prompt you to reset the database in the following scenarios:

  • Migration history conflicts caused by modified or missing migrations
  • The database schema has drifted away from the end-state of the migration history

If you have any other question regarding this, there is this comparison in the docs explaining when to use one or the other.

nburk
  • 22,409
  • 18
  • 87
  • 132
  • 8
    This is dangerously incorrect. `prisma db push` is never used against a production environment. As stated in the docs it is for local rapid prototyping only. All operations against production environments should be performed by deploying migrations. – Lauri Piispanen Sep 05 '21 at 18:27
  • 1
    Note that if you are using Prisma with MongoDB you _do_ use `db push` instead of `prisma migrate dev/deploy`. https://www.prisma.io/docs/concepts/database-connectors/mongodb#considerations – Olivier Wilkinson Jul 18 '22 at 10:36
1

From the docs:

Use db push to prototype a schema at the start of a project and initialize a migration history when you are happy with the first draft

Use db push to prototype a change to an existing schema, then run prisma migrate dev to generate a migration from your changes (you will be asked to reset)

So the key for me here, was that only the first time I had to run db push (into a heroku app with heroku postgres). After that, migrations all the time...

ismaestro
  • 7,561
  • 8
  • 37
  • 50
0

I had changed from prisma db push to prisma migrate dev Because prima db push can't allow me to change the column's name to the different one you want.

Example: The table USER has a column employee_name, you want to change it to name. If you use prisma db push your data will be lost. You must use prisma migrate dev to do this one.

Bob Nguyen
  • 11
  • 1
0
  • Prisma db push syncs (and formats) your prisma schema to that of your database schema.
  • While prisma migrate dev "command automatically generates SQL migration files (saved in /prisma/migrations) and applies them to the database".

The main difference between both is the generation of the migration files.