Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/com/daramg/server/common/config/FlywayConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.daramg.server.common.config;

import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FlywayConfig {

@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return flyway -> {
flyway.repair();
flyway.migrate();
};
Comment on lines +13 to +16
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

애플리케이션 시작 시마다 flyway.repair()를 실행하는 것은 위험할 수 있습니다. repair()는 다음과 같은 동작을 수행하는데, 이는 의도치 않은 부작용을 일으킬 수 있습니다.

  1. 실패한 마이그레이션 기록 삭제: 마이그레이션이 중간에 실패한 경우, repair는 실패 기록만 제거합니다. 이 상태에서 다시 migrate를 실행하면, 멱등성이 보장되지 않은 스크립트는 다시 실패하거나 데이터를 손상시킬 수 있습니다. 모든 마이그레이션을 멱등성 있게 작성하도록 강제하는 것은 좋지만, 실수를 방지하는 안전장치가 사라지는 셈입니다.

  2. 체크섬 불일치 자동 해결: 이미 적용된 마이그레이션 파일의 내용이 변경되면 체크섬 오류가 발생합니다. repair는 이를 현재 파일 기준으로 강제 업데이트하는데, 이는 배포된 DB 스키마와 Flyway가 관리하는 버전 간의 불일치를 유발할 수 있습니다. 이는 매우 위험하며, 어떤 변경이 적용되었는지 추적하기 어렵게 만듭니다.

Flyway의 마이그레이션 실패는 중요한 이벤트이며, 개발자가 직접 원인을 파악하고 수동으로 repair나 다른 조치를 취하는 것이 안전합니다. 이번 문제의 경우 V9 스크립트를 멱등적으로 수정한 것으로 충분하며, 이 자동 복구 로직은 제거하는 것을 강력히 권장합니다.

}
}
2 changes: 1 addition & 1 deletion src/main/resources/db/migration/V9__add_role_to_users.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE users ADD COLUMN role VARCHAR(20) NOT NULL DEFAULT 'USER';
ALTER TABLE users ADD COLUMN IF NOT EXISTS role VARCHAR(20) NOT NULL DEFAULT 'USER';
Loading