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-sql): granular model indices #3073

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

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Feb 28, 2025

Summary by CodeRabbit

  • New Features
    • Enhanced the command-line interface by adding SQL configuration options.
    • Users can now customize model indexing with flexible options for specifying models and fields.
    • Improved input validation ensures that indexing parameters are formatted correctly.
    • Introduced new structures for managing SQL-related configurations and model indices.
    • Added support for dynamic index creation based on model configurations.
    • New ModelIndices structure for defining model-specific fields.
    • New SqlConfig structure to enhance SQL configuration management.
    • Added a new public field for SQL options in the command-line arguments.
    • New method for instantiating SQL with configuration options.

Copy link

coderabbitai bot commented Feb 28, 2025

Ohayo sensei!

Walkthrough

The changes extend the Torii application’s command-line interface to support SQL-related options. A new public field is added to the main argument struct to include SQL configuration, and two new structs (ModelIndices and SqlOptions) are introduced to manage SQL model indexing options. A helper function for parsing model indices from CLI input is also provided. Existing functionality remains intact.

Changes

File(s) Change Summary
crates/torii/cli/.../args.rs Added a new public field sql: SqlOptions to the ToriiArgs struct with the #[command(flatten)] annotation for CLI argument parsing. Also added an optional sql field to ToriiArgsConfig.
crates/torii/cli/.../options.rs Introduced ModelIndices and SqlOptions structs. SqlOptions includes a boolean flag and an optional vector for model indices, a default implementation, and a helper function parse_model_indices to process model index inputs.
crates/torii/runner/.../lib.rs Added SqlConfig to imports and instantiated it in the run method of Runner, passing it to the Sql::new_with_config method.
crates/torii/sqlite/.../lib.rs Introduced SqlConfig struct, added it as a field in Sql, updated the constructor to accept this new config, and refactored add_columns_recursive into a method of Sql to utilize model indices.
crates/torii/sqlite/.../types.rs Added a new public struct ModelIndices with fields for model_tag and fields.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant CLI as CLI Parser
    participant A as ToriiArgs
    participant SO as SqlOptions Parser

    U->>CLI: Provide command-line input (--sql options)
    CLI->>A: Parse arguments (including flattened SQL options)
    A->>SO: Parse SQL-specific options via parse_model_indices
    SO-->>A: Return parsed SQL configuration
    A-->>CLI: Complete configuration with SQL options
Loading

Suggested reviewers

  • glihm

Possibly related PRs


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6c636f8 and 2a01d08.

