-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: 메뉴 이미지 업로드 로직 리팩토링 #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.nowait.domaincorerdb.menu.repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
@@ -11,4 +12,5 @@ | |
| @Repository | ||
| public interface MenuImageRepository extends JpaRepository<MenuImage, Long> { | ||
| List<MenuImage> findByMenu(Menu menu); | ||
| Optional<MenuImage> findByMenuId(Long menuId); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chain메뉴당 단일 이미지 보장 + 경합 방지(유니크 + 잠금 + 메서드 명시화) 필요
적용 예(리포지토리 메서드): - Optional<MenuImage> findByMenuId(Long menuId);
+ @Lock(LockModeType.PESSIMISTIC_WRITE)
+ Optional<MenuImage> findByMenu_Id(Long menuId);추가 import: import javax.persistence.LockModeType;
import org.springframework.data.jpa.repository.Lock;마이그레이션(예시): ALTER TABLE menu_image ADD CONSTRAINT uk_menu_image_menu UNIQUE (menu_id);다음 스크립트로 엔티티 매핑과 유니크 제약 존재 여부를 확인해 주세요. 🏁 Script executed: #!/bin/bash
# MenuImage 엔티티 매핑 확인
fd -a "MenuImage.java"
rg -nC3 -S "class MenuImage|@Table|@Index|@ManyToOne|@OneToOne|menu" $(fd -a "MenuImage.java")
# SQL 마이그레이션 내 유니크 제약 검색
rg -n -S "menu_image|unique|constraint|menu_id" -- **/*.sql **/db/** **/migration/** 2>/dev/null || trueLength of output: 1074 메뉴당 단일 이미지 보장 및 경합 방지 조치 적용 필요
// src/main/java/com/nowait/domaincorerdb/menu/repository/MenuImageRepository.java
- Optional<MenuImage> findByMenuId(Long menuId);
+ @Lock(LockModeType.PESSIMISTIC_WRITE)
+ Optional<MenuImage> findByMenu_Id(Long menuId);// 추가 import
import javax.persistence.LockModeType;
import org.springframework.data.jpa.repository.Lock;-- 예시 마이그레이션: db/migration/Vxxx__add_menu_image_unique.sql
ALTER TABLE menu_images
ADD CONSTRAINT uk_menu_images_menu_id UNIQUE (menu_id);🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
삭제→업로드 순서는 실패 시 이미지 유실 위험. 업로드→DB 갱신→커밋 후 S3 삭제로 전환 권장
findByMenu_Id(또는 잠금 적용)로 일관화하세요.최소 변경 예(해당 블록 수정):
커밋 후 기존 S3 삭제(메서드 하단, save 이후에 추가):
참고: 가능하면 “기존 엔티티를 업데이트(setImageUrl,setFileKey) 방식”이 가장 단순하고 경합에도 강합니다.