rembrembdocs

Workflows

How to squash multiple migration files into a single migration

It is sometimes useful to squash either some or all migration files into a single migration. This guide will describe two scenarios where you may want to do this:

In both cases, Prisma Migrate provides the tools for doing this, by using the migrate diff command to compare two database schemas and output a single SQL file that takes you from one to the other. The rest of this guide gives detailed instructions on how to carry this out in these two scenarios.

Migrating cleanly from a development environment

Squashing migrations can be useful when developing with a branch-based workflow. During a large local development effort on a feature branch you might generate multiple migrations using migrate dev. After the feature is finished, the migration history might contain unnecessary intermediate steps that are unwanted in the final migration history that will be pushed to the main branch.

There could be important reasons to avoid applying the intermediate steps in production — they might lose data or be extremely slow / disruptive). Even when this is not the case, you may want to avoid clutter in your production environment's migrations history.

For detailed steps on how to achieve this using migrate dev, see the section on how to migrate cleanly from a development environment.

Creating a clean history in a production environment

Squashing migrations can also be used in a production environment to squash all migration files into one. This can be useful when the production environment has accumulated a longer migration history, and replaying it in new environments has become a burden due to intermediate steps requiring extra time. Since the team is not deriving value from the migration steps (and could get them back from version control history in a pinch) the decision is made to squash the whole history into a single migration.

For detailed steps on how to achieve this using migrate diff and migrate resolve see the section on how to create a clean history in a production environment.

This section provides step-by-step instructions on how to squash migrations in the two scenarios discussed above:

How to migrate cleanly from a development environment

Before squashing your migrations, make sure you have the following starting conditions:

Then follow these steps:

This creates a single migration that takes you:

This single migration file can now be applied to production using migrate deploy.

How to create a clean history in a production environment

Before squashing your migrations, make sure you have the following starting conditions:

Then follow these steps, either on your main branch or on a newly checked out branch that gets merged back to main before anything else changes there:

You should now have a single migration file that is marked as having been applied on production. New checkouts only get one single migration taking them to the state of the production database schema.

The production database still contains the history of applied migrations in the migrations table. The history of the migrations folder and data models is also still available in source control.