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

데이터베이스 migration

~12 min · migrations, schema, downtime

Level 0Apprentice
0 XP0/101 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

새벽 3시에 깨우는 job

DB migration 은 deploy 가 죽으러 가는 곳이야. 코드는 초 단위로 앞으로 굴러; schema 는 제거할 때까지 살아. Migration 순서 틀리면 deploy rollback 으로 되돌릴 수 없는 outage.

Expand-and-contract 패턴

옛 + 새 코드 둘 다 쓰는 데이터 만지는 migration:

  1. Expand — 새 column / table / index 추가. 옛 코드 무시; 새 코드 사용 가능. 하위 호환.
  2. Migrate — 새 구조에 데이터 backfill. 보통 deploy 가 아니라 별도 백그라운드 job.
  3. Switch — 새 구조 read/write 코드 deploy. 옛 코드 여전히 허용.
  4. Contract — 100% 코드가 새 구조 사용하면 (그리고 한 release cycle 유지) 옛 column / table 삭제.

Migration runner 위치

  • Pre-deploy job — 새 코드가 사용자 닿기 전 migration. 추가 (expand) migration 에 안전.
  • App startup — 코드가 boot 에 migration. 위험: 여러 인스턴스 동시 시작 시 race condition, cold-start 길어짐.
  • 수동 / on-demand — 파괴적 migration (column drop) 은 수동 승인 뒤에 gate, deploy 와 별도 실행.

Code

사전-deploy migration job·yaml
  migrate:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: production
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123:role/gha-migrate
          aws-region: us-east-1
      - run: |
          # Run only additive migrations here.
          # Destructive ones live in a separate manual workflow.
          DATABASE_URL=$(aws ssm get-parameter --name /prod/db-url --with-decryption --query Parameter.Value --output text)
          DATABASE_URL="$DATABASE_URL" alembic upgrade head

  deploy:
    needs: migrate
    runs-on: ubuntu-latest
    environment: production
    steps:
      - run: ./deploy.sh

External links

Exercise

최근 5 migration audit. 각각 expand / migrate / switch / contract 분류. 한 PR 에 두 phase 합친 거 찾기 (사건의 흔한 원인). 다르게 할 거 적어.

Progress

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

댓글 0

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

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