Get started • Features • Docs • Workflow • Supported databases
Prisma Migrate is a powerful database schema migration tool. It uses a declarative data modelling syntax to describe your database schema. Prisma Migrate also stores your entire migration history and easily lets you revert and replay migrations. When migrating your database with Prisma Migrate, you can run provide before- and after-hooks to execute scripts, e.g. to populate the database with required values during a migration.
WARNING: Prisma Migrate is currently in an experimental state. The version available has a number of limitations that make it unsuitable for production workloads, including missing features, limited performance and stability issues.
- Declarative data modelling syntax
- Supports relational and document databases (more coming soon)
- Keeps a migration history
- Before- and after hooks to run scripts for complex migrations
- Simple defaults, optional complexity for advanced use cases
- Revert and replay migrations
- Works with existing databases using schema introspection
- CLI to support all major workflows
You can find comprehensive documentation for Prisma Migrate in the Prisma 2 docs.
Specify the connection details for your database as a data source in your Prisma schema file. The connection details might differ per database, but most commonly you'll provide the following:
- Host: The IP address or domain name of the machine where your database server is running.
- Port: The port on which your database server is listening.
- User & password: Credentials for your database server.
Here is an example project file that connects to a local PostgreSQL database:
// schema.prisma
datasource postgres {
url = "postgresql://user:password@localhost:5432"
provider = "postgres"
}
generator client {
provider = 'prisma-client-js'
}
The data model definition is a declarative and human-readable representation of your database schema. Here is the project file from above extended with a sample data model:
// schema.prisma
datasource postgres {
url = "postgresql://user:password@localhost:5432"
provider = "postgres"
}
generator client {
provider = 'prisma-client-js'
}
model User {
id Int @id
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
posts Post[]
}
model Post {
id Int @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
author User
title String
published Boolean @default(false)
}
enum Role {
USER
ADMIN
}
If you want to use Prisma Migrate with an existing database, you can introspect your database schema using the Prisma 2 CLI. This generates a declarative data model which provides the foundation for future migrations.
When starting from scratch, you can write your own data model definition inside your Prisma schema file. You can then use the Prisma Migrate CLI commands to migrate your database (Prisma Migrate maps your data model definition to the schema of the underlying database).
Instead of sending SQL migration statements to the database, you need to adjust the data model file to describe your desired database schema. You can express any schema migration you like using the new data model, this includes for example adding a new model, removing a model or updating the fields of a model. You can also add indexes or validation constraints in the data model.
You can create a new migration for your change by running prisma2 migrate save
:
prisma2 migrate save --name "add-comment-model" --experimental
Once you're happy with the changes, you can use the Prisma CLI to migrate your database (i.e. map the adjusted data model to your database). Prisma Migrate's migration engine will generate the corresponding SQL statements and send them to the database for you.
prisma2 migrate up --experimental
Prisma Client JS can be used with the following databases:
- MySQL
- PostgreSQL
- MongoDB (coming very soon)
More databases that will be supported in the future are:
- MS SQL
- Oracle
- Neo4J
- FaunaDB
- ...
Read more about how to contribute to Prisma Migrate here