Skip to content

Conversation

@gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Nov 6, 2025

  • feat: add utils function to centralize the creation of select queries
  • feat: optimize queries on ledgers alone in their buckets

@gfyrag gfyrag requested a review from a team as a code owner November 6, 2025 11:05
@coderabbitai
Copy link

coderabbitai bot commented Nov 6, 2025

Walkthrough

A refactor introducing a new scoped query helper method (newScopedSelect()) that constructs ledger-filtered queries with optimized PostgreSQL WHERE logic, replacing direct database selects and explicit ledger filtering across four resource query builders.

Changes

Cohort / File(s) Summary
Store helper and initialization
internal/storage/ledger/store.go
Adds non-exported newScopedSelect() method that constructs ledger-scoped queries using a two-case WHERE clause to avoid suboptimal Postgres plans. Updates New() constructor to initialize ledger and bucket fields in the Store object.
Resource query builders
internal/storage/ledger/resource_accounts.go, resource_aggregated_balances.go, resource_logs.go, resource_transactions.go, resource_volumes.go
Unified refactor across all resource query builders: replaces h.store.db.NewSelect() with h.store.newScopedSelect() and removes explicit ledger filtering clauses (Where("ledger = ?", ...)). resource_volumes.go additionally adds account-related column aliasing and propagation when address segments are needed.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring extra attention:

  • internal/storage/ledger/resource_accounts.go — Multiple query construction paths affected; verify balance resolution and rows generation logic remains correct after removing explicit ledger filtering
  • internal/storage/ledger/resource_volumes.go — New account column aliasing logic (accounts_addressaccount_array) must be validated for correctness in address segment scenarios
  • Confirm that the new scoped select method's two-case WHERE clause produces equivalent filtering semantics to the removed explicit ledger predicates across all call sites

Poem

🐰 Our queries now dance with ledger grace,
No ledger clauses cluttering the place,
The scoped select hopping through Postgres right,
Optimized plans shining ever bright! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: conditional where on selections' accurately reflects the main change: introducing conditional WHERE clauses in query selections to optimize ledger filtering based on bucket scope.
Description check ✅ Passed The description clearly relates to the changeset, describing two key improvements: a utils function for centralizing select query creation and query optimization for ledgers in their buckets.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/conditional-where-on-selections

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 4dbfd29 and c7d8d31.

📒 Files selected for processing (6)
  • internal/storage/ledger/resource_accounts.go (4 hunks)
  • internal/storage/ledger/resource_aggregated_balances.go (3 hunks)
  • internal/storage/ledger/resource_logs.go (1 hunks)
  • internal/storage/ledger/resource_transactions.go (4 hunks)
  • internal/storage/ledger/resource_volumes.go (4 hunks)
  • internal/storage/ledger/store.go (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: gfyrag
Repo: formancehq/ledger PR: 935
File: internal/controller/system/state_tracker.go:0-0
Timestamp: 2025-05-20T13:48:07.455Z
Learning: In the Formance ledger codebase, sequence reset queries with `select setval` don't require COALESCE around max(id) even for brand new ledgers, as the system handles this case properly.

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Comment @coderabbitai help to get the list of available commands and usage tips.

Base automatically changed from chore/upgrade-go-libs to main November 6, 2025 11:08
flemzord
flemzord previously approved these changes Nov 6, 2025
@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.97%. Comparing base (63cd3d8) to head (c7d8d31).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1100      +/-   ##
==========================================
- Coverage   81.98%   81.97%   -0.01%     
==========================================
  Files         186      186              
  Lines        8942     8938       -4     
==========================================
- Hits         7331     7327       -4     
- Misses       1187     1188       +1     
+ Partials      424      423       -1     

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

@gfyrag gfyrag force-pushed the feat/conditional-where-on-selections branch from 074728f to c7d8d31 Compare November 6, 2025 11:51
@gfyrag gfyrag enabled auto-merge November 6, 2025 12:45
@gfyrag gfyrag added this pull request to the merge queue Nov 6, 2025
Merged via the queue into main with commit f38748e Nov 6, 2025
9 checks passed
@gfyrag gfyrag deleted the feat/conditional-where-on-selections branch November 6, 2025 13:30
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