Skip to content
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

Correctly transfer 'last_updated_by' for annotations and scores when merging users #5894

Merged
merged 2 commits into from
Oct 29, 2024

Conversation

jorg-vr
Copy link
Contributor

@jorg-vr jorg-vr commented Oct 28, 2024

This pull request fixes the merge users script for transferring the last_updated_by status on non owned submissions. The same fix is also applied for scores.

  • test are added

@jorg-vr jorg-vr self-assigned this Oct 28, 2024
@jorg-vr jorg-vr requested a review from bmesuere as a code owner October 28, 2024 10:52
Copy link

coderabbitai bot commented Oct 28, 2024

Walkthrough

The changes in this pull request focus on the User model's merge_into method within app/models/user.rb. The modifications refine how annotations and scores are processed during user account merging, specifically updating the last_updated_by attribute for both annotations and scores associated with the merging user. New test cases have been added in test/models/user_test.rb to ensure that these functionalities work correctly, verifying that the last_updated_by attribute is updated as expected when merging users.

Changes

File Change Summary
app/models/user.rb Modified merge_into method to update last_updated_by for annotations and scores instead of last_updated_by_id.
test/models/user_test.rb Added new tests: merge should transfer last_updated_by of non owned annotations and merge should transfer scores last updated by to verify merging functionality for annotations and scores.

Sequence Diagram(s)

sequenceDiagram
    participant UserA as User A
    participant UserB as User B
    participant Annotation as Annotation
    participant Score as Score

    UserA->>UserB: Initiate merge
    UserB->>Annotation: Retrieve annotations
    Annotation->>UserB: Get last_updated_by_id
    alt last_updated_by_id matches UserA
        Annotation->>UserB: Update last_updated_by to UserB
    end
    UserB->>Score: Retrieve scores
    Score->>UserB: Get last_updated_by_id
    alt last_updated_by_id matches UserA
        Score->>UserB: Update last_updated_by to UserB
    end
    UserB->>UserA: Complete merge
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between eb69759 and bc8b877.

📒 Files selected for processing (2)
  • app/models/user.rb (1 hunks)
  • test/models/user_test.rb (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/models/user.rb
🔇 Additional comments (2)
test/models/user_test.rb (2)

849-872: LGTM! Well-structured test for annotation ownership transfer.

The test thoroughly verifies that the last_updated_by attribute is correctly transferred during user merging for both owned and non-owned annotations.


874-891: LGTM! Comprehensive test for score ownership transfer.

The test effectively verifies that the last_updated_by attribute is correctly transferred for scores during user merging.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8ef39d5 and eb69759.

📒 Files selected for processing (2)
  • app/models/user.rb (1 hunks)
  • test/models/user_test.rb (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
app/models/user.rb

[warning] 433-434: app/models/user.rb#L433-L434
Added lines #L433 - L434 were not covered by tests

🔇 Additional comments (2)
app/models/user.rb (1)

433-434: 🛠️ Refactor suggestion

Verify annotation ownership transfer logic.

The changes correctly handle both aspects of annotation transfer:

  1. Transferring owned annotations to the target user
  2. Updating last_updated_by references to point to the target user

However, there's a potential race condition since the updates are performed separately.

Let's verify the annotation relationships and constraints:

Consider combining both updates into a single query to prevent potential race conditions:

-      annotations.each { |a| a.update!(user: other) }
-      Annotation.where(last_updated_by_id: id).find_each { |a| a.update!(last_updated_by: other) }
+      Annotation.where(user_id: id).or(Annotation.where(last_updated_by_id: id))
+               .update_all(
+                 "user_id = CASE WHEN user_id = #{id} THEN #{other.id} ELSE user_id END,
+                  last_updated_by_id = CASE WHEN last_updated_by_id = #{id} THEN #{other.id} ELSE last_updated_by_id END"
+               )
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 433-434: app/models/user.rb#L433-L434
Added lines #L433 - L434 were not covered by tests

test/models/user_test.rb (1)

849-873: LGTM! Well-structured test case that verifies the last_updated_by transfer.

The test effectively covers both scenarios:

  1. Annotations owned by the merging user
  2. Annotations owned by others but last updated by the merging user

The assertions properly verify that the last_updated_by field is correctly transferred in both cases.

app/models/user.rb Show resolved Hide resolved
@jorg-vr jorg-vr changed the title Correctly transfer 'last_updated_by' for annotations when merging users Correctly transfer 'last_updated_by' for annotations and scores when merging users Oct 28, 2024
@jorg-vr jorg-vr merged commit b34ff3a into main Oct 29, 2024
14 checks passed
@jorg-vr jorg-vr deleted the fix/merge_users branch October 29, 2024 13:55
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