# Customizing migrations (/docs/orm/v6/prisma-migrate/workflows/customizing-migrations)

Location: ORM > v6 > Prisma Migrate > Workflows > Customizing migrations

> [!WARNING]
> This guide **does not apply for MongoDB**.<br />
> Instead of `migrate dev`, [`db push`](/orm/v6/prisma-migrate/workflows/prototyping-your-schema) is used for [MongoDB](/orm/v6/overview/databases/mongodb).

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](#example-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.

How to edit a migration file [#how-to-edit-a-migration-file]

To edit a migration file before applying it, the general procedure is the following:

1. Make a schema change that requires custom SQL (for example, to preserve existing data)

2. Create a draft migration using:

   
     

#### npm

```bash
npx prisma migrate dev --create-only
```

#### pnpm

```bash
pnpm dlx prisma migrate dev --create-only
```

#### yarn

```bash
yarn dlx prisma migrate dev --create-only
```

#### bun

```bash
bunx --bun prisma migrate dev --create-only
```
   

3. Modify the generated SQL file.

4. Apply the modified SQL by running:

   
     

#### npm

```bash
npx prisma migrate dev
```

#### pnpm

```bash
pnpm dlx prisma migrate dev
```

#### yarn

```bash
yarn dlx prisma migrate dev
```

#### bun

```bash
bunx --bun prisma migrate dev
```
   

Example: Rename a field [#example-rename-a-field]

By default, renaming a field in the schema results in a migration that will:

* `CREATE` a new column (for example, `fullname`)
* `DROP` the 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.

```prisma highlight=3;normal;
model Profile {
  id       Int    @id @default(autoincrement())
  biograpy String // [!code highlight]
  userId   Int    @unique
  user     User   @relation(fields: [userId], references: [id])
}
```

To rename the `biograpy` field to `biography`:

1. Rename the field in the schema:

   ```prisma highlight=3;delete|4;add;
   model Profile {
     id        Int    @id @default(autoincrement())
     biograpy  String // [!code --]
     biography String // [!code ++]
     userId    Int    @unique
     user      User   @relation(fields: [userId], references: [id])
   }
   ```

2. Run the following command to create a **draft migration** that you can edit before applying to the database:

   
     

#### npm

```bash
npx prisma migrate dev --name rename-migration --create-only
```

#### pnpm

```bash
pnpm dlx prisma migrate dev --name rename-migration --create-only
```

#### yarn

```bash
yarn dlx prisma migrate dev --name rename-migration --create-only
```

#### bun

```bash
bunx --bun prisma migrate dev --name rename-migration --create-only
```
   

3. Edit the draft migration as shown, changing `DROP` / `DELETE` to a single `RENAME COLUMN`:

   
     

#### Before

```sql title="./prisma/migrations/20210308092620_rename_migration/migration.sql" 
ALTER TABLE "Profile" DROP COLUMN "biograpy",
ADD COLUMN  "biography" TEXT NOT NULL;
```

#### After

```sql title="./prisma/migrations/20210308092620_rename_migration/migration.sql" 
ALTER TABLE "Profile"
RENAME COLUMN "biograpy" TO "biography"
```
   

   For SQL Server, you should use the stored procedure `sp_rename` instead of `ALTER TABLE RENAME COLUMN`.

   
     

#### After

```sql title="./prisma/migrations/20210308092620_rename_migration/migration.sql" 
 EXEC sp_rename 'dbo.Profile.biograpy', 'biography', 'COLUMN';
```
   

4. Save and apply the migration:

   
     

#### npm

```bash
npx prisma migrate dev
```

#### pnpm

```bash
pnpm dlx prisma migrate dev
```

#### yarn

```bash
yarn dlx prisma migrate dev
```

#### bun

```bash
bunx --bun prisma migrate dev
```
   

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 [#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:

1. Add the new `biography` field to your Prisma schema and create a migration

   ```prisma highlight=4;add;
   model Profile {
     id        Int    @id @default(autoincrement())
     bio       String
     biography String // [!code ++]
     userId    Int    @unique
     user      User   @relation(fields: [userId], references: [id])
   }
   ```

2. *Expand*: update the application code and write to both the `bio` and `biography` fields, but continue reading from the `bio` field, and deploy the code

3. Create an empty migration and copy existing data from the `bio` to the `biography` field

   
     

#### npm

```bash
npx prisma migrate dev --name copy_biography --create-only
```

#### pnpm

```bash
pnpm dlx prisma migrate dev --name copy_biography --create-only
```

#### yarn

```bash
yarn dlx prisma migrate dev --name copy_biography --create-only
```

#### bun

```bash
bunx --bun prisma migrate dev --name copy_biography --create-only
```
   

   ```sql title="prisma/migrations/20210420000000_copy_biography/migration.sql"
   UPDATE "Profile" SET biography = bio;
   ```

4. Verify the integrity of the `biography` field in the database

5. Update application code to **read** from the new `biography` field

6. Update application code to **stop writing** to the `bio` field

7. *Contract*: remove the `bio` from the Prisma schema, and create a migration to remove the `bio` field

   ```prisma highlight=3;delete;
   model Profile {
     id        Int    @id @default(autoincrement())
     bio       String // [!code --]
     biography String
     userId    Int    @unique
     user      User   @relation(fields: [userId], references: [id])
   }
   ```

   
     

#### npm

```bash
npx prisma migrate dev --name remove_bio
```

#### pnpm

```bash
pnpm dlx prisma migrate dev --name remove_bio
```

#### yarn

```bash
yarn dlx prisma migrate dev --name remove_bio
```

#### bun

```bash
bunx --bun prisma migrate dev --name remove_bio
```
   

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](https://www.prisma.io/dataguide/types/relational/expand-and-contract-pattern)

Example: Change the direction of a 1-1 relation [#example-change-the-direction-of-a-1-1-relation]

To change the direction of a 1-1 relation:

1. Make the change in the schema:

   ```prisma
   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
   }
   ```

2. Run the following command to create a **draft migration** that you can edit before applying to the database:

   
     

#### npm

```bash
npx prisma migrate dev --name rename-migration --create-only
```

#### pnpm

```bash
pnpm dlx prisma migrate dev --name rename-migration --create-only
```

#### yarn

```bash
yarn dlx prisma migrate dev --name rename-migration --create-only
```

#### bun

```bash
bunx --bun prisma migrate dev --name rename-migration --create-only
```
   

   ```text no-copy
   ⚠️  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.
   ```

3. Edit the draft migration as shown:

   
     

#### Before

```sql

-- 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;
```

#### After

```sql

-- 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;
```
   

4. Save and apply the migration:

   
     

#### npm

```bash
npx prisma migrate dev
```

#### pnpm

```bash
pnpm dlx prisma migrate dev
```

#### yarn

```bash
yarn dlx prisma migrate dev
```

#### bun

```bash
bunx --bun prisma migrate dev
```

## Related pages

- [`Baselining a database`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/baselining): How to initialize a migration history for an existing database that contains important data.
- [`Data migrations`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/data-migration): How to migrate data using Prisma ORM with the expand and contract pattern.
- [`Development and production`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/development-and-production): Development and production
- [`Generating down migrations`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/generating-down-migrations): How to generate down migrations
- [`Native database functions`](https://www.prisma.io/docs/orm/v6/prisma-migrate/workflows/native-database-functions): How to enable PostgreSQL native database functions for projects that use Prisma Migrate.