📒 Files selected for processing (1)
  • crates/torii/cli/src/args.rs (10 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/torii/cli/src/args.rs

🪧 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 generate docstrings to generate docstrings for 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.

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 60e0a6b and 098ce9a.

📒 Files selected for processing (2)
  • crates/torii/cli/src/args.rs (1 hunks)
  • crates/torii/cli/src/options.rs (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: ci
crates/torii/cli/src/options.rs

[error] 385-385: Rust formatting check failed. The help message exceeds the line length limit.

🔇 Additional comments (3)
crates/torii/cli/src/options.rs (3)

375-379: Clean model indices structure with good field naming.

Ohayo sensei! The new ModelIndices struct looks well-designed for storing model tags and their associated fields. The field names are clear and self-explanatory.


405-409: Default implementation looks good.

The default implementation of SqlOptions sets sensible defaults - model_indices_keys to false (meaning all columns get indices by default) and model_indices to None.


411-423: Solid implementation of the model indices parser.

Nice implementation, sensei! The parser correctly handles the expected format and provides a clear error message when the format is invalid.

I particularly like that you're using the anyhow::Result type for error handling, which makes it easy to propagate errors with context.

Comment on lines +54 to +56
#[command(flatten)]
pub sql: SqlOptions,

Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

LGTM! Adding SQL options to the CLI interface.

Ohayo! This change looks good, adding a new sql field to ToriiArgs with the #[command(flatten)] attribute to expose SQL options in the CLI.

However, I noticed that while you've added this field to ToriiArgs, there's no corresponding update to ToriiArgsConfig or the methods that handle configuration file loading (with_config_file and the TryFrom<ToriiArgs> implementation).

Run the following script to examine the current pattern of including options in the configuration structure:


🏁 Script executed:

#!/bin/bash
# Check if other flattened options have config counterparts
rg -A 2 "\[command\(flatten\)\]" crates/torii/cli/src/args.rs
rg "Option<\w+Options>" crates/torii/cli/src/args.rs

Length of output: 744


Ohayo, Sensei!
Great job adding the new SQL options to the CLI interface using the flattened attribute. However, our verification shows that unlike the other options (e.g., indexing, events, erc, metrics, server, relay), the sql option lacks a corresponding counterpart in the configuration structure and the associated config file loading methods.

  • Please update ToriiArgsConfig to include an equivalent Option<SqlOptions> field.
  • Also, revise the configuration file loading methods (like with_config_file and the TryFrom<ToriiArgs> implementation) to properly process the new SQL options.

Comment on lines 381 to 403
#[derive(Debug, clap::Args, Clone, Serialize, Deserialize, PartialEq)]
#[command(next_help_heading = "SQL options")]
pub struct SqlOptions {
/// Whether model tables should default to having indices on all columns
#[arg(
long = "sql.model_indices_keys",
default_value_t = false,
help = "If true, creates indices on only key fields columns of model tables by default. If false, all model field columns will have indices."
)]
#[serde(default)]
pub model_indices_keys: bool,

/// Specify which fields should have indices for specific models
/// Format: "model_name:field1,field2;another_model:field3,field4"
#[arg(
long = "sql.model_indices",
value_delimiter = ';',
value_parser = parse_model_indices,
help = "Specify which fields should have indices for specific models. Format: \"model_name:field1,field2;another_model:field3,field4\""
)]
#[serde(default)]
pub model_indices: Option<Vec<ModelIndices>>,
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix the help message that's causing pipeline failure.

The help message for model_indices_keys exceeds the line length limit, causing a CI pipeline failure.

Please break the help message into multiple lines to fix the formatting error:

-        help = "If true, creates indices on only key fields columns of model tables by default. If false, all model field columns will have indices."
+        help = "If true, creates indices on only key fields columns of model tables by default. \
+                If false, all model field columns will have indices."
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[derive(Debug, clap::Args, Clone, Serialize, Deserialize, PartialEq)]
#[command(next_help_heading = "SQL options")]
pub struct SqlOptions {
/// Whether model tables should default to having indices on all columns
#[arg(
long = "sql.model_indices_keys",
default_value_t = false,
help = "If true, creates indices on only key fields columns of model tables by default. If false, all model field columns will have indices."
)]
#[serde(default)]
pub model_indices_keys: bool,
/// Specify which fields should have indices for specific models
/// Format: "model_name:field1,field2;another_model:field3,field4"
#[arg(
long = "sql.model_indices",
value_delimiter = ';',
value_parser = parse_model_indices,
help = "Specify which fields should have indices for specific models. Format: \"model_name:field1,field2;another_model:field3,field4\""
)]
#[serde(default)]
pub model_indices: Option<Vec<ModelIndices>>,
}
#[derive(Debug, clap::Args, Clone, Serialize, Deserialize, PartialEq)]
#[command(next_help_heading = "SQL options")]
pub struct SqlOptions {
/// Whether model tables should default to having indices on all columns
#[arg(
long = "sql.model_indices_keys",
default_value_t = false,
help = "If true, creates indices on only key fields columns of model tables by default. \
If false, all model field columns will have indices."
)]
#[serde(default)]
pub model_indices_keys: bool,
/// Specify which fields should have indices for specific models
/// Format: "model_name:field1,field2;another_model:field3,field4"
#[arg(
long = "sql.model_indices",
value_delimiter = ';',
value_parser = parse_model_indices,
help = "Specify which fields should have indices for specific models. Format: \"model_name:field1,field2;another_model:field3,field4\""
)]
#[serde(default)]
pub model_indices: Option<Vec<ModelIndices>>,
}
🧰 Tools
🪛 GitHub Actions: ci

