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

feat(torii): historical entity updates #3090

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Mar 10, 2025

#3030

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new table for storing historical entities, enhancing data management capabilities.
    • Added a new parameter to methods for tracking whether records are historical, improving data context during processing.
    • Updated the Relay struct to manage historical models more effectively.
    • Added a new method for initializing the Relay with historical models.
  • Chores

    • Enhanced data retrieval efficiency with backend optimizations for improved performance.

@Larkooo Larkooo marked this pull request as draft March 10, 2025 06:39
Copy link

coderabbitai bot commented Mar 10, 2025

Walkthrough

Ohayo sensei! This pull request introduces a new SQL migration that creates an entities_historical table for storing historical records. The table includes columns such as id, keys, event_id, data, model_id, executed_at, created_at, and updated_at, with default timestamps for the latter two. Additionally, it modifies the existing entity_model table by adding a historical_counter column of type BIGINT, initialized to zero, to optimize query performance.

Changes

File(s) Change Summary
crates/torii/migrations/20250310063604_historical_entities.sql Adds a new SQL migration that creates the entities_historical table with columns: id, keys, event_id, data, model_id, executed_at, created_at, updated_at (defaulting to current timestamp); also adds a historical_counter (BIGINT default 0) column to the entity_model table.
crates/torii/cli/src/options.rs Updates documentation for the historical field in EventsOptions struct to clarify its purpose regarding historical models during indexing.
crates/torii/indexer/src/processors/mod.rs Renames historical_events to historical_models in EventProcessorConfig struct and updates the is_historical method accordingly.
crates/torii/indexer/src/processors/store_set_record.rs Adds historical parameter to process method in StoreSetRecordProcessor, using config.is_historical to determine its value.
crates/torii/indexer/src/processors/store_update_member.rs Modifies process method in StoreUpdateMemberProcessor to include historical parameter in db.set_entity call.
crates/torii/indexer/src/processors/store_update_record.rs Updates process method in StoreUpdateRecordProcessor to include historical parameter in db.set_entity call.
crates/torii/sqlite/src/executor/mod.rs Introduces EntityQuery struct and modifies SetEntity variant in QueryType to use this new struct; updates logic in handle_query_message for handling historical entities.
crates/torii/sqlite/src/lib.rs Adds is_historical parameter to set_entity method in Sql implementation, with a conditional check included in the method logic.
crates/torii/libp2p/src/server/mod.rs Adds historical_models field to Relay struct and modifies constructor and set_entity method to accommodate handling of historical models.
crates/torii/runner/src/lib.rs Updates Runner implementation to replace historical_events with historical_models and modifies relay server initialization to use cloned historical models.

Possibly related PRs

  • refactor(torii): update query logic for them to not block #2397: The changes in the main PR, which involve creating a new SQL migration for historical entities and modifying the entity_model table, are related to the retrieved PR as both involve updates to the set_entity method and the handling of QueryType, specifically the SetEntity variant, indicating a connection in how entities are processed and stored in the database.

Suggested reviewers

  • glihm

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 14be1dd and f36e171.

