# Expand-and-contract migrations (/docs/guides/database/data-migration)

Location: Guides > Database > Expand-and-contract migrations

Introduction [#introduction]

When making changes to your database schema in production, it's crucial to ensure data consistency and avoid downtime. This guide shows you how to use the expand and contract pattern to safely migrate data between columns. We'll walk through a practical example of replacing a boolean field with an enum field while preserving existing data.

Prerequisites [#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 [#1-set-up-your-environment]

1.1. Review initial schema [#11-review-initial-schema]

Start with a basic schema containing a Post model:

```prisma
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 [#12-configure-prisma]

Create a `prisma.config.ts` file in the root of your project with the following content:

```typescript title="prisma.config.ts"
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"),
  },
});
```

> [!NOTE]
> You'll need to install the required packages. If you haven't already, install them using your package manager:
> 
> 
>   
> 
>   #### npm

>     ```bash
>     npm install prisma @types/pg --save-dev
>     ```
>
> 
>   #### pnpm

>     ```bash
>     pnpm add prisma @types/pg --save-dev
>     ```
>
> 
>   #### yarn

>     ```bash
>     yarn add prisma @types/pg --dev
>     ```
>
> 
>   #### bun

>     ```bash
>     bun add prisma @types/pg --dev
>     ```
>
> 
> 
> 
>   
> 
>   #### npm

>     ```bash
>     npm install @prisma/client @prisma/adapter-pg pg dotenv
>     ```
>
> 
>   #### pnpm

>     ```bash
>     pnpm add @prisma/client @prisma/adapter-pg pg dotenv
>     ```
>
> 
>   #### yarn

>     ```bash
>     yarn add @prisma/client @prisma/adapter-pg pg dotenv
>     ```
>
> 
>   #### bun

>     ```bash
>     bun add @prisma/client @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](/orm/core-concepts/supported-databases/database-drivers).

1.3. Create a development branch [#13-create-a-development-branch]

Create a new branch for your changes:

```bash
git checkout -b create-status-field
```

2. Expand the schema [#2-expand-the-schema]

2.1. Add new column [#21-add-new-column]

Update your schema to add the new Status enum and field:

```prisma
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 [#22-create-migration]

Generate the migration:

  

#### npm

```bash
npx prisma migrate dev --name add-status-column
```

#### pnpm

```bash
pnpm dlx prisma migrate dev --name add-status-column
```

#### yarn

```bash
yarn dlx prisma migrate dev --name add-status-column
```

#### bun

```bash
bunx --bun prisma migrate dev --name add-status-column
```

Then generate Prisma Client:

  

#### npm

```bash
npx prisma generate
```

#### pnpm

```bash
pnpm dlx prisma generate
```

#### yarn

```bash
yarn dlx prisma generate
```

#### bun

```bash
bunx --bun prisma generate
```

3. Migrate the data [#3-migrate-the-data]

3.1. Create migration script [#31-create-migration-script]

Create a new TypeScript file for the data migration:

```typescript
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 [#32-set-up-migration-script]

Add the migration script to your package.json:

```json
{
  "scripts": {
    "data-migration:add-status-column": "tsx ./prisma/migrations/<migration-timestamp>/data-migration.ts"
  }
}
```

3.3. Execute migration [#33-execute-migration]

1. Update your DATABASE\_URL to point to the production database
2. Run the migration script:

  

#### npm

```bash
npm run data-migration:add-status-column
```

#### pnpm

```bash
pnpm run data-migration:add-status-column
```

#### yarn

```bash
yarn data-migration:add-status-column
```

#### bun

```bash
bun run data-migration:add-status-column
```

4. Contract the schema [#4-contract-the-schema]

4.1. Create cleanup branch [#41-create-cleanup-branch]

Create a new branch for removing the old column:

```bash
git checkout -b drop-published-column
```

4.2. Remove old column [#42-remove-old-column]

Update your schema to remove the published field:

```prisma
model Post {
  id      Int      @id @default(autoincrement())
  title   String
  content String?
  status  Status   @default(Unknown)
}

enum Status {
  Draft
  InProgress
  InReview
  Published
}
```

4.3. Generate cleanup migration [#43-generate-cleanup-migration]

Create and run the final migration:

  

#### npm

```bash
npx prisma migrate dev --name drop-published-column
```

#### pnpm

```bash
pnpm dlx prisma migrate dev --name drop-published-column
```

#### yarn

```bash
yarn dlx prisma migrate dev --name drop-published-column
```

#### bun

```bash
bunx --bun prisma migrate dev --name drop-published-column
```

Then generate Prisma Client:

  

#### npm

```bash
npx prisma generate
```

#### pnpm

```bash
pnpm dlx prisma generate
```

#### yarn

```bash
yarn dlx prisma generate
```

#### bun

```bash
bunx --bun prisma generate
```

5. Deploy to production [#5-deploy-to-production]

5.1. Set up deployment [#51-set-up-deployment]

Add the following command to your CI/CD pipeline:

  

#### npm

```bash
npx prisma migrate deploy
```

#### pnpm

```bash
pnpm dlx prisma migrate deploy
```

#### yarn

```bash
yarn dlx prisma migrate deploy
```

#### bun

```bash
bunx --bun prisma migrate deploy
```

5.2. Monitor deployment [#52-monitor-deployment]

Watch for any errors in your logs and monitor your application's behavior after deployment.

Troubleshooting [#troubleshooting]

Common issues and solutions [#common-issues-and-solutions]

1. **Migration fails due to missing default**
   * Ensure you've added a proper default value
   * Check that all existing records can be migrated

2. **Data loss prevention**
   * Always backup your database before running migrations
   * Test migrations on a copy of production data first

3. **Transaction rollback**
   * If the data migration fails, the transaction will automatically rollback
   * Fix any errors and retry the migration

Next steps [#next-steps]

Now that you've completed your first expand and contract migration, you can:

* Learn more about [Prisma Migrate](/orm/prisma-migrate)
* Explore [schema prototyping](/orm/prisma-migrate/workflows/prototyping-your-schema)
* Understand [customizing migrations](/orm/prisma-migrate/workflows/customizing-migrations)

For more information:

* [Expand and Contract Pattern Documentation](https://www.prisma.io/dataguide/types/relational/expand-and-contract-pattern)
* [Prisma Migrate Workflows](/orm/prisma-migrate/workflows/development-and-production)

## Related pages

- [`Multiple databases`](https://www.prisma.io/docs/guides/database/multiple-databases): Learn how to use multiple Prisma Clients in a single app to connect to multiple databases, handle migrations, and deploy your application to Vercel
- [`Schema management in teams`](https://www.prisma.io/docs/guides/database/schema-changes): Learn how to use Prisma Migrate effectively when collaborating on a project as a team