[error] 385-385: Rust formatting check failed. The help message exceeds the line length limit.

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

♻️ Duplicate comments (1)
crates/torii/cli/src/options.rs (1)

375-397: ⚠️ Potential issue

Ohayo sensei, fix the long help message to pass formatting checks.

The pipeline is failing at line 379 for exceeding the Rust style guidelines. Consider splitting the help text across two lines:

-        help = "If true, creates indices on only key fields columns of model tables by default. If false, all model field columns will have indices."
+        help = "If true, creates indices on only key fields columns of model tables by default. \
+                If false, all model field columns will have indices."
🧰 Tools
🪛 GitHub Actions: ci

[error] 379-379: Rust formatting check failed. Please format the code according to Rust style guidelines.

🧹 Nitpick comments (4)
crates/torii/sqlite/src/types.rs (1)

229-234: Ohayo sensei, consider adding doc comments for the new struct.

A short explanation about its purpose and usage can improve maintainability and clarity for other developers.

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

42-45: Ohayo sensei, consider documenting SqlConfig.

Adding a brief doc comment could clarify the purpose of model_indices for new contributors.


85-91: Ohayo sensei, struct initialization looks neat.

Everything is consistent with the newly introduced config field. Consider a small doc comment for maintainability if needed.


828-1064: Ohayo sensei, consider splitting add_columns_recursive into smaller methods.

This function is quite large and can be broken down for clarity and reduced complexity.

🧰 Tools
🪛 GitHub Actions: ci

[error] 851-851: Rust formatting check failed. Please format the code according to Rust style guidelines.


[error] 867-867: Rust formatting check failed. Please format the code according to Rust style guidelines.


[error] 875-875: Rust formatting check failed. Please format the code according to Rust style guidelines.


[error] 974-974: Rust formatting check failed. Please format the code according to Rust style guidelines.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 098ce9a and fb0488f.

