Database Schema Migrations
Manage changes to your database schema using Prisma Migrate for smooth development and deployment.
Database Schema Migrations is a free Next.js 15 Fullstack (App Router + Server Actions) lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Next.js 15 Fullstack (App Router + Server Actions) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Database Migrations?
As your application evolves, so does its database structure, also known as its schema.
Database migrations are like version control for your database schema. They are a way to manage changes to your database structure in a controlled and trackable manner.
- They ensure your database matches your application's needs.
- They prevent data loss during schema changes.
- They provide a clear history of all schema modifications.
Why Use Prisma Migrate?
Managing database schema changes manually can be complex and error-prone. Prisma Migrate simplifies this process significantly.
- Automatic Generation: It generates SQL migration files based on changes in your Prisma schema.
- History Tracking: It keeps a history of applied migrations, making rollbacks and team collaboration easier.
- Environment Consistency: Ensures your development, staging, and production databases all have the correct schema.
Your Prisma Schema File
Prisma Migrate uses your schema.prisma file as the source of truth for your database schema. Any changes you want to make to your database structure start here.
Let's recall a basic schema.prisma setup:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}Initializing Your First Migration
When you first set up your Prisma schema, you'll want to create an initial migration to apply this schema to your database. You use the prisma migrate dev command for this.
This command does two main things:
- Generates a new migration file (SQL) in the
prisma/migrationsfolder. - Applies the migration to your database.
npx prisma migrate dev --name initial_setupUnderstanding Migration Files
After running npx prisma migrate dev, Prisma creates a new folder inside prisma/migrations with a timestamp and the name you provided (e.g., 20231026123456_initial_setup).
Inside this folder, you'll find:
migration.sql: This file contains the raw SQL commands generated by Prisma to create or alter your database tables.migration.jsormigration.ts: (If using TypeScript/JavaScript) Metadata about the migration.
It's important to commit these files to your version control system!
Making Schema Changes
Now, let's say you want to add an email field to your User model. You simply update your schema.prisma file.
Prisma will detect this change and help you generate a new migration.
model User {
id Int @id @default(autoincrement())
name String
email String @unique // Added new field
posts Post[]
}Generating a New Migration
After modifying your schema.prisma, run npx prisma migrate dev again. Prisma will compare your updated schema with the current database state and generate a new migration file.
It will then apply this new migration, adding the email column to your User table.
npx prisma migrate dev --name add_user_emailApplying Migrations in Production
For production environments, you typically use npx prisma migrate deploy. This command applies all pending migrations that have not yet been run on the database.
Unlike migrate dev, deploy does not generate new migration files; it only applies existing ones. This makes it safe and predictable for production deployments.
npx prisma migrate deployChecking Migration Status
To see which migrations have been applied to your database and which are pending, you can use the npx prisma migrate status command.
This is useful for debugging or ensuring your database is up-to-date with your application's expected schema.
npx prisma migrate statusPrisma Migrate Commands Quiz
Which of the following statements correctly describe the purpose of Prisma Migrate commands?
Recap: Schema Migrations
Great job! You've learned how to manage your database schema effectively using Prisma Migrate.
- Prisma Migrate helps version control your database schema.
- You define schema changes in
schema.prisma. npx prisma migrate devcreates and applies migrations in development.npx prisma migrate deployapplies existing migrations in production.npx prisma migrate statuschecks the current migration state.
This powerful tool ensures your database structure stays consistent and aligned with your application's needs as it grows.
Frequently asked questions
Is the “Database Schema Migrations” lesson free?
Yes — the full text of “Database Schema Migrations” is free to read here on the web, and the Next.js 15 Fullstack (App Router + Server Actions) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Next.js 15 Fullstack (App Router + Server Actions) course, upgrade to CoddyKit PRO.
What will I learn in “Database Schema Migrations”?
Manage changes to your database schema using Prisma Migrate for smooth development and deployment. You practise Next.js 15 Fullstack (App Router + Server Actions) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Next.js 15 Fullstack (App Router + Server Actions)?
No prior experience is required. Next.js 15 Fullstack (App Router + Server Actions) on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Database Schema Migrations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Next.js 15 Fullstack (App Router + Server Actions) lesson?
Yes. Every Next.js 15 Fullstack (App Router + Server Actions) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Setting Up Prisma ORM
- CRUD with Server Actions
- Database Schema Migrations
- Database Seeding & Connection Pooling