Skip to content

Conversation

@robin-watcha
Copy link

요약

  • serialized_property setter에서 동일한 값으로 설정 시 불필요하게 dirty로 마킹되는 버그 수정
  • 기존 값과 새 값을 비교하여, 실제로 값이 변경될 때만 _will_change! 호출

문제 현상

device = BaseDevice.find(1)  # press_user_id: 123
device.press_user_id = 123   # 같은 값으로 설정
device.changed?              # => true (문제!)
device.save!                 # 불필요한 UPDATE 발생

수정 내용

  • _write_sprop_value 메서드: write_attribute, []= 호출 시 값 비교 로직 추가
  • _define_serialized_property setter: 일반 setter 호출 시 값 비교 로직 추가

테스트

  • 같은 값 설정 시 dirty 안됨
  • 다른 값 설정 시 정상 dirty
  • nil → 값, 값 → nil 변경 시 dirty
  • nil → nil 설정 시 dirty 안됨
  • write_attribute, []= 접근자 테스트
  • setter proc 적용 후 동일 값 테스트

🤖 Generated with Claude Code

…marking when values remain unchanged. Add comprehensive test coverage for edge cases.
@gemini-code-assist
Copy link

Summary of Changes

Hello @robin-watcha, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

이 Pull Request는 serialized_property를 사용하는 객체에서 불필요한 데이터베이스 업데이트를 방지하기 위해 dirty tracking 메커니즘을 최적화합니다. 기존에는 속성 값이 변경되지 않았음에도 불구하고 객체가 dirty로 마킹되어 불필요한 UPDATE 쿼리가 발생할 수 있었으나, 이제는 실제 값 변경이 있을 때만 dirty 상태를 설정하여 시스템의 효율성과 성능을 향상시킵니다.

Highlights

  • 불필요한 dirty marking 수정: serialized_property setter에서 동일한 값으로 설정할 때 객체가 불필요하게 dirty로 마킹되는 버그를 수정했습니다. 이제 값이 실제로 변경될 때만 _will_change!가 호출됩니다.
  • 값 비교 로직 추가: _write_sprop_value 메서드와 _define_serialized_property setter에 기존 값과 새 값을 비교하는 로직이 추가되어, 변경이 없을 경우 dirty marking을 건너뜁니다.
  • 테스트 케이스 확장: 동일한 값 설정, 다른 값 설정, nil 처리, write_attribute, []= 접근자, setter proc 적용 등 다양한 시나리오에 대한 새로운 테스트 케이스를 추가하여 변경 사항의 정확성을 검증했습니다.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@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

serialized_property setter에서 동일한 값을 할당할 때 불필요하게 dirty로 마킹되던 문제를 수정한 좋은 변경입니다. 이전 값과 새 값을 비교하여 실제로 변경이 있을 때만 _will_change!를 호출하도록 수정한 접근 방식이 올바릅니다.

_write_sprop_value_define_serialized_property 두 곳에 변경 사항을 일관되게 적용한 점이 좋습니다. 또한, 다양한 시나리오(같은 값, 다른 값, nil 처리, setter proc 등)를 포괄하는 종합적인 테스트 케이스를 추가하여 수정된 내용의 안정성을 크게 높인 점이 인상적입니다.

한 가지 개선 제안으로, 두 메서드에 중복되는 로직이 있어 이를 별도의 헬퍼 메서드로 추출하여 코드 유지보수성을 높이는 방안을 리뷰 의견으로 남겼습니다. 전반적으로 훌륭한 수정입니다!

Comment on lines 30 to +49
hsh = send(col)
if value.nil?
old_value = hsh[key.to_s]

new_value = if value.nil?
nil
else
apply_setter && setting[:setter] ? setting[:setter].call(value) : value
end

# Skip dirty marking if value hasn't changed
return old_value if old_value == new_value

send("#{col}_will_change!")

if new_value.nil?
hsh.delete(key.to_s)
else
value = setting[:setter].call(value) if apply_setter && setting[:setter]
hsh[key.to_s] = value
hsh[key.to_s] = new_value
end
value
new_value

Choose a reason for hiding this comment

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

medium

이 로직은 _define_serialized_property 메서드 내에 정의된 setter (326-345행)의 로직과 거의 동일하여 코드 중복이 발생합니다. 유지보수성을 높이기 위해 이 공통 로직을 별도의 private 헬퍼 메서드로 추출하는 것을 고려해 보세요.

예를 들어, 다음과 같은 헬퍼 메서드를 if defined?(ActiveRecord::Base) && base < ActiveRecord::Base 블록 내에 정의할 수 있습니다.

private

def _update_sprop_if_changed(column, key, hsh, old_value, new_value)
  return old_value if old_value == new_value

  send("#{column}_will_change!")

  if new_value.nil?
    hsh.delete(key.to_s)
  else
    hsh[key.to_s] = new_value
  end
  new_value
end

그런 다음 _write_sprop_value_define_serialized_property의 setter에서 이 헬퍼 메서드를 호출하여 중복을 제거할 수 있습니다. 이렇게 하면 코드가 더 간결해지고 향후 수정이 필요할 때 한 곳만 변경하면 됩니다.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants