Customizing migrations
In some scenarios, you need to edit a migration file before you apply it. For example, to change the direction of a 1-1 relation (moving the foreign key from one side to another) without data loss, you need to move data as part of the migration - this SQL is not part of the default migration, and must be written by hand.
This guide explains how to edit migration files and gives some examples of use cases where you may want to do this.
To edit a migration file before applying it, the general procedure is the following:
- Make a schema change that requires custom SQL (for example, to preserve existing data)
- Create a draft migration using:
bunx prisma migrate dev --create-onlypnpm prisma migrate dev --create-onlyyarn prisma migrate dev --create-onlynpx prisma migrate dev --create-only- Modify the generated SQL file.
- Apply the modified SQL by running:
bunx prisma migrate devpnpm prisma migrate devyarn prisma migrate devnpx prisma migrate devBy default, renaming a field in the schema results in a migration that will:
CREATEa new column (for example,fullname)DROPthe existing column (for example,name) and the data in that column
To actually rename a field and avoid data loss when you run the migration in production, you need to modify the generated migration SQL before applying it to the database. Consider the following schema fragment - the biograpy field is spelled wrong.
model Profile {
id Int @id @default(autoincrement())
biograpy String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}To rename the biograpy field to biography:
Rename the field in the schema:
model Profile {
id Int @id @default(autoincrement())
biograpy String
biography String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}- Run the following command to create a draft migration that you can edit before applying to the database:
bunx prisma migrate dev --name rename-migration --create-onlypnpm prisma migrate dev --name rename-migration --create-onlyyarn prisma migrate dev --name rename-migration --create-onlynpx prisma migrate dev --name rename-migration --create-only- Edit the draft migration as shown, changing
DROP/DELETEto a singleRENAME COLUMN:
ALTER TABLE "Profile" DROP COLUMN "biograpy",
ADD COLUMN "biography" TEXT NOT NULL;ALTER TABLE "Profile"
RENAME COLUMN "biograpy" TO "biography"- Save and apply the migration:
bunx prisma migrate devpnpm prisma migrate devyarn prisma migrate devnpx prisma migrate devYou can use the same technique to rename a model - edit the generated SQL to rename the table rather than drop and re-create it.
You can use the same technique to rename a model - edit the generated SQL to rename the table rather than drop and re-create it.
Example: Use the expand and contract pattern to evolve the schema without downtime
Section titled “Example: Use the expand and contract pattern to evolve the schema without downtime”Making schema changes to existing fields, e.g., renaming a field can lead to downtime. It happens in the time frame between applying a migration that modifies an existing field, and deploying a new version of the application code which uses the modified field.
You can prevent downtime by breaking down the steps required to alter a field into a series of discrete steps designed to introduce the change gradually. This pattern is known as the expand and contract pattern.
The pattern involves two components: your application code accessing the database and the database schema you intend to alter.
With the expand and contract pattern, renaming the field bio to biography would look as follows with Prisma:
- Add the new
biographyfield to your Prisma schema and create a migration
model Profile {
id Int @id @default(autoincrement())
bio String
biography String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}- Expand: update the application code and write to both the
bioandbiographyfields, but continue reading from thebiofield, and deploy the code - Create an empty migration and copy existing data from the
bioto thebiographyfield
bunx prisma migrate dev --name copy_biography --create-onlypnpm prisma migrate dev --name copy_biography --create-onlyyarn prisma migrate dev --name copy_biography --create-onlynpx prisma migrate dev --name copy_biography --create-onlyUPDATE "Profile" SET biography = bio;- Verify the integrity of the
biographyfield in the database - Update application code to read from the new
biographyfield - Update application code to stop writing to the
biofield - Contract: remove the
biofrom the Prisma schema, and create a migration to remove thebiofield
model Profile { id Int @id @default(autoincrement()) bio String biography String userId Int @unique user User @relation(fields: [userId], references: [id])}UPDATE "Profile" SET biography = bio;- Verify the integrity of the
biographyfield in the database - Update application code to read from the new
biographyfield - Update application code to stop writing to the
biofield - Contract: remove the
biofrom the Prisma schema, and create a migration to remove thebiofield
model Profile {
id Int @id @default(autoincrement())
bio String
biography String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}bunx prisma migrate dev --name remove_biopnpm prisma migrate dev --name remove_bioyarn prisma migrate dev --name remove_bionpx prisma migrate dev --name remove_bioBy using this approach, you avoid potential downtime that altering existing fields that are used in the application code are prone to, and reduce the amount of coordination required between applying the migration and deploying the updated application code.
Note that this pattern is applicable in any situation involving a change to a column that has data and is in use by the application code. Examples include combining two fields into one, or transforming a 1:n relation to a m:n relation.
To learn more, check out the Data Guide article on the expand and contract pattern
By using this approach, you avoid potential downtime that altering existing fields that are used in the application code are prone to, and reduce the amount of coordination required between applying the migration and deploying the updated application code.
Note that this pattern is applicable in any situation involving a change to a column that has data and is in use by the application code. Examples include combining two fields into one, or transforming a 1:n relation to a m:n relation.
To learn more, check out the Data Guide article on the expand and contract pattern
Example: Change the direction of a 1-1 relation
Section titled “Example: Change the direction of a 1-1 relation”To change the direction of a 1-1 relation:
- Make the change in the schema:
model User {
id Int @id @default(autoincrement())
name String
posts Post[]
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int @unique
}
model Profile {
id Int @id @default(autoincrement())
biography String
user User
}- Run the following command to create a draft migration that you can edit before applying to the database:
bunx prisma migrate dev --name rename-migration --create-onlypnpm prisma migrate dev --name rename-migration --create-onlyyarn prisma migrate dev --name rename-migration --create-onlynpx prisma migrate dev --name rename-migration --create-only⚠️ There will be data loss when applying the migration:
• The migration will add a unique constraint covering the columns `[profileId]` on the table `User`. If there are existing duplicate values, the migration will fail.- Edit the draft migration as shown:
⚠️ There will be data loss when applying the migration:
• The migration will add a unique constraint covering the columns `[profileId]` on the table `User`. If there are existing duplicate values, the migration will fail.- Edit the draft migration as shown:
-- DropForeignKey
ALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey";
-- DropIndex
DROP INDEX "Profile_userId_unique";
-- AlterTable
ALTER TABLE "Profile" DROP COLUMN "userId";
-- AlterTable
ALTER TABLE "User" ADD COLUMN "profileId" INTEGER NOT NULL;
-- CreateIndex
CREATE UNIQUE INDEX "User_profileId_unique" ON "User"("profileId");
-- AddForeignKey
ALTER TABLE "User" ADD FOREIGN KEY ("profileId") REFERENCES "Profile"("id") ON DELETE CASCADE ON UPDATE CASCADE;-- DropForeignKey
ALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey";
-- DropIndex
DROP INDEX "Profile_userId_unique";
-- AlterTable
ALTER TABLE "User" ADD COLUMN "profileId" INTEGER;
UPDATE "User"
SET "profileId" = "Profile".id
FROM "Profile"
WHERE "User".id = "Profile"."userId";
ALTER TABLE "User" ALTER COLUMN "profileId" SET NOT NULL;
-- AlterTable
ALTER TABLE "Profile" DROP COLUMN "userId";
-- CreateIndex
CREATE UNIQUE INDEX "User_profileId_unique" ON "User"("profileId");
-- AddForeignKey
ALTER TABLE "User" ADD FOREIGN KEY ("profileId") REFERENCES "Profile"("id") ON DELETE CASCADE ON UPDATE CASCADE;- Save and apply the migration:
bunx prisma migrate devpnpm prisma migrate devyarn prisma migrate devnpx prisma migrate dev