Skip to content

refactor(ledger): add compatible credit filter storage - #5172

Merged
GAlexIHU merged 3 commits into
mainfrom
codex/credit-filter-storage
Sep 21, 2026
Merged

GAlexIHU merged 3 commits into
mainfrom
codex/credit-filter-storage

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Introduces nullable, versioned JSON storage for credit filters on grants and ledger routes. Writers populate both representations; readers continue using the existing feature columns. Feature validation and normalization move into ledger/crediteligibility without changing their behavior.

Stack: this PR → #5173 → #5174. The follow-up drafts have not yet been rebased onto this revision. Merge and deploy in order. Deploy this release to every API instance and worker before applying step 2's backfill. Routing keys, feature matching, and the external API stay unchanged.

Validation: PostgreSQL account, credit-purchase, and legacy-lineage tests; JSON round-trip and unknown-restriction tests; scoped lint.

Legacy lineage storage and its existing feature reads and writes remain unchanged.

The persisted Filters type carries Version directly. Its JSON codec currently supports only v1, preserves the selected version through normalization and JSON round trips, and rejects missing or unsupported versions. Both new-row writers explicitly select v1.

RetriggerConfidence Score: 4/5

The migration and dual-write behavior appear operationally safe, but the repository-required predicate documentation correction remains blocking and the v1 codec should retain an explicit frozen payload.

Fix All in Claude CodeFindings

  1. P2 V1 format is not frozen ▶
  2. P2 Predicate comment has typo ▶
Fix with agent prompt
### Issue 1
openmeter/ledger/crediteligibility/storage.go:16-17
The v1 codec aliases the full `Filters` domain type for both encoding and decoding. If a future restriction field is added to `Filters`, it will automatically be written and accepted with `schema_version: 1`, while older v1 readers will reject it as unknown. This breaks mixed-version compatibility and the documented requirement that v1 reject unknown restriction fields. Keep a dedicated feature-only v1 payload so extending the domain type cannot silently change the v1 wire format.

### Issue 2
openmeter/ent/db/predicate/predicate.go:undefined-203
The new comment says the error is “not nit” rather than “not nil,” so it does not accurately describe the helper. The same typo appears on `LedgerSubAccountRouteOrErr` at line 400. This violates the repository requirement that comments and docstrings clearly explain behavior and must be corrected before merging.

```suggestion
// ChargeCreditPurchaseOrErr calls the predicate only if the error is not nil.
```

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR introduces nullable, versioned JSON storage for credit filters while retaining legacy feature columns for reads and dual writes during the staged rollout.

  • Adds JSONB filter columns for credit purchases and ledger sub-account routes.
  • Moves feature-filter validation and normalization into ledger/crediteligibility.
  • Adds version validation, JSON encoding, migration files, and storage bridge tests.
  • The latest revision directly aliases the extensible domain type as the v1 wire payload, weakening the intended frozen-version boundary.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  W[Credit purchase or route writer] --> N[Normalize feature filters]
  N --> J[Write versioned filters JSONB]
  N --> L[Write legacy feature column]
  R[Current readers] --> L
  F[Future staged readers] --> J
Loading

Reviews (6) · Last reviewed commit: "refactor(ledger): simplify filter JSON e..."

Summary by CodeRabbit

  • New Features

    • Added versioned credit eligibility filters for credit purchases and ledger routes.
    • Filters now support validation, normalization, and structured JSON serialization.
    • Unsupported versions, unknown fields, duplicates, and invalid feature values are rejected.
  • Compatibility

    • Existing feature-based data remains supported during the transition to structured filters.
    • Filter information is preserved when reading records created by older systems.
  • Documentation

    • Documented filter versioning, validation rules, storage behavior, and compatibility expectations.

@GAlexIHU GAlexIHU added the release-note/misc Miscellaneous changes label Sep 21, 2026
@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Repository: openmeterio/openmeter/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 4b5558bc-2616-47cc-bc19-3be78543cc43

📥 Commits

Reviewing files that changed from the base of the PR and between b5069d9 and 7a19a66.

