users tableDatabase migrations in NextJS - Mini Project: Build & Apply
Start learning this pattern below
Jump into concepts and practice - no test required
migrationSQL that contains the SQL command to create a users table with columns id (integer primary key), name (text), and email (text). Use a template string for the SQL.Use backticks ` to create a multi-line string for the SQL command.
connectionString and set it to the string 'postgresql://user:password@localhost:5432/mydb' to represent the database connection URL.Use single quotes for the connection string.
runMigration that connects to the database using connectionString, runs the migrationSQL command, and then closes the connection. Use the pg library's Client class. Import { Client } from 'pg' at the top.Remember to use await for asynchronous calls and to close the client connection.
runMigration function as a named export from the module.Use a named export to make the function available to other files.
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
