Expand-and-contract migrations (Prisma ORM v7)
Learn how to perform data migrations using the expand and contract pattern with Prisma ORM
Introduction
Section titled “Introduction”When you change your database schema in production, you need to keep the data consistent and avoid downtime. This guide shows you how to use the expand and contract pattern to safely migrate data between columns. The example replaces a boolean field with an enum field while preserving existing data.
Prerequisites
Section titled “Prerequisites”Before starting this guide, make sure you have:
- Node.js installed (version 20 or higher)
- A Prisma ORM project with an existing schema
- A supported database (PostgreSQL, MySQL, SQLite, SQL Server, etc.)
- Access to both development and production databases
- Basic understanding of Git branching
- Basic familiarity with TypeScript
1. Set up your environment
Section titled “1. Set up your environment”1.1. Review initial schema
Section titled “1.1. Review initial schema”Start with a basic schema containing a Post model:
generator client {
provider = "prisma-client"
output = "./generated/prisma"
}
datasource db {
provider = "postgresql"
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
}1.2. Configure Prisma
Section titled “1.2. Configure Prisma”Create a prisma.config.ts file in the root of your project with the following content:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});```bash bun add prisma@prev tsx @types/pg --dev ```
```bash pnpm add prisma@prev tsx @types/pg --save-dev ```
```bash yarn add prisma@prev tsx @types/pg --dev ```
```bash npm install prisma@prev tsx @types/pg --save-dev ```
```bash bun add @prisma/client@7 @prisma/adapter-pg pg dotenv ```
```bash pnpm add @prisma/client@7 @prisma/adapter-pg pg dotenv ```
```bash yarn add @prisma/client@7 @prisma/adapter-pg pg dotenv ```
```bash npm install @prisma/client@7 @prisma/adapter-pg pg dotenv ```If you are using a different database provider (MySQL, SQL Server, SQLite), install the corresponding driver adapter package instead of
@prisma/adapter-pg. For more information, see Database drivers.
1.3. Create a development branch
Section titled “1.3. Create a development branch”Create a new branch for your changes:
git checkout -b create-status-field2. Expand the schema
Section titled “2. Expand the schema”2.1. Add new column
Section titled “2.1. Add new column”Update your schema to add the new Status enum and field:
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean? @default(false)
status Status @default(Unknown)
}
enum Status {
Unknown
Draft
InProgress
InReview
Published
}2.2. Create migration
Section titled “2.2. Create migration”Generate the migration:
bunx prisma migrate dev --name add-status-columnpnpm prisma migrate dev --name add-status-columnyarn prisma migrate dev --name add-status-columnnpx prisma migrate dev --name add-status-columnThen generate Prisma Client:
bunx prisma generatepnpm prisma generateyarn prisma generatenpx prisma generate3. Migrate the data
Section titled “3. Migrate the data”3.1. Create migration script
Section titled “3.1. Create migration script”Create a new TypeScript file for the data migration:
import { PrismaClient } from "../generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import "dotenv/config";
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL,
});
const prisma = new PrismaClient({
adapter,
});
async function main() {
await prisma.$transaction(async (tx) => {
const posts = await tx.post.findMany();
for (const post of posts) {
await tx.post.update({
where: { id: post.id },
data: {
status: post.published ? "Published" : "Unknown",
},
});
}
});
}
main()
.catch(async (e) => {
console.error(e);
process.exit(1);
})
.finally(async () => await prisma.$disconnect());3.2. Set up migration script
Section titled “3.2. Set up migration script”Add the migration script to your package.json:
{
"scripts": {
"data-migration:add-status-column": "tsx ./prisma/migrations/<migration-timestamp>/data-migration.ts"
}
}3.3. Execute migration
Section titled “3.3. Execute migration”- Update your DATABASE_URL to point to the production database
- Run the migration script:
bun run data-migration:add-status-columnpnpm run data-migration:add-status-columnyarn data-migration:add-status-columnnpm run data-migration:add-status-column4. Contract the schema
Section titled “4. Contract the schema”4.1. Create cleanup branch
Section titled “4.1. Create cleanup branch”Create a new branch for removing the old column:
git checkout -b drop-published-column4.2. Remove old column
Section titled “4.2. Remove old column”Update your schema to remove the published field:
model Post {
id Int @id @default(autoincrement())
title String
content String?
status Status @default(Draft)
}
enum Status {
Draft
InProgress
InReview
Published
}4.3. Generate cleanup migration
Section titled “4.3. Generate cleanup migration”Create and run the final migration:
bunx prisma migrate dev --name drop-published-columnpnpm prisma migrate dev --name drop-published-columnyarn prisma migrate dev --name drop-published-columnnpx prisma migrate dev --name drop-published-columnThen generate Prisma Client:
bunx prisma generatepnpm prisma generateyarn prisma generatenpx prisma generate5. Deploy to production
Section titled “5. Deploy to production”5.1. Set up deployment
Section titled “5.1. Set up deployment”Add the following command to your CI/CD pipeline:
bunx prisma migrate deploypnpm prisma migrate deployyarn prisma migrate deploynpx prisma migrate deploy5.2. Monitor deployment
Section titled “5.2. Monitor deployment”Watch for any errors in your logs and monitor your application's behavior after deployment.
Troubleshooting
Section titled “Troubleshooting”Common issues and solutions
Section titled “Common issues and solutions”-
Migration fails due to missing default
- Ensure you've added a proper default value
- Check that all existing records can be migrated
-
Data loss prevention
- Always backup your database before running migrations
- Test migrations on a copy of production data first
-
Transaction rollback
- If the data migration fails, the transaction will automatically rollback
- Fix any errors and retry the migration
Next steps
Section titled “Next steps”Now that you've completed your first expand and contract migration, you can:
- Learn more about Prisma Migrate
- Explore schema prototyping
- Understand customizing migrations
For more information: