8000 refactor: introduce entity schemas by gfyrag · Pull Request #973 · formancehq/ledger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: introduce entity schemas #973

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

Merged
merged 2 commits into from
Jun 17, 2025
Merged

Conversation

gfyrag
Copy link
Contributor
@gfyrag gfyrag commented Jun 16, 2025

No description provided.

@gfyrag gfyrag requested a review from a team as a code owner June 16, 2025 12:01
Copy link
coderabbitai bot commented Jun 16, 2025

Walkthrough

This change refactors resource filtering and schema validation by replacing the previous filter-based system with a structured schema approach. It introduces a new EntitySchema and FieldType abstraction, updates repository handlers to use Schema() instead of Filters(), and adapts mocks and method signatures accordingly. Validation logic is now encapsulated within field types.

Changes

Files/Groups Change Summary
internal/storage/common/resource.go Replaces Filter struct and Filters() method with EntitySchema and FieldType interface; adds new field types and validation logic; updates operator handling.
internal/controller/ledger/mocks_test.go Removes mocks for Filters(), adds mocks for new Schema() method and new FieldType interface.
internal/storage/ledger/resource_accounts.go
internal/storage/ledger/resource_aggregated_balances.go
internal/storage/ledger/resource_logs.go
internal/storage/ledger/resource_transactions.go
internal/storage/ledger/resource_volumes.go
internal/storage/system/resource_ledgers.go
Replace Filters() with Schema() in all resource handlers; update method signatures to remove unused parameters.
internal/storage/ledger/store.go Re 8000 moves the now-obsolete validateAddressFilter function.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RepositoryHandler
    participant EntitySchema
    participant FieldType

    Client->>RepositoryHandler: Call Schema()
    RepositoryHandler->>EntitySchema: Return schema with Fields
    loop For each filter in request
        Client->>EntitySchema: Lookup Field by name
        EntitySchema->>FieldType: Validate operator and value
        FieldType-->>EntitySchema: Validation result
        EntitySchema-->>Client: Accept or reject filter
    end
Loading

Possibly related PRs

  • feat: simplify health check #750: Updates mocks for Schema() and FieldType interfaces, which directly correspond to the interface and type changes introduced in this PR.

Suggested labels

build-images

Suggested reviewers

  • ascandone
  • flemzord

Poem

A schema now guides what fields should be,
With types and rules for all to see.
Filters retired, new logic in tow,
Validation's precise, as schemas now show.
Mocks are refreshed, old code swept away—
Hopping forward, the rabbit’s way! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch refacto/introduce-entity-schema
  • Post Copyable Unit Tests in Comment

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 auto-generate unit tests to generate unit tests 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
codecov bot commented Jun 16, 2025

Codecov Report

Attention: Patch coverage is 89.78495% with 19 lines in your changes missing coverage. Please review.

Project coverage is 82.96%. Comparing base (b36d454) to head (0889870).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/storage/common/resource.go 86.36% 13 Missing and 5 partials ⚠️
internal/storage/system/resource_ledgers.go 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #973      +/-   ##
==========================================
+ Coverage   82.86%   82.96%   +0.09%     
==========================================
  Files         142      142              
  Lines        8050     8089      +39     
==========================================
+ Hits         6671     6711      +40     
- Misses       1054     1059       +5     
+ Partials      325      319       -6     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

8000
@gfyrag gfyrag force-pushed the refacto/introduce-entity-schema branch 5 times, most recently from 5280d0c to 7d4b5b6 Compare June 16, 2025 13:39
Base automatically changed from fix/transactions-paging-using-effective-order to main June 17, 2025 09:50
if property.Type.IsIndexable() {
key = strings.Split(key, "[")[0]
}
match := func() bool {
Copy link

Choose a reason for hiding this comment

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

Since this is in a for loop it might be nice to have this inline function defined somewhere else. It can be harder to track the scope of returns and breaks when there are nested for loops and functions, so moving the function definition outside would improve readability / maintainability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a function matchKey to the Field structure.
Is it better?

@gfyrag gfyrag force-pushed the refacto/introduce-entity-schema branch from 0c36ccb to 0889870 Compare June 17, 2025 09:57
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: 3

🧹 Nitpick comments (10)
internal/storage/ledger/resource_logs.go (1)

14-21: Schema looks good but consider exposing aliases

The minimal schema (date, id) is correct, yet callers often use "timestamp" or "seq" for these columns elsewhere in the code-base. If you expect those keys to be accepted by the automatic validator, add WithAliases() now to avoid scattered manual alias handling later.

internal/storage/ledger/resource_accounts.go (2)

16-25: Schema is fine but could mirror balance:<asset> pattern

balance is defined as a numeric map – great.
For asset-scoped keys (balance:USD) the filter implementation relies on a regexp, not on the schema.
If you foresee more typed map fields, consider extending FieldTypeMap to expose those sub-keys, so this handler does not need its own regex later.


28-29: Chained builder reads better – nitpick on variable naming

ret := ... shadows the package-wide ret convention used elsewhere. qry or q would remove any doubt that this is a local builder.

internal/storage/ledger/resource_aggregated_balances.go (1)

112-129: Operator argument ignored – document intent

ResolveFilter now discards the operator parameter (_).
For maintainers it would help to add a short comment explaining that only equality semantics are supported here; this prevents future confusion when someone tries address[neq].

internal/storage/ledger/resource_transactions.go (3)

15-27: Comprehensive schema – one potential alias

Consider adding WithAliases("txid") on id – that name crops up in API requests and would plug straight into the new validator layer.


84-114: Filter logic mostly unchanged – minor bool/operator mismatch

For reverted, the operator argument is silently ignored; only the Boolean value is honoured.
Example: reverted[neq]=true will still be treated as equality.
Either document this special-case or reject any operator except eq to avoid surprising clients.


120-151: Expand query is heavy – cache or pre-compute?

The effectiveVolumes expansion nests two DISTINCT ON windows plus an aggregation. On large move tables it will be expensive.
Investigate whether a materialised view or a temporary cache keyed by the PIT could off-load this work.

internal/storage/system/resource_ledgers.go (2)

30-34: Consider exploiting incoming filters for early push-down

BuildDataset ignores the RepositoryHandlerBuildContext; leveraging ctx.UseFilter("bucket"), etc., to add early WHERE clauses would reduce dataset size and improve performance.


56-57: LIKE should probably be ILIKE for case-insensitive matches

If user queries are meant to be case-insensitive, replace "like" with "ilike" (or parameterise) to avoid surprising results.

internal/storage/common/resource.go (1)

101-126: Minor readability: avoid shadowing key

Redeclaring key := key inside the loop and mutating it later is confusing. Use a distinct variable (lookupKey e.g.) to improve 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 b36d454 and 0889870.

📒 Files selected for processing (9)
  • internal/controller/ledger/mocks_test.go (2 hunks)
  • internal/storage/common/resource.go (5 hunks)
  • internal/storage/ledger/resource_accounts.go (2 hunks)
  • internal/storage/ledger/resource_aggregated_balances.go (2 hunks)
  • internal/storage/ledger/resource_logs.go (2 hunks)
  • internal/storage/ledger/resource_transactions.go (3 hunks)
  • internal/storage/ledger/resource_volumes.go (1 hunks)
  • internal/storage/ledger/store.go (0 hunks)
  • internal/storage/system/resource_ledgers.go (2 hunks)
💤 Files with no reviewable changes (1)
  • internal/storage/ledger/store.go
🧰 Additional context used
🧬 Code Graph Analysis (7)
internal/storage/ledger/resource_logs.go (1)
internal/storage/common/resource.go (5)
  • EntitySchema (59-61)
  • Field (426-429)
  • NewDateField (462-464)
  • NewNumericField (472-474)
  • ResourceQuery (343-349)
internal/storage/ledger/resource_accounts.go (1)
internal/storage/common/resource.go (8)
  • EntitySchema (59-61)
  • Field (426-429)
  • NewStringField (457-459)
  • NewDateField (462-464)
  • NewNumericMapField (487-489)
  • NewStringMapField (482-484)
  • RepositoryHandlerBuildContext (63-66)
  • ResourceQuery (343-349)
internal/storage/ledger/resource_volumes.go (1)
internal/storage/common/resource.go (6)
  • EntitySchema (59-61)
  • Field (426-429)
  • NewStringField (457-459)
  • NewNumericMapField (487-489)
  • NewDateField (462-464)
  • NewStringMapField (482-484)
internal/storage/ledger/resource_aggregated_balances.go (2)
internal/storage/common/resource.go (5)
  • EntitySchema (59-61)
  • Field (426-429)
  • NewStringField (457-459)
  • NewStringMapField (482-484)
  • ResourceQuery (343-349)
internal/controller/ledger/store.go (1)
  • GetAggregatedVolumesOptions (141-143)
internal/storage/system/resource_ledgers.go (1)
internal/storage/common/resource.go (7)
  • EntitySchema (59-61)
  • Field (426-429)
  • NewStringField (457-459)
  • NewStringMapField (482-484)
  • RepositoryHandlerBuildContext (63-66)
  • ResourceQuery (343-349)
  • JoinCondition (36-39)
internal/storage/ledger/resource_transactions.go (1)
internal/storage/common/resource.go (9)
  • EntitySchema (59-61)
  • Field (426-429)
  • NewBooleanField (477-479)
  • NewStringField (457-459)
  • NewDateField (462-464)
  • NewStringMapField (482-484)
  • NewNumericField (472-474)
  • ResourceQuery (343-349)
  • JoinCondition (36-39)
internal/controller/ledger/mocks_test.go (1)
internal/storage/common/resource.go (1)
  • EntitySchema (59-61)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Tests
  • GitHub Check: Dirty
🔇 Additional comments (7)
internal/storage/ledger/resource_logs.go (1)

43-45: Project helper – LGTM

The refactor to ignore the now-unused query argument is clean and self-explanatory.

internal/storage/ledger/resource_volumes.go (1)

17-27: Nice, alias “account” is baked in

Good use of WithAliases("account"); the schema is well-aligned with the existing manual filter logic. No further remarks.

internal/storage/ledger/resource_accounts.go (1)

108-110: Project – no issues

The projection remains a simple *, consistent with earlier behaviour.

internal/storage/ledger/resource_aggregated_balances.go (1)

15-22: Lean schema : OK

Only address and metadata are required for aggregated balances; definition is concise and correct.

internal/storage/ledger/resource_transactions.go (1)

116-118: Project – no change, fine

internal/controller/ledger/mocks_test.go (1)

144-156: Generated mocks look fine – skipping detailed review

No issues spotted in the added Schema() stub or the new MockFieldType; both follow gomock conventions.

Also applies to: 281-345

internal/storage/system/resource_ledgers.go (1)

19-28: Schema definition is clear and consistent

Fields and types align with downstream filter logic. LGTM.

Comment on lines +112 to +118
if !slices.Contains(property.Type.Operators(), operator) {
return NewErrInvalidQuery("operator '%s' is not allowed for property '%s'", operator, name)
}
ret[property.Name] = value
break
}

