Skip to content

[client] tb_client.scopes 정규화 및 삭제 정합성 보장#307

Open
wwwcomcomcomcom wants to merge 5 commits intodevelopfrom
refactor/client-scope-normalization
Open

[client] tb_client.scopes 정규화 및 삭제 정합성 보장#307
wwwcomcomcomcom wants to merge 5 commits intodevelopfrom
refactor/client-scope-normalization

Conversation

@wwwcomcomcomcom
Copy link
Copy Markdown
Collaborator

@wwwcomcomcomcom wwwcomcomcomcom commented Apr 8, 2026

개요

tb_client.scopes JSON 텍스트 컬럼을 tb_client_scope 조인 테이블로 정규화하고, ApplicationOAuthScope 삭제 시 Client의 scope 참조가 정리되지 않던 문제를 해결하였습니다.

본문

문제

ClientJpaEntity.scopesStringSetConverter를 통해 JSON 배열 텍스트(["appId:scopeName", ...])로 저장되어 있어 다음 문제가 발생하였습니다:

  • OAuthScope 삭제 시 tb_client에 삭제된 scope 문자열이 잔존
  • Application 삭제 시 해당 application의 모든 scope 문자열이 잔존
  • Client Credentials 토큰 발급 시 Oauth2TokenServiceImpl.stringsToScopes()에서 삭제된 scope를 만나면 500 에러 발생
  • 기존 배열 텍스트로 정합성을 유지하기 위해선 LIKE 문 사용이 불가피하여 full table search가 발생할 우려가 있음

변경 내용

정규화

  • ClientJpaEntity.scopes@ElementCollection + @CollectionTable("tb_client_scope")로 변경하였습니다.
  • @OnDelete(action = OnDeleteAction.CASCADE)를 적용하여 Client 삭제 시 DB 레벨에서 scope 행이 자동 삭제됩니다.
  • FetchType.EAGER를 설정하여 Client 조회 시 scopes가 항상 함께 로딩됩니다.

삭제 정합성

  • DeleteOAuthScopeServiceImpl에서 scope 삭제 전 removeScopeFromClients()를 호출하여 해당 scope를 참조하는 Client를 정리합니다.
  • DeleteApplicationServiceImpl에서 Application 삭제 전 removeScopesByApplicationId()를 호출하여 관련 scope를 일괄 정리합니다.
  • 두 메서드 모두 tb_client_scope.scope 인덱스를 활용하는 네이티브 쿼리로 구현하였습니다.

QueryDSL fetch join

  • ClientJpaCustomRepositoryImpl의 목록 조회 쿼리에 leftJoin(clientJpaEntity.scopes).fetchJoin()을 추가하여 추가 SELECT 없이 한 번의 쿼리로 scopes를 로딩합니다.

DB 마이그레이션

tb_client_scope 테이블을 추가해야합니다. 기존 tb_client.scopes 컬럼의 데이터를 새 테이블로 이관해야 합니다.

추가 리뷰 요청

삭제시 정합성 유지를 위해 full table search가 불가피하여 이러한 변경을 하게 되었는데, client 도메인은 읽기가 훨씬 더 자주 일어나다보니 적합한 trade off 인지 고민입니다. 다른 방법이 있다면 생각 공유해주시길 바랍니다. ex) json을 지원하는 다른 db 사용

@wwwcomcomcomcom wwwcomcomcomcom added bug:버그 무언가 작동하지 않습니다 enhancement:개선작업 새 기능 또는 기존 코드 개선에 관한 내용입니다 labels Apr 8, 2026
@github-actions github-actions bot added the waiting for review:검토 대기 확인을 대기하고 있습니다 label Apr 8, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request migrates OAuth scope storage from a text field to a dedicated collection table using @ElementCollection. Key updates include adding native delete queries to ClientJpaRepository, updating QueryDSL fetch joins, and implementing scope cleanup in deletion services. Review feedback recommends optimizing read performance with JSON serialization, using val for collection properties per the style guide, and refining collection updates while noting that scope-related logic in the web module is slated for future removal.

Copy link
Copy Markdown
Member

@snowykte0426 snowykte0426 left a comment

Choose a reason for hiding this comment

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

전체적으로 괜찮은 것 같은데 Client뿐 아니라 API키 조회 부분도 정규화를 진행해 일관성을 맞추는게 어떠할까요?

그리고 DB 마이그레이션 스크립트도 작성 부탁드리겠습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug:버그 무언가 작동하지 않습니다 enhancement:개선작업 새 기능 또는 기존 코드 개선에 관한 내용입니다 waiting for review:검토 대기 확인을 대기하고 있습니다

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants