C.W.K.
Stream
Lesson 03 of 10 · published

Migration: 스키마 진화

~14 min · apps, migrations

Level 0스키마 새싹
0 XP0/86 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

마이그레이션이 스키마의 source of truth

마이그레이션 = 스키마의 versioned, reproducible 변경. repo 의 20260503_add_user_avatar.sql. dev 에 한 번, staging 에 한 번, prod 에 한 번 실행 — 같은 변경, 같은 순서, 모든 환경.

필수 룰

  1. 모든 스키마 변경이 마이그레이션. "prod 에 ALTER 직접 실행" 없음. 그게 reproducibility 체인 영원히 깨뜨림.
  2. Ship 후 마이그레이션은 불변. 이미 prod 에 실행된 마이그레이션 수정 필요하면 새 마이그레이션 작성. 적용된 마이그레이션 절대 편집 금지.
  3. 마이그레이션 forward 와 back 둘 다 테스트. 대부분 도구가 down() 방향 지원. dev 에서 사용; prod 엔 forward-only 신뢰.
  4. Destructive 마이그레이션은 multi-step. 컬럼 이름 변경 = (1) 새 컬럼 추가, (2) 백필, (3) 둘 다에 쓰기 시작, (4) read 전환, (5) 옛 컬럼 drop. 배포 가로질러.

도구 zoo

Alembic (Python/SQLAlchemy), Flyway (JVM, 언어 무관), goose (Go), Prisma Migrate (Node/TypeScript), supabase migration / db push (Supabase), graphile-migrate (Postgres-native). repo 당 하나, 절대 둘 안 됨.

Code

Alembic 마이그레이션·python
# alembic/versions/20260503_add_user_avatar.py
def upgrade():
    op.add_column('users', sa.Column('avatar_url', sa.Text(), nullable=True))
    op.execute("UPDATE users SET avatar_url = 'https://...' WHERE id = 1")

def downgrade():
    op.drop_column('users', 'avatar_url')
Plain SQL 마이그레이션 (Flyway / supabase)·sql
-- migrations/20260503120000_add_user_avatar.sql
ALTER TABLE users ADD COLUMN avatar_url TEXT;
UPDATE users SET avatar_url = '/avatars/default.png' WHERE avatar_url IS NULL;
ALTER TABLE users ALTER COLUMN avatar_url SET NOT NULL;
Supabase CLI flow·bash
supabase migration new add_user_avatar
# 생성된 마이그레이션 파일 편집
supabase db push --include-all

External links

Exercise

샌드박스에서 프로젝트 도구로 작은 스키마 변경의 마이그레이션 생성. 적용. 첫 거에 의존하는 두 번째 마이그레이션 생성. 적용. 그 다음 'oops, fix forward' 연습: 첫 거가 잘못한 거 수정하는 세 번째 마이그레이션.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.