Skip to content

Conversation

@hahn-kev
Copy link
Collaborator

@hahn-kev hahn-kev commented May 2, 2025

this enables changes to better enforce constraints when running to avoid conflicts with unique constraints

Summary by CodeRabbit

  • New Features
    • Introduced the ability to retrieve objects by type and name, with optional inclusion of deleted items.
  • Bug Fixes
    • Improved handling of tags with duplicate names, ensuring correct behavior when creating or updating tags with identical names, even in non-sequential commit orders.
  • Tests
    • Added new tests to verify correct handling of tags with the same name in out-of-order commit scenarios.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 2, 2025

Walkthrough

A new asynchronous method for retrieving objects by type and JSON type name was introduced in the IChangeContext interface and implemented in the ChangeContext class. The SetTagChange class updated its NewEntity and ApplyChange methods to be asynchronous, adding logic to check for duplicate tag names and conditionally mark tags as deleted. Two new asynchronous tests were added to verify tag name uniqueness and update scenarios with out-of-order commits. These changes collectively enhance asynchronous handling and tag management in the codebase.

Changes

File(s) Change Summary
src/SIL.Harmony.Core/IChangeContext.cs Added generic async method GetObjectsOfType<T> to the IChangeContext interface.
src/SIL.Harmony/Changes/ChangeContext.cs Implemented the new GetObjectsOfType<T> method in the ChangeContext class.
src/SIL.Harmony.Sample/Changes/SetTagChange.cs Updated NewEntity and ApplyChange to be asynchronous; added logic for duplicate tag name checks and deletion.
src/SIL.Harmony.Tests/DataModelReferenceTests.cs Added two new async tests for tag creation and update with duplicate names and out-of-order commits.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Method
    participant SetTagChange as SetTagChange
    participant Context as IChangeContext
    participant Tag as Tag Entity

    Test->>SetTagChange: NewEntity(commit, context)
    SetTagChange->>Context: GetObjectsOfType<Tag>("Tag")
    Context-->>SetTagChange: Async enumerable of tags
    SetTagChange->>SetTagChange: Check for existing tag with same text
    SetTagChange->>Tag: Create new Tag (set DeletedAt if duplicate)
    SetTagChange-->>Test: Return new Tag

    Test->>SetTagChange: ApplyChange(entity, context)
    SetTagChange->>Context: GetObjectsOfType<Tag>("Tag")
    Context-->>SetTagChange: Async enumerable of tags
    SetTagChange->>SetTagChange: Check for other tag with same text
    SetTagChange->>Tag: Update DeletedAt if duplicate, update text
    SetTagChange-->>Test: Complete
Loading

Poem

In the garden of tags, async winds blow,
Where duplicate names now softly show.
Contexts fetch types with a gentle hop,
And tests ensure the rules don’t stop.
Out-of-order changes, rabbits don’t fear—
For harmony’s code is crystal clear!
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 20ceb62 and ff1e28f.

📒 Files selected for processing (1)
  • src/SIL.Harmony.Tests/DataModelReferenceTests.cs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/SIL.Harmony.Tests/DataModelReferenceTests.cs
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build
  • GitHub Check: build
  • GitHub Check: Analyze (csharp)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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
Contributor

@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: 2

🧹 Nitpick comments (1)
src/SIL.Harmony.Sample/Changes/SetTagChange.cs (1)

11-20: Convert to async and handle duplicate tag names

The method now correctly uses the new GetObjectsOfType<T> method to check for duplicate tag names, and sets the DeletedAt property accordingly.

Fix the extra space in the ternary operator:

-    DeletedAt = tagExists ?  commit.DateTime : null
+    DeletedAt = tagExists ? commit.DateTime : null
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4af7d83 and 20ceb62.

📒 Files selected for processing (4)
  • src/SIL.Harmony.Core/IChangeContext.cs (1 hunks)
  • src/SIL.Harmony.Sample/Changes/SetTagChange.cs (1 hunks)
  • src/SIL.Harmony.Tests/DataModelReferenceTests.cs (1 hunks)
  • src/SIL.Harmony/Changes/ChangeContext.cs (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/SIL.Harmony.Core/IChangeContext.cs (2)
src/SIL.Harmony.Tests/ObjectBaseTestingHelpers.cs (1)
  • T (5-8)
src/SIL.Harmony/SyncHelper.cs (1)
  • T (80-86)
🔇 Additional comments (3)
src/SIL.Harmony.Core/IChangeContext.cs (1)

17-17: Excellent addition to the interface!

The new GetObjectsOfType<T> method follows the same pattern as the existing GetObjectsReferencing method. The type constraint where T : class is appropriate since we're dealing with entity objects, and the parameter jsonTypeName allows for precise type filtering beyond what the generic parameter alone would provide.

src/SIL.Harmony/Changes/ChangeContext.cs (1)

29-34: Well-implemented method!

The implementation is clean and efficient:

  1. It uses GetSnapshotsWhere with a predicate that correctly handles both the deletion status and type name filtering
  2. It projects the results using Select to get the DbObject property
  3. It uses OfType<T>() to ensure type safety

This provides a solid foundation for enforcing unique constraints as mentioned in the PR objectives.

src/SIL.Harmony.Sample/Changes/SetTagChange.cs (1)

23-32: Consider the order of operations when handling duplicate tags

The method correctly checks for duplicate tag names and marks the entity as deleted when needed. However, the text is still updated even if the entity is marked as deleted.

Is it intentional to update the Text property after the entity is marked as deleted? Consider whether it makes more sense to update the text first and then check for duplicates, or to return early after marking the entity as deleted:

 public override async ValueTask ApplyChange(Tag entity, IChangeContext context)
 {
     if (entity.Text == Text) return;
+    entity.Text = Text;
     var tagExists = await context.GetObjectsOfType<Tag>(nameof(Tag)).AnyAsync(t => t.Id != EntityId && t.Text == Text);
     if (tagExists)
     {
         entity.DeletedAt = context.Commit.DateTime;
     }
-    entity.Text = Text;
 }

or

 public override async ValueTask ApplyChange(Tag entity, IChangeContext context)
 {
     if (entity.Text == Text) return;
     var tagExists = await context.GetObjectsOfType<Tag>(nameof(Tag)).AnyAsync(t => t.Id != EntityId && t.Text == Text);
     if (tagExists)
     {
         entity.DeletedAt = context.Commit.DateTime;
+        entity.Text = Text;
+        return;
     }
     entity.Text = Text;
 }

Copy link
Contributor

@myieye myieye left a comment

Choose a reason for hiding this comment

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

For the purpose of documentation, I do think it would be valuable to assert the expected state at the end of each test, as code rabbit (somewhat poorly) suggested.

@hahn-kev hahn-kev merged commit 5207421 into main May 13, 2025
6 checks passed
@hahn-kev hahn-kev deleted the get-snapshots-by-type branch May 13, 2025 06:44
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.

3 participants