Datamodel & Migrations

Migrations (MySQL)

Overview

When using Prisma with a MySQL database, all your database migrations are performed using the prisma deploy command of the Prisma CLI.

Database migrations with Prisma

There are two steps to every database migration:

  1. Adjust the datamodel file to reflect the new desired schema
  2. Run prisma deploy to apply the changes and perform the migration of the underlying database

Migration operations

Prisma uses temporary directives to perform one-time migration operations. After deploying a service that contain a temporary directive, a temporary directive needs to be manually removed from the type definitions file.

Renaming

The temporary directive @rename(oldName: String!) is used to rename a type or a field of a type.

If the @rename directive is not used, Prisma deletes old type/field before creating the new one, resulting in loss of data!

Renaming a type

Here is an example scenario:

  1. Before migration
type Post {
  text: String
}
  1. Goals
  • Rename the Post type to Story
  1. Adding the @rename directive
# renaming the `Post` type to `Story`
type Story @rename(oldName: "Post") {
  text: String
}
  1. Migrating the database with prisma deploy

After having the saved the datamodel file with these changes, you need to run:

prisma deploy
  1. Removing the @rename directive manually
type Story {
  text: String
}

Renaming a field

Here is an example scenario:

  1. Before migration
type Post {
  text: String
}
  1. Goals
  • Rename the text field to content
  1. Adding the @rename directive
# renaming the `text` field to `content`
type Post {
  content: String @rename(oldName: "text")
}
  1. Migrating the database with prisma deploy

After having the saved the datamodel file with these changes, you need to run:

prisma deploy
  1. Removing the @rename directive manually
type Post {
  content: String
}