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:
- Adjust the datamodel file to reflect the new desired schema
- 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:
- Before migration
type Post {
text: String
}
- Goals
- Rename the
Post
type toStory
- Adding the
@rename
directive
# renaming the `Post` type to `Story`
type Story @rename(oldName: "Post") {
text: String
}
- Migrating the database with
prisma deploy
After having the saved the datamodel file with these changes, you need to run:
prisma deploy
- Removing the
@rename
directive manually
type Story {
text: String
}
Renaming a field
Here is an example scenario:
- Before migration
type Post {
text: String
}
- Goals
- Rename the
text
field tocontent
- Adding the
@rename
directive
# renaming the `text` field to `content`
type Post {
content: String @rename(oldName: "text")
}
- Migrating the database with
prisma deploy
After having the saved the datamodel file with these changes, you need to run:
prisma deploy
- Removing the
@rename
directive manually
type Post {
content: String
}