Skip to content

Optimize unpaginated queries and subscription charge lookups - #5501

Merged
turip merged 2 commits into
mainfrom
feat/subscription-charge-lookup-queries
Nov 12, 2026
Merged

turip merged 2 commits into
mainfrom
feat/subscription-charge-lookup-queries

Conversation

@turip

@turip turip commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Overview

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.
  • PostgreSQL tests cover pagination query counts, filters/order, clearing prior limits/offsets, empty results, fetch errors, and charge listings. Targeted subscription-sync provisioning/reconciliation and override lifecycle regressions passed.
  • 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.

RetriggerView in GreptileConfidence 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"]
Loading

Reviews (2) · Last reviewed commit: "test: remove charge subscription migrati..." · Reviewed by Greptile

@turip
turip requested a review from a team as a code owner November 12, 2026 19:28
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Changes: Pagination behavior

Layer / File(s) Summary
Zero-value page handling
pkg/framework/entutils/entpaginate/paginate.tpl, pkg/framework/entutils/testutils/ent1/db/paginate.go
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.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Suggested reviewers: galexihu, borosr

Merge Risk: Minimal · up to d22a5

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 5bc6c56 and d22a537.

Files ignored due to path filters: 3

  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • openmeter/ent/db/paginate.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing: 11

  • openmeter/billing/charges/adapter/search_test.go
  • openmeter/ent/schema/charges.go
  • pkg/framework/entutils/entpaginate/paginate.tpl
  • pkg/framework/entutils/entpaginate/paginate_test.go
  • pkg/framework/entutils/testutils/ent1/db/paginate.go
  • tools/migrate/migrations/20261112190449_charge_credit_purchase_subscription_lookup.down.sql
  • tools/migrate/migrations/20261112190449_charge_credit_purchase_subscription_lookup.up.sql
  • tools/migrate/migrations/20261112190450_charge_flat_fee_subscription_lookup.down.sql
  • tools/migrate/migrations/20261112190450_charge_flat_fee_subscription_lookup.up.sql
  • tools/migrate/migrations/20261112190451_charge_usage_based_subscription_lookup.down.sql
  • tools/migrate/migrations/20261112190451_charge_usage_based_subscription_lookup.up.sql

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@turip turip added the release-note/misc Miscellaneous changes label Nov 12, 2026
@turip turip added release-note/bug-fix Release note: Bug Fixes and removed release-note/misc Miscellaneous changes labels Nov 12, 2026
@turip
turip merged commit 843fa7d into main Nov 12, 2026
32 of 38 checks passed
@turip
turip deleted the feat/subscription-charge-lookup-queries branch November 12, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants