0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · 강의

데이터베이스 스키마 마이그레이션

원활한 개발과 배포를 위해 Prisma Migrate를 사용하여 데이터베이스 스키마 변경 사항을 관리합니다.

데이터베이스 스키마 마이그레이션은(는) CoddyKit의 무료 Next.js 15 Fullstack (App Router + Server Actions) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Next.js 15 Fullstack (App Router + Server Actions) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Next.js 15 Fullstack (App Router + Server Actions) 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

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:

  1. Generates a new migration file (SQL) in the prisma/migrations folder.
  2. Applies the migration to your database.
npx prisma migrate dev --name initial_setup

Understanding 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.js or migration.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_email

Applying 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 deploy

Checking 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 status

Prisma 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 dev creates and applies migrations in development.
  • npx prisma migrate deploy applies existing migrations in production.
  • npx prisma migrate status checks the current migration state.

This powerful tool ensures your database structure stays consistent and aligned with your application's needs as it grows.

자주 묻는 질문

“데이터베이스 스키마 마이그레이션” 강의는 무료인가요?

네 — “데이터베이스 스키마 마이그레이션” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Next.js 15 Fullstack (App Router + Server Actions) 강의 전체를 잠금 해제할 수 있습니다. Next.js 15 Fullstack (App Router + Server Actions) 강의에는 총 4개의 강의가 포함되어 있습니다.

“데이터베이스 스키마 마이그레이션”에서 뭘 배우나요?

원활한 개발과 배포를 위해 Prisma Migrate를 사용하여 데이터베이스 스키마 변경 사항을 관리합니다. 브라우저에서 직접 실행하는 실습 코드로 Next.js 15 Fullstack (App Router + Server Actions)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Next.js 15 Fullstack (App Router + Server Actions)을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Next.js 15 Fullstack (App Router + Server Actions)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“데이터베이스 스키마 마이그레이션” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Next.js 15 Fullstack (App Router + Server Actions) 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Next.js 15 Fullstack (App Router + Server Actions) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Prisma ORM 설정
  2. Server Actions를 활용한 CRUD
  3. 데이터베이스 스키마 마이그레이션
  4. 데이터베이스 시딩과 연결 풀링
← Next.js 15 Fullstack (App Router + Server Actions)(으)로 돌아가기