if !found {
return NewErrInvalidQuery("unknown key '%s' when building query", key)
if err := property.Type.ValidateValue(value); err != nil {
return NewErrInvalidQuery("invalid value '%v' for property '%s': %s", value, name, err)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

$exists value is still validated against underlying type

For map fields, ValidateValue delegates to the underlying type and will reject typical $exists payloads (true, false, or nil). Either:

  1. Skip value-validation in validateFilters when operator == OperatorExists, or
  2. Make TypeMap.ValidateValue accept boolean / nil.

Current behaviour makes legitimate $exists queries fail.

🤖 Prompt for AI Agents
In internal/storage/common/resource.go around lines 112 to 118, the code
validates the value for all operators including the $exists operator, which
causes legitimate $exists queries to fail because the underlying type validation
rejects boolean or nil values. To fix this, modify the validateFilters function
to skip the value validation step when the operator is OperatorExists, ensuring
$exists queries are not incorrectly rejected.

Comment on lines +20 to 33
case OperatorMatch:
return "="
case "$lt":
case OperatorLT:
return "<"
case "$gt":
case OperatorGT:
return ">"
case "$lte":
case OperatorLTE:
return "<="
case "$gte":
case OperatorGTE:
return ">="
case "$like":
case OperatorLike:
return "like"
}
panic("unreachable")
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

$exists not handled – call will panic

ConvertOperatorToSQL omits OperatorExists. If a caller mistakenly routes $exists through this helper, execution reaches panic("unreachable").

+ case OperatorExists:
+     return "is not null"

Or guard callers to never invoke this helper with $exists.

📝 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
case OperatorMatch:
return "="
case "$lt":
case OperatorLT:
return "<"
case "$gt":
case OperatorGT:
return ">"
case "$lte":
case OperatorLTE:
return "<="
case "$gte":
case OperatorGTE:
return ">="
case "$like":
case OperatorLike:
return "like"
}
panic("unreachable")
case OperatorMatch:
return "="
case OperatorLT:
return "<"
case OperatorGT:
return ">"
case OperatorLTE:
return "<="
case OperatorGTE:
return ">="
case OperatorLike:
return "like"
case OperatorExists:
return "is not null"
}
panic("unreachable")
🤖 Prompt for AI Agents
In internal/storage/common/resource.go around lines 20 to 33, the
ConvertOperatorToSQL function does not handle the OperatorExists case, causing a
panic if $exists is passed. To fix this, add a case for OperatorExists in the
switch statement and return the appropriate SQL representation or handle it
safely. Alternatively, ensure callers never pass OperatorExists to this function
by adding validation before calling it.

