rembrembdocs

Learn how to migrate your schema in a development environment using Prisma Migrate

To get started with Prisma Migrate, start by adding some models to your schema.prisma

schema.prisma

datasource db {
  provider = "postgresql"
}

model User { 
  id    Int    @id @default(autoincrement()) 
  name  String
  posts Post[]
}

model Post { 
  id        Int     @id @default(autoincrement()) 
  title     String
  published Boolean @default(true) 
  authorId  Int
  author    User    @relation(fields: [authorId], references: [id]) 
} 

Create an initial migration

Create an initial migration using the prisma migrate command:

This will generate a migration with the appropriate commands for your database.

migration.sql

CREATE TABLE "User" (
  "id" SERIAL,
  "name" TEXT NOT NULL,
  PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Post" (
  "id" SERIAL,
  "title" TEXT NOT NULL,
  "published" BOOLEAN NOT NULL DEFAULT true,
  "authorId" INTEGER NOT NULL,
  PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE
  "Post"
ADD
  FOREIGN KEY("authorId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

Your Prisma schema is now in sync with your database schema and you have initialized a migration history:

migrations/
  └─ 20210313140442_init/
    └─ migration.sql

Note: The folder name will be different for you. Folder naming is in the format of YYYYMMDDHHMMSS_your_text_from_name_flag.

Additional migrations

Now say you add additional fields to your model

schema.prisma

model User {
  id       Int    @id @default(autoincrement())
  jobTitle String
  name     String
  posts    Post[]
}

You can run prisma migrate again to update your migrations

migration.sql

  -- AlterTable
ALTER TABLE
  "User"
ADD
  COLUMN "jobTitle" TEXT NOT NULL;

Your Prisma schema is once again in sync with your database schema, and your migration history contains two migrations:

migrations/
  └─ 20210313140442_init/
    └─ migration.sql
  └─ 20210313140442_added_job_title/
    └─ migration.sql

Committing to versions control

Your migration history can be committed to version control and use to deploy changes to test environments and production.

It's possible to integrate Prisma migrations to an existing project.

Introspect to create or update your Prisma schema

Make sure your Prisma schema is in sync with your database schema. This should already be true if you are using a previous version of Prisma Migrate.

Create a baseline migration

Create a baseline migration that creates an initial history of the database before using Prisma migrate. This migrations contains the data that must be maintained, which means the database cannot be reset. This tells Prisma migrate to assume that one or more migrations have already been applied. This prevents generated migrations from failing when they try to create tables and fields that already exist.

To create a baseline migration:

Work around features not supported by Prisma Schema Language

To include unsupported database features that already exist in the database, you must replace or modify the initial migration SQL:

migration.sql

/* Generated migration SQL */

CREATE OR REPLACE FUNCTION notify_on_insert() 
RETURNS TRIGGER AS $$ 
BEGIN
  PERFORM pg_notify('new_record', NEW.id::text); 
  RETURN NEW; 
END; 
$$ LANGUAGE plpgsql; 

Apply the initial migrations

To apply your initial migration(s):

The new migration history and the database schema should now be in sync with your Prisma schema.

Commit the migration history and Prisma schema

Commit the following to source control: