How to squash migrations in Prisma
October 21, 2023
Squashing migrations in Prisma helps to optimize your migration history by combining multiple migration files into one. This is particularly useful for projects with a large number of migrations, making the history cleaner and easier to manage.
- Prisma CLI installed
- A Prisma project with existing migrations
Steps to Squash Migrations
First, backup your database. This step is critical. Don't skip it.
Delete Migrations Folder
migrations folder in your
Generate Squashed Migration
prisma migrate dev to create a new migration that represents the squashed schema.
This will create a new
migrations folder containing a single migration that represents your current schema.
Test The New Migration
Apply the new migration to a fresh database to ensure it works as expected. Make sure to only run this in development since it will wipe your database.
Commit the new
migrations folder and the updated
schema.prisma to your version control system.
Caveats and Warnings
- History Mismatch: Squashing migrations will cause a history mismatch in environments where the migrations have been applied. Coordinate with your team to apply the squashed migration in all environments. This can be a problem if your migrations are used in systems that you don’t control. This is something that we have to consider at Basedash since we offer self-hosting.
- Data Loss: The
prisma migrate resetcommand used for testing will wipe your database. Make sure you have a backup.
- Backup your database.
- Delete the old
- Generate a new, squashed migration.
- Test the new migration.
- Commit your changes.
And there you have it: your migrations are now squashed and your project is cleaner for it.
How to automate Prisma migrations in a CI/CD pipeline
How to implement soft deletes in Prisma
How to use the shadow database in Prisma
How to reset and seed a Prisma database
UUID vs GUID vs CUID vs NanoID: A guide to database primary keys
How to generate UUIDs in Prisma