title | metaTitle | metaDescription |
---|---|---|
How to upgrade |
How to upgrade from Prisma 1 to Prisma 2 |
Learn how to upgrade your Prisma 1 project to Prisma 2 |
This page helps you make an informed decision on when and how to upgrade from Prisma 1 to Prisma 2.
The upgrade documentation consists of several pages, here's an overview of how to use them:
- How to upgrade (you are here): Starting point to learn about the upgrade process in general.
- Schema incompatibilities: A reference page about the schema incompatibilities between Prisma 1 and Prisma 2. Reading this page is optional but it will give you a better understanding of certain steps in the upgrade process.
In addition to these two pages, there are various practical guides that walk you through an example scenario of the upgrade process:
- Upgrading the Prisma layer: No matter what your Prisma 1 setup looks like, you should always start your upgrade process by following this guide.
Once you're done with that guide, you can choose one of the following four guides to upgrade your application layer:
- Old to new Nexus: Choose this guide if you're currently running Prisma 1 with GraphQL Nexus.
- prisma-binding to Nexus: Choose this guide if you're currently running Prisma 1 with
prisma-binding
and want to upgrade to Nexus. - prisma-binding to SDL-first: Choose this guide if you're currently running Prisma 1 with
prisma-binding
and want to upgrade to an SDL-first GraphQL server. - REST API: Choose this guide if you're currently running Prisma 1 using Prisma client 1 and are building a REST API.
Note: Please let us know if your upgrade scenario is not covered by these guides.
On a high-level, the biggest differences between Prisma 1 and Prisma 2 are the following:
- Prisma 2 doesn't require hosting a database proxy server (i.e., the Prisma server).
- Prisma 2 makes the features of Prisma 1 more modular and splits them into dedicated tools:
- Prisma Client: An improved version of Prisma client 1.0
- Prisma Migrate: Data modeling and migrations (formerly
prisma deploy
).
- Prisma 1 datamodel and
prisma.yml
have been merged into the Prisma schema. - Prisma 2 uses its own modeling language instead of being based on GraphQL SDL.
- Prisma 2 doesn't expose "a GraphQL API for your database" anymore, but only allows for programmatic access via the Prisma Client API. This means Prisma 2 doesn't support Prisma binding any more.
- More powerful introspection allows connecting Prisma 2 to any existing database
Prisma 2 doesn't have full feature parity with Prisma 1 yet. These are the biggest features that are still missing from Prisma 2:
- Database migrations: In Prisma 1, you could migrate your database schema using
prisma deploy
. Prisma 2 aims to bring back this functionality with Prisma Migrate. Until Prisma Migrate is available, you'll have to run your database migrations with plain SQL or another migration tool. - Realtime API (Subscriptions): Prisma 2 currently doesn't have a way to subscribe to events happening in the database and get notified in realtime. It is currently unclear if, when and in what form a realtime API will be added to Prisma 2. For the time being, you can implement realtime functionality using native database triggers, or if you're using GraphQL subscriptions you can consider triggering subscriptions manually inside your mutation resolvers.
- MongoDB support: Prisma 2 doesn't yet support MongoDB. If you're using Prisma 1 with MongoDB and want to upgrade to Prisma 2, you either need to migrate your data into a relational database first or wait until MongoDB is supported.
The database schema that is created when running prisma deploy
in Prisma 1 is only partially compatible with the one that Prisma 2 creates. This section gives a quick overview of the general incompatibilities and the potential workarounds.
Note: For a detailed explanation of the problems and respective workarounds, please refer to the Schema incompatibilities page.
Here's an overview of the different columns:
- Problem: A short description of the problem when upgrading from Prisma 1 to Prisma 2
- SQL: Can this be solved by making a non-breaking change to the SQL schema?
- Prisma schema: Can this be solved by making a non-breaking change to the Prisma 2 schema?
- Breaking Prisma 1: Do the SQL statements break the Prisma 1 setup? This is only relevant when you're choosing the gradual side-by-side upgrade strategy.
Problem | SQL | Prisma schema | Breaking Prisma 1 |
---|---|---|---|
Default values aren't represented in database | Yes | Yes | No |
Generated CUIDs as ID values aren't represented in database | No | Yes | No |
@createdAt isn't represented in database |
Yes | Yes | No |
@updatedAt isn't represented in database |
No | Yes | No |
Inline 1-1 relations are recognized as 1-n (missing UNIQUE constraint) |
Yes | No | No |
All non-inline relations are recognized as m-n | Yes | No | Yes |
Json type is represented as TEXT in database |
Yes | No | No (MySQL) Yes (PostgreSQL) |
Enums are represented as TEXT in database |
Yes | No | No (MySQL) Yes (PostgreSQL) |
Required 1-1 relations are not represented in database | No | Yes | No |
@db attributes from Prisma 1 are not transferred to the Prisma schema |
No | Yes | No |
Mismatching CUID length | Yes | No | No |
Scalar lists (arrays) are maintained with extra table | Depends | No | Depends |
Note: A general drawback with the workarounds in the Prisma schema is that changes to the Prisma schema get lost after re-introspecting the database and need to be re-added manually after each introspection run.
The Prisma Upgrade CLI helps you apply the workarounds that are explained on the Schema incompatibilities page. It generates the SQL statements to fix the database schema and make it compatible with Prisma 2. Note that you are in full control over the operations that are executed against your database, the Upgrade CLI only generates and prints the statements for you. The Upgrade CLI also takes care of the workarounds in the Prisma schema.
On a high-level, the upgrade workflow using the Upgrade CLI looks as follows.
For the initial setup:
- You set up Prisma 2 by installing the Prisma 2 CLI and running
npx prisma init
. - You connect to your database and introspect it with
npx prisma introspect
.
Deprecation warning
From Prisma 3.0.0 onwards, the prisma introspect
command will be deprecated and replaced with the prisma db pull
command.
For fixing the schema incompatibilities:
- You invoke the Upgrade CLI with
npx prisma-upgrade
. - The Upgrade CLI generates SQL commands for you to run on your database.
- You run the SQL commands against your database.
- You run the
prisma introspect
command again. - You run the
npx prisma-upgrade
command again. - The Upgrade CLI adjusts the Prisma 2 schema by adding missing attributes.
Note that the Upgrade CLI is designed in a way that you can stop and re-start the process at any time. Once you ran a SQL command that was generated by the Upgrade CLI against your database, the SQL command will not show up the next time you invoke the Upgrade CLI. That way, you can gradually resolve all schema incompatibilities when it's convenient for you.
There are two main upgrade strategies:
- Upgrade all at once: Entirely remove Prisma 1 from your project and move everything over to Prisma 2 at once.
- Gradual upgrade side-by-side: Add Prisma 2 to the existing Prisma 1 project and gradually replace existing Prisma 1 features with Prisma 2 while running them side-by-side.
Note that if you are planning to run Prisma 1 and Prisma 2 side-by-side, you must not yet resolve the schema compatibilities that are breaking the Prisma 1 setup.
If your project is not yet running in production or has little traffic and user data, the all at once strategy is recommended.
In case your project already sees a lot of traffic and has a lot of user data stored in the database, you might want to consider the gradual upgrade strategy where you're running Prisma 1 and Prisma 2 side-by-side for a certain amount of time until you've replace all former Prisma 1 functionality with Prisma 2
Note that you won't be able to fix the schema incompatibilities that require are breaking Prisma 1 if you choose the gradual upgrade strategy and intend to run Prisma 1 and Prisma 2 side-by-side. That's because these data migrations are breaking the schema that Prisma 1 expects. This means that your Prisma Client API might not feel as idiomatic as it could, but you still get the full feature set of Prisma Client.
No matter which of the strategies you choose, on a high-level the envisioned upgrade path looks as follows:
- Install the new Prisma 2 CLI as a development dependency
- Create your Prisma schema and configure the database connection URL
- Use the Prisma 2 CLI to introspect your Prisma 1 database and generate your Prisma schema
- Run the Prisma Upgrade CLI to "fix" the Prisma schema
- Install and generate Prisma Client 2.0
- Adjust your application code, specifically replace the API calls from the Prisma client 1.0 with those of Prisma Client 2.0
Once you've made the decision to upgrade, continue with the Upgrading the Prisma layer guide.