Mental model
This guide provides a conceptual overview of database migrations using Prisma Migrate when working with relational databases. It covers: what database migrations are, their value, and what Prisma Migrate is and how you can evolve your database schema with Prisma Migrate in different environments.
If you are working with MongoDB, use prisma db push
to evolve your schema.
What are database migrations?
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.
Patterns for evolving database schemas
This section describes general schema migration patterns for evolving database schemas.
The two main schema migration patterns are:
- Model/Entity-first migration: with this pattern, you define the structure of the database schema with code and then use a migration tool to generate the SQL, for example, for syncing your application and database schema.
- Database-first migration: with this pattern, you define the structure of your database and apply it to your database using SQL. You then introspect the database to generate the code that describes the structure of your database to sync your application and database schema.
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.
What is Prisma Migrate?
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:
Local development environment (Feature branch)
- Evolve your Prisma schema
- Use either
prisma migrate dev
orprisma db push
to sync your Prisma schema with the database schema of your local development database
Preview/ staging environment(Feature pull request)
- Push your changes to the feature pull request
- Use a CI system (e.g. GitHub Actions) to sync your Prisma schema and migration history with your preview database using
prisma migrate deploy
Production (main branch)
- Merge your application code from the feature branch to your main branch
- Use a CI system (e.g. GitHub Actions) to sync your Prisma schema and migration history with your production database using
prisma migrate deploy
How Prisma Migrate tracks the migration state
Prisma Migrate uses the following pieces of state to track the state of your database schema:
- Prisma schema: your source of truth that defines the structure of the database schema.
- Migrations history: SQL files in your
prisma/migrations
folder representing the history of changes made to your database schema. - Migrations table:
prisma_migrations
table in the database that stores metadata for migrations that have been applied to the database. - Database schema: the state of the database.
Requirements when working with Prisma Migrate
- Ideally, you should use one database per environment. For example, you might have a separate database for development, preview, and production environments.
- The databases you use in development environments are disposable — you can easily create, use, and delete databases on demand.
- The database configuration used in each environments should be consistent. This is important to ensure a certain migration that moves across the workflow yields the same changes to the database.
- The Prisma schema serves as the source of truth — describing the shape of your database schema.
Evolve your database schema with Prisma Migrate
This section describes how you can evolve your database schema in different environments: development, staging, and production, using Prisma Migrate.