Comment on lines +566 to +568
func (t TypeMap) ValidateValue(value any) error {
return t.underlyingType.ValidateValue(value)
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Allow boolean / nil in TypeMap.ValidateValue

func (t TypeMap) ValidateValue(value any) error {
-    return t.underlyingType.ValidateValue(value)
+    // `$exists` usually carries a boolean or is nil
+    if value == nil {
+        return nil
+    }
+    if _, ok := value.(bool); ok {
+        return nil
+    }
+    return t.underlyingType.ValidateValue(value)
}

Prevents false negatives on $exists filters without touching other operators.

📝 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
func (t TypeMap) ValidateValue(value any) error {
return t.underlyingType.ValidateValue(value)
}
func (t TypeMap) ValidateValue(value any) error {
// `$exists` usually carries a boolean or is nil
if value == nil {
return nil
}
if _, ok := value.(bool); ok {
return nil
}
return t.underlyingType.ValidateValue(value)
}
🤖 Prompt for AI Agents
In internal/storage/common/resource.go around lines 566 to 568, the
ValidateValue method on TypeMap currently calls underlyingType.ValidateValue
directly, which does not allow boolean or nil values. Modify the method to
explicitly allow boolean and nil values by returning nil (no error) when the
value is a boolean or nil, and otherwise call underlyingType.ValidateValue. This
change prevents false negatives on $exists filters while preserving validation
for other operators.

@gfyrag gfyrag added this pull request to the merge queue Jun 17, 2025
Merged via the queue into main with commit 539800c Jun 17, 2025
10 checks passed
@gfyrag gfyrag deleted the refacto/introduce-entity-schema branch June 17, 2025 10:13
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
0