You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize unpaginated queries and subscription charge lookups - #5501
Unpaginated Ent listings currently count every matching row before fetching those same rows. For Page{}, fetch once and derive TotalCount from the returned items; explicit pages retain the existing count-based totals and empty-result behavior.
Add (namespace, subscription_id) indexes to the credit-purchase, flat-fee, and usage-based charge tables so subscription charge lookups can avoid scanning the namespace.
Notes for reviewer
Each index has its own migration, using CREATE INDEX CONCURRENTLY and DROP INDEX CONCURRENTLY for rollback.
The shared pagination template is the source of the generated client changes. Charge filtering, deletion semantics, ordering, and page metadata are preserved.
The migration runner applies, rolls back, and reapplies the three indexes successfully. View parity and Atlas schema parity checks passed.
Scoped make lint-go-fast passed for the affected packages.
Summary by CodeRabbit
Improvements
Unpaginated results now return all matching items and derive the total count from those results, without a separate count query.
Paginated results retain accurate total counts, including on partial and empty pages.
Subscription-related charge lookups are optimized.
Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.
Summary
Unpaginated Ent queries now fetch rows once and set TotalCount from the results. Explicit pages keep the existing count-first behavior.
Unpaginated Ent queries fetch matching rows once and count them from the results.
Subscription lookups can use namespace-and-subscription indexes on all three charge tables.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Paginate(ctx, page)"] --> B["Clear prior limit and offset"]
B --> C{"page.IsZero()"}
C -->|Yes| D["Fetch all matching items"]
D --> E["TotalCount = len(items)"]
C -->|No| F["Count matching rows"]
F --> G{"Count is zero?"}
G -->|Yes| H["Return empty items"]
G -->|No| I["Fetch requested page"]
No actionable comments were generated in the recent review.
Walkthrough
Zero-value pagination now loads matching items without a separate count query. Charge pagination tests cover ordering, pages, totals, and SQL query counts. The charge schema and migrations add subscription lookup indexes to three charge tables.
Both pagination implementations load all matching items for a zero-value page and derive the total count from the results.
Pagination query and result coverage pkg/framework/entutils/entpaginate/paginate_test.go, openmeter/billing/charges/adapter/search_test.go
Tests check paged and unpaged results, query counts, filtering, ordering, empty results, and canceled-context errors.
Charge subscription lookup indexes
Layer / File(s)
Summary
Charge subscription indexes and migrations openmeter/ent/schema/charges.go, tools/migrate/migrations/20261112190449_charge_credit_purchase_subscription_lookup.*.sql, tools/migrate/migrations/20261112190450_charge_flat_fee_subscription_lookup.*.sql, tools/migrate/migrations/20261112190451_charge_usage_based_subscription_lookup.*.sql
The schema and migrations add indexes on namespace and subscription_id for credit purchases, flat fees, and usage-based charges. The migrations create and drop the indexes concurrently.
Unpaginated listings now skip the count query, and subscription lookups on three charge tables gain indexes. No actionable merge-blocking risk was found in the supplied evidence.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names two changes that the changeset contains: optimizing unpaginated Ent queries (the paginate template, generated paginate.go, and tests) and adding subscription-related charge indexes (th…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names two changes that the changeset contains: optimizing unpaginated Ent queries (the paginate template, generated paginate.go, and tests) and adding subscription-related charge indexes (the schema mixin and the three charge table migrations). It is concise and specific enough for a teammate scanning history. Its wording ("subscription charge lookups") fits the new (namespace, subscription_id) indexes.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Unpaginated Ent listings currently count every matching row before fetching those same rows. For
Page{}, fetch once and deriveTotalCountfrom the returned items; explicit pages retain the existing count-based totals and empty-result behavior.Add
(namespace, subscription_id)indexes to the credit-purchase, flat-fee, and usage-based charge tables so subscription charge lookups can avoid scanning the namespace.Notes for reviewer
CREATE INDEX CONCURRENTLYandDROP INDEX CONCURRENTLYfor rollback.make lint-go-fastpassed for the affected packages.Summary by CodeRabbit


Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.Summary
Unpaginated Ent queries now fetch rows once and set
TotalCountfrom the results. Explicit pages keep the existing count-first behavior.Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD A["Paginate(ctx, page)"] --> B["Clear prior limit and offset"] B --> C{"page.IsZero()"} C -->|Yes| D["Fetch all matching items"] D --> E["TotalCount = len(items)"] C -->|No| F["Count matching rows"] F --> G{"Count is zero?"} G -->|Yes| H["Return empty items"] G -->|No| I["Fetch requested page"]Reviews (2) · Last reviewed commit: "test: remove charge subscription migrati..." · Reviewed by Greptile