📒 Files selected for processing (4)
  • crates/torii/cli/src/options.rs (2 hunks)
  • crates/torii/runner/src/lib.rs (2 hunks)
  • crates/torii/sqlite/src/lib.rs (6 hunks)
  • crates/torii/sqlite/src/types.rs (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: ci
crates/torii/runner/src/lib.rs

[error] 153-153: Rust formatting check failed. Please format the code according to Rust style guidelines.

crates/torii/cli/src/options.rs

[error] 379-379: Rust formatting check failed. Please format the code according to Rust style guidelines.

crates/torii/sqlite/src/lib.rs

[error] 851-851: Rust formatting check failed. Please format the code according to Rust style guidelines.


[error] 867-867: Rust formatting check failed. Please format the code according to Rust style guidelines.


[error] 875-875: Rust formatting check failed. Please format the code according to Rust style guidelines.


[error] 974-974: Rust formatting check failed. Please format the code according to Rust style guidelines.

🔇 Additional comments (13)
crates/torii/runner/src/lib.rs (2)

41-41: Ohayo sensei, nice new import for SqlConfig.

It aligns with the other changes introducing SQL config options. No concerns here.


156-158: Ohayo sensei, verify the silent fallback logic.

Using unwrap_or_default() will silently fallback if model_indices is not provided in arguments. Confirm whether this is intended or if a warning or explicit error might be more appropriate.

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

9-9: Ohayo sensei, this import looks correct.

ModelIndices is indeed used later in this file, so this addition makes sense.


399-403: Ohayo sensei, the default implementation looks good.

This ensures consistent behavior whether or not the user provides any SQL options.


405-417: Ohayo sensei, confirm robust parsing.

Currently, the parser rejects anything outside the strict "model:field1,field2" format. Consider trimming whitespace or validating empty fields more gracefully.

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

24-24: Ohayo sensei, importing ModelIndices here is consistent.

No issues spotted.


53-53: Ohayo sensei, injecting config into Sql is a solid approach.

Centralizing SQL-related settings in a dedicated field improves code organization.


69-69: Ohayo sensei, good inclusion of config in the constructor parameters.

This ensures it's available throughout the lifecycle of the Sql instance.


784-792: Ohayo sensei, carefully verify the usage of add_columns_recursive.

You’re passing upgrade_diff to handle schema changes, so ensure the logic for partial model upgrades is tested thoroughly.


1066-1071: Ohayo sensei, asynchronous execution management is good here.

No issues to flag with the execute method’s approach.


1072-1076: Ohayo sensei, flush method is straightforward.

No concerns—just ensure it’s tested alongside high-concurrency scenarios.


1078-1082: Ohayo sensei, rollback logic looks fine.

Ensure any external references are gracefully handled after rollback.


1084-1109: Ohayo sensei, add_controller method is well-designed.

Updating or inserting the controller is consistent with the overall pattern used. No immediate concerns.

Copy link

codecov bot commented Feb 28, 2025

Codecov Report

Attention: Patch coverage is 64.12214% with 94 lines in your changes missing coverage. Please review.

Project coverage is 57.44%. Comparing base (60e0a6b) to head (2a01d08).

Files with missing lines Patch % Lines
crates/torii/sqlite/src/lib.rs 57.41% 89 Missing ⚠️
crates/torii/cli/src/options.rs 88.23% 2 Missing ⚠️
crates/torii/runner/src/lib.rs 0.00% 2 Missing ⚠️
crates/torii/cli/src/args.rs 97.05% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3073      +/-   ##
==========================================
+ Coverage   57.39%   57.44%   +0.04%     
==========================================
  Files         440      440              
  Lines       59963    60046      +83     
==========================================
+ Hits        34417    34491      +74     
- Misses      25546    25555       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 80e4ea6 and 6c636f8.

📒 Files selected for processing (1)
  • crates/torii/cli/src/args.rs (10 hunks)
🧰 Additional context used
🪛 GitHub Actions: ci
crates/torii/cli/src/args.rs

[error] 253-253: Rust formatting check failed. The code does not adhere to the expected formatting style.

🔇 Additional comments (7)
crates/torii/cli/src/args.rs (7)

54-56: Ohayo! Good job adding the SQL options to the CLI interface.

The sql field is properly added to the ToriiArgs struct with the #[command(flatten)] attribute, consistent with how other options are handled.


113-115: Well implemented SQL option merging, Sensei!

Your implementation correctly follows the established pattern for merging configuration values from files, checking if the SQL options are set to default values before using the config values.


146-146: Perfect addition to the config structure!

This addresses the previous review comment about adding a corresponding Option<SqlOptions> field to the ToriiArgsConfig struct.


178-178: Consistent handling of SQL options in config conversion!

The implementation properly follows the pattern used for other options, only including SQL options in the config if they differ from the default values.


199-199: Good import addition for ModelIndices.

Properly imported the ModelIndices type from the sqlite module for use in the tests.


221-224: Well-crafted tests for the CLI precedence!

The test correctly verifies that SQL options from command line take precedence over those in the config file, and that the model indices are correctly parsed.

Also applies to: 241-243, 256-264


297-300: Comprehensive config fallback test for SQL options!

The test properly validates that SQL options from the config file are correctly loaded when no command line options are provided.

Also applies to: 334-340

Comment on lines 253 to 255
assert_eq!(torii_args.events.historical, vec!["a-A".to_string()]);
assert_eq!(torii_args.server, ServerOptions::default());
assert!(torii_args.indexing.transactions);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix formatting issue flagged by the CI pipeline.

The CI pipeline reports a formatting issue on line 253. Please run cargo fmt to automatically format the code according to Rust's style guidelines.

🧰 Tools
🪛 GitHub Actions: ci

[error] 253-253: Rust formatting check failed. The code does not adhere to the expected formatting style.

@loothero
Copy link
Collaborator

Creating an index for every column is not a sensible default. If it were, SQlite would do this. Indexes have a trade off and at the extreme of every column has an index, that trade off is almost always -EV.

Default should be just the keys. I don't think we need to worry about migrating existing systems, it's fine if this change only applies to new torii instances.

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