Migrating a Prisma Database: My Workflow

––– views

1 min read

Prisma migration commands are long and confusing, here is a list of 5 commands that you need to effectively run migrations

List of commands

JSON package.json

"scripts": {
"migrate:status": "prisma migrate status",
"migrate:dev": "prisma migrate dev",
"migrate:resolve": "prisma migrate resolve --applied $MIGRATION",
"migrate:deploy": "prisma migrate deploy",
"migrate:push": "prisma db push"

  • :status - shows which migration you have locally but not applied to target database
  • :dev - creates a new migration from the active changes in schema.prisma
  • :resolve - applies the new migration to the target database
  • :deploy - deploys the changes to the database (but they are not live yet)
  • :push - command to sync local changes with database
  • My migration workflow

    In my prisma/.env file I keep all the different databases that I need to connect to.


    // the db we're targeting is on the bottom

    The first one (dbname) is an empty local database, whenever I need to create a new migration I always switch to this DB, to not lose any data from the one I’m using.

    I suggest you have 2 local databases one for keeping test data and one for migration

    Action steps to achieve a migration

  • Do the model changes in schema.prisma
  • Start by changing the target database to a local one
  • run npm run migrate:dev
  • At this point, you will be asked to name the migration
  • After successfully creating a new migration change to the target database you want to migrate
  • By running npm run migrate:status you’ll see all the pending migrations, if you want to apply all the pending migrations run npm run migrate:deploy
  • To migrate a single migration run MIGRATION=”new_migration_name” npm run migrate:resolve - This will apply the migration to the current target DB
  • Now run npm run migrate:deploy
  • This will deploy the changes
  • Finally, run npm run migrate:push
  • If all goes well you should see: “🚀 Your database is now in sync with your Prisma schema.“
  • 🔥
    When adding new properties to an existing model make sure they have a default value or are not required.

    To comment please authenticate