⛔ Files ignored due to path filters (15)
  • openmeter/ent/db/chargecreditpurchase.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase/chargecreditpurchase.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ledgersubaccountroute.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ledgersubaccountroute/ledgersubaccountroute.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ledgersubaccountroute/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ledgersubaccountroute_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ledgersubaccountroute_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • openmeter/ent/db/mutation.go is excluded by !**/ent/db/**
  • openmeter/ent/db/predicate/predicate.go is excluded by !**/ent/db/**
  • openmeter/ent/db/runtime.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (12)
  • openmeter/billing/charges/creditpurchase/adapter/charge.go
  • openmeter/billing/charges/creditpurchase/featurefilters.go
  • openmeter/ent/schema/chargescreditpurchase.go
  • openmeter/ent/schema/ledger_account.go
  • openmeter/ledger/account/adapter/repo_test.go
  • openmeter/ledger/account/adapter/subaccount.go
  • openmeter/ledger/crediteligibility/README.md
  • openmeter/ledger/crediteligibility/filters.go
  • openmeter/ledger/crediteligibility/storage.go
  • openmeter/ledger/crediteligibility/storage_test.go
  • tools/migrate/migrations/20260921132542_credit_filter_storage.down.sql
  • tools/migrate/migrations/20260921132542_credit_filter_storage.up.sql
 _____________________________________________
< HD, 4K, 8K...I see bugs in all resolutions. >
 ---------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

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

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

@GAlexIHU
GAlexIHU force-pushed the codex/credit-filter-storage branch from 8304389 to ade3099 Compare September 21, 2026 14:39
// ChargeCreditPurchase is the predicate function for chargecreditpurchase builders.
type ChargeCreditPurchase func(*sql.Selector)

// ChargeCreditPurchaseOrErr calls the predicate only if the error is not nit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Predicate comment has typo

The new comment says the error is “not nit” rather than “not nil,” so it does not accurately describe the helper. The same typo appears on LedgerSubAccountRouteOrErr at line 400. This violates the repository requirement that comments and docstrings clearly explain behavior and must be corrected before merging.

Suggested change
// ChargeCreditPurchaseOrErr calls the predicate only if the error is not nit.
// ChargeCreditPurchaseOrErr calls the predicate only if the error is not nil.

Context Used: AGENTS.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: openmeter/ent/db/predicate/predicate.go
Line: 203

Comment:
**Predicate comment has typo**

The new comment says the error is “not nit” rather than “not nil,” so it does not accurately describe the helper. The same typo appears on `LedgerSubAccountRouteOrErr` at line 400. This violates the repository requirement that comments and docstrings clearly explain behavior and must be corrected before merging.

```suggestion
// ChargeCreditPurchaseOrErr calls the predicate only if the error is not nil.
```

**Context Used:** AGENTS.md ([source](https://github.com/openmeterio/openmeter/blob/main/AGENTS.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Comment on lines +16 to +17
type plain Filters
return json.Marshal(plain(filters))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 V1 format is not frozen

The v1 codec aliases the full Filters domain type for both encoding and decoding. If a future restriction field is added to Filters, it will automatically be written and accepted with schema_version: 1, while older v1 readers will reject it as unknown. This breaks mixed-version compatibility and the documented requirement that v1 reject unknown restriction fields. Keep a dedicated feature-only v1 payload so extending the domain type cannot silently change the v1 wire format.

Prompt To Fix With AI
This is a comment left during a code review.
Path: openmeter/ledger/crediteligibility/storage.go
Line: 16-17

Comment:
**V1 format is not frozen**

The v1 codec aliases the full `Filters` domain type for both encoding and decoding. If a future restriction field is added to `Filters`, it will automatically be written and accepted with `schema_version: 1`, while older v1 readers will reject it as unknown. This breaks mixed-version compatibility and the documented requirement that v1 reject unknown restriction fields. Keep a dedicated feature-only v1 payload so extending the domain type cannot silently change the v1 wire format.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

@GAlexIHU
GAlexIHU marked this pull request as ready for review September 21, 2026 15:16
@GAlexIHU
GAlexIHU requested a review from a team as a code owner September 21, 2026 15:16
@GAlexIHU
GAlexIHU added this pull request to stack #5175 September 21, 2026 15:22
@GAlexIHU
GAlexIHU merged commit 05f3299 into main Sep 21, 2026
29 of 30 checks passed
@GAlexIHU
GAlexIHU deleted the codex/credit-filter-storage branch September 21, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants