What if one small mistake in your database update could break your whole app?
Why Database migrations in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a website with a database, and you need to change the structure, like adding a new column or changing a table. You try to do it by hand, writing SQL commands and running them on each server.
Doing this manually is risky and slow. You might forget a step, cause errors, or make the database inconsistent across different environments. It's hard to track what changes were made and when.
Database migrations let you write changes as code that runs automatically and safely. They keep track of what's been done, so you can update your database structure step-by-step without mistakes.
ALTER TABLE users ADD COLUMN age INT; -- Run this manually on each database
export async function up(db) {
await db.schema.alterTable('users', table => {
table.integer('age')
})
}
// Migration runs automatically and tracks changesIt makes updating your database easy, safe, and repeatable across all environments.
When your app adds a new feature that needs extra data, migrations update the database without downtime or errors.
Manual database changes are error-prone and hard to track.
Migrations automate and record database updates safely.
This keeps your app's data structure consistent everywhere.
Practice
What is the main purpose of database migrations in a Next.js project?
Solution
Step 1: Understand database migrations
Database migrations are used to change the database schema safely without losing data.Step 2: Relate to Next.js usage
In Next.js, migrations help keep the database structure in sync with app changes.Final Answer:
To safely update the database structure as the app evolves -> Option BQuick Check:
Database migrations = safe schema updates [OK]
- Confusing migrations with UI or styling tasks
- Thinking migrations deploy the app
- Assuming migrations create app features
Which command is used to create and apply a new migration in a Next.js project using Prisma?
?
Solution
Step 1: Identify Prisma migration command
The commandnpx prisma migrate devcreates and applies migrations during development.Step 2: Differentiate from other commands
npm run buildbuilds the app,next devruns dev server, andnpx prisma generateregenerates Prisma client but does not migrate.Final Answer:
npx prisma migrate dev -> Option CQuick Check:
Migration command = npx prisma migrate dev [OK]
- Using 'npm run build' to migrate database
- Confusing 'npx prisma generate' with migration
- Running 'next dev' expecting migration
Given this Prisma schema change and migration command, what will happen?
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
// After adding a new field:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
isActive Boolean @default(true)
}
Command: npx prisma migrate dev --name add-isActiveSolution
Step 1: Understand schema change
A new Boolean fieldisActivewith defaulttrueis added to the User model.Step 2: Effect of migration command
The command creates a migration file that adds the new column with default value, so existing rows gettrueautomatically.Final Answer:
A new migration file is created and applied adding the isActive column with default true -> Option AQuick Check:
Adding field with default = migration applies safely [OK]
- Thinking migration fails due to missing data for new field
- Assuming schema does not change without manual SQL
- Believing app crashes immediately after migration
What is wrong with this migration command usage in a Next.js project?
npx prisma migrate dev --name
Solution
Step 1: Check command syntax
The--nameflag requires a migration name string after it.Step 2: Identify missing argument
Here, no name is provided, so the command will error out.Final Answer:
The migration name is missing after --name flag -> Option DQuick Check:
Migration name required after --name [OK]
- Omitting migration name after --name
- Confusing migrate dev with migrate deploy
- Using npm instead of npx unnecessarily
You want to add a new required field age Int to your User model in Prisma, but your database already has users without this field. What is the best way to handle this migration?
Solution
Step 1: Understand required field constraints
Adding a required fieldage Intdirectly will fail because existing rows lack this data.Step 2: Use a safe migration approach
First add the field as optional (age Int?), migrate, then update existing data, and finally change it to required.Final Answer:
Addage Int?as optional first, migrate, then backfill data, then make it required -> Option AQuick Check:
Make new required fields optional first [OK]
- Adding required field directly causing migration failure
- Deleting data unnecessarily
- Skipping migration and causing runtime errors
