Skip to content

feat(customcurrency): materialize subscription item currencies - #4742

Merged
chrisgacsal merged 4 commits into
mainfrom
codex/custom-currency-subscriptions
Aug 6, 2026
Merged

chrisgacsal merged 4 commits into
mainfrom
codex/custom-currency-subscriptions

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • add nullable fiat and managed custom-currency references to subscription items
  • materialize the effective rate-card currency when subscription items are created or recreated
  • cover plan creation, updates, add-on application, cancellation, and direct sync paths
  • hydrate legacy rows with no stored currency without rejecting them
  • reject newly persisted priced items whose currency was not materialized

Rollout boundary

This is the expand-and-write breakpoint for subscription custom currencies.

  • the schema migration is additive and does not backfill existing subscription items
  • existing rows with no item currency remain readable
  • normal rewrites materialize those rows
  • Subscription.Currency keeps its current meaning
  • this PR does not introduce invoice-currency, customer-fiat, cost-basis, billing, or subscription-sync semantic changes

Deployed environments can backfill existing rows after this version is rolled out. The semantic change can then land separately.

Validation

  • make generate
  • make lint-go
  • POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/subscription/... -count=1
  • versioned migration up/down/up test, including legacy null-row preservation
  • full root test run: all packages passed except the expected test/notification failure because Svix was not running on 127.0.0.1:8071

Summary by CodeRabbit

  • New Features
    • Subscription items now retain effective fiat/custom currency details, including managed custom-currency references, through plan creation, add-on application, and lifecycle operations.
  • Bug Fixes
    • Legacy subscription items automatically regain missing currency metadata during update and cancellation.
    • Priced items with invalid or missing currency references are rejected with clearer errors.
  • Documentation
    • Updated invariants to describe currency snapshot behavior for newly materialized and legacy items.
  • Reliability
    • Workflow service now validates configuration at startup and fails fast with reported setup issues.

Greptile Summary

This PR materializes effective currencies on subscription items. The main changes are:

  • Adds fiat and managed custom-currency references to subscription items.
  • Resolves and stores currencies during create, update, add-on, cancellation, and sync flows.
  • Keeps legacy rows without stored currency readable.
  • Adds schema migrations, persistence checks, wiring, and lifecycle tests.

Confidence Score: 5/5

This looks safe to merge.

  • Cross-namespace managed currency writes are rejected at the repository boundary.
  • Create, update, and sync flows resolve currencies within the subscription namespace before persistence.
  • No blocking issue remains in the updated code.

Important Files Changed

Filename Overview
openmeter/subscription/repo/subscriptionitemrepo.go Validates resolved currency references and namespace ownership before persistence.
openmeter/subscription/repo/mapping.go Maps stored currency references and validates explicitly loaded custom-currency metadata.
openmeter/subscription/service/currency.go Materializes and resolves item currencies before subscription synchronization.
openmeter/ent/schema/subscription.go Adds subscription-item currency fields, relationships, indexes, and consistency checks.
tools/migrate/migrations/20260806122618_add_subscription_item_currency_constraints.up.sql Adds currency-reference constraints while preserving legacy rows with null currency data.

Reviews (8): Last reviewed commit: "fix: currency ns validation for susbcrip..." | Re-trigger Greptile

Context used (3)

  • Context used - CLAUDE.md (source)
  • Context used - AGENTS.md (source)
  • Context used - api/spec/AGENTS.md (source)

@coderabbitai

coderabbitai Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Subscription items now snapshot fiat or managed custom currency references. Subscription and add-on flows materialize currencies before persistence. Repositories and migrations enforce currency invariants. Workflow construction now validates dependencies and returns initialization errors.

Changes

Subscription currency support

Layer / File(s) Summary
Currency reference schema and migrations
openmeter/ent/schema/*, tools/migrate/migrations/*, tools/migrate/subscription_item_currency_references_test.go, openmeter/subscription/README.md
Subscription items store nullable currency codes and managed custom-currency IDs. Constraints, relationships, migrations, rollback checks, and documented invariants cover these references.
Currency identity materialization
openmeter/productcatalog/subscription/plan.go, openmeter/subscription/plan.go, openmeter/subscription/subscriptionspec.go, openmeter/subscription/currencyresolver/*, openmeter/subscription/service/*, openmeter/subscription/addon/diff/*, openmeter/subscription/workflow/service/*
Plans expose currencies.CurrencyReference. Subscription and add-on flows materialize and resolve effective currencies on priced rate cards.
Subscription item currency persistence
openmeter/subscription/repo/*, openmeter/subscription/service/currency_materialization_test.go
Repository creation validates and stores currency references. Mapping restores fiat and custom currency metadata. Tests cover persistence and legacy-item reconciliation.
Workflow constructor validation and wiring
openmeter/subscription/workflow/service/service.go, app/common/subscription.go, cmd/*/wire_gen.go, openmeter/subscription/testutils/service.go, test/*
NewWorkflowService validates seven required dependencies and returns errors. Application and test setup paths pass currencyResolver and handle constructor failures.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Plan
  participant SubscriptionService
  participant CurrencyResolver
  participant SubscriptionSpec
  participant SubscriptionItemRepo
  participant Database
  Plan->>SubscriptionService: Provide CurrencyReference
  SubscriptionService->>SubscriptionSpec: MaterializeRateCardCurrencies
  SubscriptionService->>CurrencyResolver: ResolveCurrenciesForSubscriptionSpec
  SubscriptionService->>SubscriptionItemRepo: Create materialized item
  SubscriptionItemRepo->>Database: Store currency and custom_currency_id
Loading

Possibly related PRs

Suggested labels: area/subscriptions

Suggested reviewers: chrisgacsal, turip

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 26.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: materializing subscription item currencies.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/custom-currency-subscriptions

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 added the release-note/feature Release note: Exciting New Features label Jul 17, 2026
@GAlexIHU
GAlexIHU marked this pull request as ready for review July 17, 2026 17:45
@GAlexIHU
GAlexIHU requested a review from a team as a code owner July 17, 2026 17:45

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openmeter/ent/schema/subscription.go`:
- Around line 273-277: Make custom-currency relationships tenant-scoped end to
end: update the subscription schema edge to use namespace and currency ID,
regenerate the migration and extend its database test to reject cross-namespace
currencies; update the mapping hydration path to reject namespace mismatches and
the subscription item save path to validate managed-currency ownership, adding
repository coverage for cross-namespace identities. Apply these changes at
openmeter/ent/schema/subscription.go:273-277,
tools/migrate/migrations/20260717150347_add_subscription_item_currency_references.up.sql:2,
tools/migrate/subscription_item_currency_references_test.go:133-149,
openmeter/subscription/repo/mapping.go:131-135,
openmeter/subscription/repo/subscriptionitemrepo.go:170-175, and
openmeter/subscription/repo/subscriptionitemrepo_test.go:65-129.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6b6f6390-f688-448a-9c63-065d54815580

📥 Commits

Reviewing files that changed from the base of the PR and between 6a58931 and 1230145.

⛔ Files ignored due to path filters (18)
  • openmeter/ent/db/client.go is excluded by !**/ent/db/**
  • openmeter/ent/db/customcurrency.go is excluded by !**/ent/db/**
  • openmeter/ent/db/customcurrency/customcurrency.go is excluded by !**/ent/db/**
  • openmeter/ent/db/customcurrency/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/customcurrency_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/customcurrency_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/customcurrency_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/runtime.go is excluded by !**/ent/db/**
  • openmeter/ent/db/setorclear.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem/subscriptionitem.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_update.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (25)
  • app/common/subscription.go
  • openmeter/ent/schema/custom_currencies.go
  • openmeter/ent/schema/subscription.go
  • openmeter/productcatalog/subscription/plan.go
  • openmeter/subscription/addon/diff/apply.go
  • openmeter/subscription/addon/diff/apply_test.go
  • openmeter/subscription/plan.go
  • openmeter/subscription/repo/mapping.go
  • openmeter/subscription/repo/subscriptionitemrepo.go
  • openmeter/subscription/repo/subscriptionitemrepo_test.go
  • openmeter/subscription/service/currency_materialization_test.go
  • openmeter/subscription/service/service.go
  • openmeter/subscription/service/sync.go
  • openmeter/subscription/subscriptionspec.go
  • openmeter/subscription/testutils/service.go
  • openmeter/subscription/workflow/service/addon_test.go
  • openmeter/subscription/workflow/service/service.go
  • openmeter/subscription/workflow/service/service_test.go
  • openmeter/subscription/workflow/service/subscription.go
  • openmeter/subscription/workflow/service/subscription_test.go
  • test/billing/subscription_suite.go
  • test/customer/testenv.go
  • tools/migrate/migrations/20260717150347_add_subscription_item_currency_references.down.sql
  • tools/migrate/migrations/20260717150347_add_subscription_item_currency_references.up.sql
  • tools/migrate/subscription_item_currency_references_test.go

Comment thread openmeter/ent/schema/subscription.go Resolved
@chrisgacsal
chrisgacsal force-pushed the codex/custom-currency-productcatalog-subscription branch 6 times, most recently from 5437aab to 14ba071 Compare July 29, 2026 22:45
Base automatically changed from codex/custom-currency-productcatalog-subscription to main July 30, 2026 08:53
@chrisgacsal
chrisgacsal force-pushed the codex/custom-currency-subscriptions branch from 1230145 to abd63d4 Compare July 30, 2026 12:44
@chrisgacsal
chrisgacsal force-pushed the codex/custom-currency-subscriptions branch from abd63d4 to 9e8ec70 Compare August 5, 2026 10:00
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@tools/migrate/migrations/20260805102531_add_subscription_item_currency_constraints.up.sql`:
- Line 2: Update the Ent schema constraint that defines subscription-item
currency validation so custom currency codes are verified against the currency
identified by custom_currency_id, rather than only checking that both are
present. Regenerate the migration through the project’s Atlas/migrate workflow
instead of editing the SQL directly, and add a regression case covering a valid
but mismatched custom code and ID that must be rejected.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c97c8139-4b55-4e3b-bc84-4333730925e1

📥 Commits

Reviewing files that changed from the base of the PR and between 9e8ec70 and d686bd9.

⛔ Files ignored due to path filters (10)
  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • openmeter/ent/db/mutation.go is excluded by !**/ent/db/**
  • openmeter/ent/db/runtime.go is excluded by !**/ent/db/**
  • openmeter/ent/db/setorclear.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem/subscriptionitem.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionitem_update.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (11)
  • openmeter/ent/schema/subscription.go
  • openmeter/subscription/README.md
  • openmeter/subscription/repo/mapping.go
  • openmeter/subscription/repo/subscriptionitemrepo.go
  • openmeter/subscription/repo/subscriptionitemrepo_test.go
  • openmeter/subscription/service/currency_materialization_test.go
  • tools/migrate/migrations/20260805102202_add_subscription_item_currency_references.down.sql
  • tools/migrate/migrations/20260805102202_add_subscription_item_currency_references.up.sql
  • tools/migrate/migrations/20260805102531_add_subscription_item_currency_constraints.down.sql
  • tools/migrate/migrations/20260805102531_add_subscription_item_currency_constraints.up.sql
  • tools/migrate/subscription_item_currency_references_test.go
🚧 Files skipped from review as they are similar to previous changes (5)
  • openmeter/subscription/repo/subscriptionitemrepo_test.go
  • openmeter/subscription/repo/subscriptionitemrepo.go
  • openmeter/ent/schema/subscription.go
  • openmeter/subscription/service/currency_materialization_test.go
  • openmeter/subscription/repo/mapping.go

Comment thread openmeter/subscription/repo/mapping.go Resolved

@GAlexIHU GAlexIHU left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

✅

@chrisgacsal
chrisgacsal force-pushed the codex/custom-currency-subscriptions branch from 84a04d8 to 8f0d177 Compare August 6, 2026 12:08
Comment thread openmeter/subscription/repo/mapping.go Resolved
@chrisgacsal
chrisgacsal force-pushed the codex/custom-currency-subscriptions branch from 8f0d177 to 5d2efe2 Compare August 6, 2026 12:42
@chrisgacsal
chrisgacsal merged commit b371e31 into main Aug 6, 2026
27 checks passed
@chrisgacsal
chrisgacsal deleted the codex/custom-currency-subscriptions branch August 6, 2026 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants