Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a database migration in the context of Next.js applications?
A database migration is a way to change the database schema over time in a controlled and organized manner. It helps keep the database structure in sync with the application code.
Click to reveal answer
beginner
Why should you use migrations instead of manually changing the database?
Migrations ensure changes are repeatable, trackable, and reversible. They prevent errors from manual edits and help teams work together safely on database changes.
Click to reveal answer
beginner
Which tool is commonly used with Next.js for managing database migrations?
Prisma is a popular tool used with Next.js that provides an easy way to write and run database migrations using its schema and CLI commands.
Click to reveal answer
intermediate
What command do you run to create a new migration with Prisma in a Next.js project?
You run npx prisma migrate dev --name descriptive_migration_name to create and apply a new migration during development.
Click to reveal answer
intermediate
How do migrations help when deploying a Next.js app to production?
Migrations allow you to update the production database schema safely and consistently, ensuring the app and database stay compatible without losing data.
Click to reveal answer
What is the main purpose of a database migration?
ATo backup the database automatically
BTo speed up database queries
CTo change the database schema safely over time
DTo delete old data from the database
✗ Incorrect
Migrations are designed to safely change the database structure as the app evolves.
Which tool integrates well with Next.js for migrations?
AWebpack
BReact Router
CExpress
DPrisma
✗ Incorrect
Prisma is a database toolkit that works well with Next.js for schema and migrations.
What does the command npx prisma migrate dev do?
ACreates and applies a new migration during development
BDeletes all migrations
CStarts the Next.js development server
DGenerates React components
✗ Incorrect
This command creates a migration file and applies it to the database in development.
Why is it important to track migrations in version control?
ATo avoid writing code
BTo share database changes with the team
CTo reduce server costs
DTo increase app speed
✗ Incorrect
Tracking migrations helps teams stay in sync on database changes.
What happens if you run migrations on a production database?
AThe database schema updates safely to match the app
BThe app crashes immediately
CAll data is erased
DNothing happens
✗ Incorrect
Migrations update the production database schema without losing data.
Explain what database migrations are and why they are important in Next.js projects.
Think about how your app's data structure changes over time.
You got /3 concepts.
Describe the steps to create and apply a new migration using Prisma in a Next.js app.
Focus on the Prisma CLI commands and schema updates.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of database migrations in a Next.js project?
easy
A. To deploy the Next.js app to the server
B. To safely update the database structure as the app evolves
C. To create user interfaces automatically
D. To write CSS styles for the app
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 B
Which command is used to create and apply a new migration in a Next.js project using Prisma?
?
easy
A. next dev
B. npm run build
C. npx prisma migrate dev
D. npx prisma generate
Solution
Step 1: Identify Prisma migration command
The command npx prisma migrate dev creates and applies migrations during development.
Step 2: Differentiate from other commands
npm run build builds the app, next dev runs dev server, and npx prisma generate regenerates Prisma client but does not migrate.
Final Answer:
npx prisma migrate dev -> Option C
Quick Check:
Migration command = npx prisma migrate dev [OK]
Hint: Use 'npx prisma migrate dev' to create and apply migrations [OK]
Common Mistakes:
Using 'npm run build' to migrate database
Confusing 'npx prisma generate' with migration
Running 'next dev' expecting migration
3.
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-isActive
medium
A. A new migration file is created and applied adding the isActive column with default true
B. The migration fails because isActive is missing in existing rows
C. The database schema remains unchanged
D. The app crashes due to schema mismatch
Solution
Step 1: Understand schema change
A new Boolean field isActive with default true is 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 get true automatically.
Final Answer:
A new migration file is created and applied adding the isActive column with default true -> Option A
Quick Check:
Adding field with default = migration applies safely [OK]
Hint: Adding field with default creates migration safely [OK]
Common Mistakes:
Thinking migration fails due to missing data for new field
Assuming schema does not change without manual SQL
Believing app crashes immediately after migration
4.
What is wrong with this migration command usage in a Next.js project?
npx prisma migrate dev --name
medium
A. The command is correct and will run successfully
B. The command should be run with npm, not npx
C. The command should be prisma migrate deploy instead
D. The migration name is missing after --name flag
Solution
Step 1: Check command syntax
The --name flag 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 D
Quick Check:
Migration name required after --name [OK]
Hint: Always provide a name after --name flag [OK]
Common Mistakes:
Omitting migration name after --name
Confusing migrate dev with migrate deploy
Using npm instead of npx unnecessarily
5.
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?
hard
A. Add age Int? as optional first, migrate, then backfill data, then make it required
B. Add age Int directly and run migration; it will succeed automatically
C. Delete all existing users before migration to avoid errors
D. Skip migration and add the field only in the app code
Solution
Step 1: Understand required field constraints
Adding a required field age Int directly 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:
Add age Int? as optional first, migrate, then backfill data, then make it required -> Option A
Quick Check:
Make new required fields optional first [OK]
Hint: Add new required fields as optional first [OK]
Common Mistakes:
Adding required field directly causing migration failure