apps/docs/content/docs/orm/prisma-migrate/understanding-prisma-migrate/mental-model.mdx
Database migrations are a controlled set of changes that modify and evolve the structure of your database schema. Migrations help you transition your database schema from one state to another. For example, within a migration you can create or remove tables and columns, split fields in a table, or add types and constraints to your database.
For migrations, there are two main types of migrations that can be made:
:::info[Note] For simplicity, we chose the terminology above to describe the different patterns for evolving database schemas. Other tools and libraries may use different terminology to describe the different patterns. :::
The migration files (SQL) should ideally be stored together with your application code. They should also be tracked in version control and shared with the rest of the team working on the application. Migrations provide state management which helps you to track the state of the database.
Migrations also allow you to replicate the state of a database at a specific point in time which is useful when collaborating with other members of the team, e.g. switching between different branches. For further information on database migrations, see the Prisma Data Guide.
Prisma Migrate is a database migration tool that supports the model/ entity-first migration pattern to manage database schemas in your local environment and in production.
The workflow when using Prisma Migrate in your project would be iterative and look like this:
prisma migrate dev or prisma db push to sync your Prisma schema with the database schema of your local development databaseprisma migrate deployprisma migrate deployPrisma Migrate uses the following pieces of state to track the state of your database schema:
prisma/migrations folder representing the history of changes made to your database schema.prisma_migrations table in the database that stores metadata for migrations that have been applied to the database.This section describes how you can evolve your database schema in different environments: development, staging, and production, using Prisma Migrate.
prisma migrate devThe prisma migrate dev command allows you to track the changes you make to your database. The prisma migrate dev command automatically generates SQL migration files (saved in /prisma/migrations) and applies them to the database. When a migration is applied to the database, the migrations table (_prisma_migrations) in your database is also updated.
The prisma migrate dev command tracks the state of the database using the following pieces of state:
:::info[Note] The pieces of state used to track the state of a migration are the same as the ones described in how Prisma Migrate tracks the migration state section. :::
You can customize migrations before you apply them to the database using the --create-only flag. For example, you might want to edit a migration if you want to rename columns without incurring any data loss or load database extensions (in PostgreSQL) and database views (currently not supported).
Under the hood, Prisma Migrate uses a shadow database to detect a schema drift and generate new migrations.
:::info[Note]
prisma migrate dev is intended to be used only in development with a disposable database.
:::
If prisma migrate dev detects a schema drift or a migration history conflict, you will be prompted to reset (drop and recreate your database) your database to sync the migration history and the database schema.
A schema drift occurs when the expected database schema is different from what is in the migration history. For example, this can occur when you manually update the database schema without also updating the Prisma schema and prisma/migrations accordingly.
For such instances, you can use the prisma migrate diff command to compare your migration history and revert changes made to your database schema.
You can use migrate diff to generate the SQL that either:
/migrationsYou can then apply the changes to your database using prisma db execute command.
The prisma db push command allows you to sync your Prisma schema and database schema without persisting a migration (/prisma/migrations). The prisma db push command tracks the state of the database using the following pieces of state:
The prisma db push command is useful when:
prisma db push)prisma db push anticipates that changes will result in data loss, you can either accept data loss with the --accept-data-loss option or stop the process - there is no way to customize the changes.If the prisma db push command detects destructive change to your database schema, it will prompt you to reset your database. For example, this will happen when you add a required field to a table with existing content without providing a default value.
:::info[Schema Drift] A schema drift occurs when your database schema is out of sync with your migrations history and migrations table. :::
The prisma migrate deploy command allows you to sync your migration history from your development environment with your database in your staging or production environment.
Under the hood, the migrate deploy command:
_prisma_migrations) and the migration history (/prisma/migrations)_prisma_migrations table with the new migrationsThe command should be run in an automated CI/ CD environment, for example GitHub Actions.
If you don't have a migration history (/migrations), i.e using prisma db push, you will have to continue using prisma db push in your staging and production environments. Beware of the changes being applied to the database schema as some of them might be destructive. For example, prisma db push can't tell when you're performing a column rename. It will prompt a database reset (drop and re-creation).