📒 Files selected for processing (1)
  • crates/torii/sqlite/src/lib.rs (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/torii/sqlite/src/lib.rs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-explorer

🪧 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.

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: 0

🧹 Nitpick comments (1)
crates/torii/migrations/20250310063604_historical_entities.sql (1)

1-2: Ohayo sensei, clear and welcoming comments!
The header comments nicely explain the purpose of the migration and the design rationale. Just a nitpick: the term "historicallity" may be a bit unconventional—if it's intentional domain slang, that's great; otherwise, consider a more standard term like "history tracking" for clarity.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5b83295 and 2d1d29a.

📒 Files selected for processing (1)
  • crates/torii/migrations/20250310063604_historical_entities.sql (1 hunks)
🔇 Additional comments (2)
crates/torii/migrations/20250310063604_historical_entities.sql (2)

3-16: Ohayo sensei, solid table creation logic!
The entities_historical table is defined with the expected columns and default timestamp settings. The decision to omit a primary key is clearly documented and appears to support the one-to-many relationship design. However, please verify that this design aligns with your application’s data integrity requirements—if uniqueness for each row might be needed later, you might consider a composite or surrogate key.


18-20: Ohayo sensei, the ALTER TABLE statement looks good!
Adding the historical_counter column to the entity_model table with a default value of 0 is well executed. Ensure that the entity_model table exists and that this schema change integrates smoothly with existing application logic.

@Larkooo Larkooo marked this pull request as ready for review March 10, 2025 07:04
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.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 2d1d29a and 31bf2d0.

📒 Files selected for processing (7)
  • crates/torii/cli/src/options.rs (1 hunks)
  • crates/torii/indexer/src/processors/mod.rs (1 hunks)
  • crates/torii/indexer/src/processors/store_set_record.rs (2 hunks)
  • crates/torii/indexer/src/processors/store_update_member.rs (2 hunks)
  • crates/torii/indexer/src/processors/store_update_record.rs (2 hunks)
  • crates/torii/sqlite/src/executor/mod.rs (2 hunks)
  • crates/torii/sqlite/src/lib.rs (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: docs
  • GitHub Check: ensure-wasm
  • GitHub Check: clippy
🔇 Additional comments (14)
crates/torii/indexer/src/processors/store_set_record.rs (2)

5-5: New import added for tag-based historical tracking.

Ohayo sensei! You've added the get_tag import from dojo_types, which will be used to determine if a model should have historical data tracking enabled.


106-115: Historical tracking implementation added to entity processing.

The implementation correctly determines if a model should be tracked historically by combining the namespace and name to create a tag, then checking against the config. This is a clean approach that maintains separation of concerns and adds the new functionality without disrupting the existing flow.

crates/torii/indexer/src/processors/mod.rs (2)

36-36: Terminology change from 'historical_events' to 'historical_models'.

Good semantic change sensei! This properly reflects that historical tracking is applied at the model level rather than the event level, which aligns better with the overall architecture.


43-43: Updated is_historical method to use the renamed field.

The method implementation has been correctly updated to match the field rename, maintaining consistency throughout the codebase.

crates/torii/indexer/src/processors/store_update_member.rs (2)

5-5: New import added for tag-based historical tracking.

Ohayo! This import addition mirrors the same change in store_set_record.rs, maintaining consistency across processor implementations.


125-135: Historical tracking implementation added to entity processing.

The implementation properly determines if a model should be tracked historically and passes this information to the database layer. The whitespace restructuring also improves readability.

crates/torii/sqlite/src/lib.rs (1)

322-322: Added is_historical parameter to set_entity method.

This parameter addition allows the caller to specify whether the entity should be tracked historically, which is essential for the feature being implemented.

crates/torii/cli/src/options.rs (1)

276-277: Documentation update looks good!

Ohayo sensei! The documentation update for the historical field is clearer and more comprehensive now, indicating that historical tracking applies to both event messages and entities.

Also applies to: 281-281

crates/torii/indexer/src/processors/store_update_record.rs (2)

5-5: New import added for tag generation.

Ohayo sensei! Added import of get_tag function from dojo_types::naming to support historical entity functionality.


117-126: Historical entity handling implemented correctly.

Nicely done! The implementation correctly determines whether the entity should be treated as historical based on the model tag, and passes this information to the database layer. This aligns well with the PR objective to add historical entity tracking.

crates/torii/sqlite/src/executor/mod.rs (4)

107-116: Good struct addition for entity queries!

Ohayo sensei! This new EntityQuery struct is well-designed and mirrors the existing EventMessageQuery structure, maintaining consistency in the codebase. The addition of the is_historical field enables the tracking of historical entities.


123-123: QueryType enum updated appropriately.

The SetEntity variant now uses the new EntityQuery type instead of just Ty, which provides the necessary context for historical tracking.


462-462: Model reference fixed.

Updated to use entity.ty.clone() which is more appropriate than using the entire entity.


470-508: Historical entity tracking logic well implemented!

Excellent implementation of historical entity tracking! The code follows the same pattern used for historical event messages, maintaining consistency throughout the codebase. The logic correctly:

  1. Queries the current historical counter
  2. Increments it for historical entities
  3. Stores historical data in the entities_historical table
  4. Updates the counter in the entity_model table

This approach provides a good foundation for querying historical entity states.

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: 0

🧹 Nitpick comments (1)
crates/torii/sqlite/src/executor/mod.rs (1)

107-522: Consider adding documentation for the historical entity feature

Ohayo sensei! While the implementation is solid, it would be helpful to add some comments explaining the purpose of the historical entity tracking system and how historical_counter is used. This would make it easier for future developers to understand the design intent.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 31bf2d0 and b0e0664.

📒 Files selected for processing (3)
  • crates/torii/migrations/20250310063604_historical_entities.sql (1 hunks)
  • crates/torii/sqlite/src/executor/mod.rs (2 hunks)
  • crates/torii/sqlite/src/lib.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/torii/migrations/20250310063604_historical_entities.sql
  • crates/torii/sqlite/src/lib.rs
🔇 Additional comments (6)
crates/torii/sqlite/src/executor/mod.rs (6)

107-116: Well-structured struct for entity queries!

Ohayo sensei! The new EntityQuery struct looks nicely organized with all the fields needed for entity operations, including the new is_historical flag. This struct will make tracking historical entity changes much cleaner.


123-123: Good refactoring of the QueryType enum!

The change from SetEntity(Ty) to SetEntity(EntityQuery) is a solid improvement, sensei! It encapsulates all entity-related information in a single structure, making the code more maintainable and the interface more consistent.


462-462: Using entity.ty.clone() instead of entity is more precise!

Nice optimization, sensei! You're now just using the type information rather than the entire entity object.


469-479: Historical entity counter retrieval looks good!

The implementation matches the pattern used for historical event messages. The query handles the case when no record exists by defaulting to 0, which is a good defensive approach.


480-510: Solid implementation of historical entity tracking!

The conditional logic for handling historical entities is well-structured. It:

  1. Increments the counter for historical entities
  2. Serializes entity data to JSON
  3. Properly handles both cases - with and without keys
  4. Uses parameterized queries to prevent SQL injection

Good attention to detail, sensei!


512-522: Proper update of entity_model with historical counter!

The upsert query is well-crafted with the ON CONFLICT clause to handle both insert and update scenarios. This ensures the historical counter is always properly maintained.

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.

1 participant