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
fix(billing): creating a product no longer removes a shared feature from its other products - #2064
Creating a product (or a feature) that references an already-existing feature replaced the feature's stored product links with only the incoming product, silently unlinking the feature from every other product it belonged to. UpsertFeature now unions the incoming product IDs with the stored ones, so an upsert only ever adds links.
billing/product: UpsertFeature unions the existing and incoming ProductIDs (deduplicated) instead of replacing the stored set. The union lives in a small unionProductIDs helper. This covers both create paths that funnel through it: Service.Create (product create with features) and the CreateFeature API handler.
billing/plan: the final convergence loop in UpsertPlans now calls UpdateFeature instead of UpsertFeature (and UpdateFeature joins the ProductService interface). That loop deliberately converges the stored set to exactly what the plan file states, including removing the feature from products that no longer list it; UpdateFeature keeps those replace semantics.
billing/product tests: regression case for the union — a feature stored with products A+B, upserted with product C, ends up stored with A+B+C. Two more cases added in review follow-up: empty incoming IDs leave the stored links untouched, and overlapping IDs (stored [a,b], incoming [b,c]) merge to [a,b,c] with no duplicates.
Technical Details
Shrinking a feature's product set stays possible and deliberate: UpdateFeature (full replace) and RemoveFeatureFromProduct are unchanged. The BillingFeature reconciler is unaffected — it creates new features through CreateFeature without product IDs and routes updates through UpdateFeature.
Behavior note
CreateFeature on an existing name can no longer shrink the links. It only adds now. An upsert unions the stored product set with the incoming one, so creating a product or a feature never unlinks the feature from the other products it already belongs to. Shrinking the set is a deliberate act and goes through UpdateFeature or RemoveFeatureFromProduct.
Test Plan
New regression test fails on the old code (stored set would have been just the incoming product) and passes with the fix
go build ./... passes
go test ./billing/product/ ./billing/plan/ passes
Review follow-up: unionProductIDs extraction is behavior-identical; go vet clean; the two new cases fail on the old code and pass with the fix
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Bug Fixes
Adding a product association to a feature now preserves its existing product links, while adding only new links and avoiding duplicates.
Plan updates now save the final product associations for each feature, keeping those links consistent with the plan changes. Existing associations remain intact when no new product links are provided.
Feature upserts now preserve existing product links and add incoming links without duplicates. Plan reconciliation calls UpdateFeature to apply the final product IDs assigned to each feature.
raystack/frontier#2045: Defines the replace-set behavior of UpdateFeature used by plan reconciliation.
raystack/frontier#2046: Adds the BillingFeature reconcile kind and references the product-link behavior addressed here.
Suggested reviewers:kushsharma, rohanchkrabrty
Merge Risk: Low · up to cbd7c
Creating a product no longer removes a shared feature from its other products. Two concurrent upserts of the same feature can still lose a link, but that race existed before this change, so merging is low risk.
Pre-merge checks: 2
Check name
Status
Explanation
Linked Issues check
Issue #2047 requires that creating a product with an existing feature adds the new product ID to the feature's existing links. It also requires that CreateFeature on an existing name either fails or k…
Out of Scope Changes check
The whole-PR diff changes three files. billing/product/service.go changes UpsertFeature to union product IDs and adds the helper unionProductIDs. This is the core fix for #2047. billing/plan/service.g…
Full details: Linked Issues check
Explanation
Issue #2047 requires that creating a product with an existing feature adds the new product ID to the feature's existing links. It also requires that CreateFeature on an existing name either fails or keeps the existing links. The whole-PR diff (merge base to head) changes billing/product/service.go in UpsertFeature from existingFeature.ProductIDs = feature.ProductIDs to existingFeature.ProductIDs = unionProductIDs(existingFeature.ProductIDs, feature.ProductIDs). unionProductIDs calls utils.Deduplicate on the combined slice. Service.Create and the CreateFeature handler both reach UpsertFeature, so both paths keep stored links and add incoming IDs. This meets the first behavior and the keep-links option for CreateFeature. The BillingProduct reconcile path uses Service.Create, so it is covered too. The billing/plan/service.go diff switches the final UpsertPlans convergence loop from UpsertFeature to UpdateFeature and adds UpdateFeature to the ProductService interface. The issue states that this loop must keep the full-list rebuild, and UpdateFeature keeps replace semantics, so that behavior is preserved. The regression test in billing/product/service_test.go (per the summary) covers the union case. I did not run the tests. The verdict relies on the diff and the summary for test content. Minor note: append(stored, incoming...) can write into the stored slice's backing array. This does not affect the issue requirements.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff changes three files. billing/product/service.go changes UpsertFeature to union product IDs and adds the helper unionProductIDs. This is the core fix for #2047. billing/plan/service.go adds UpdateFeature to the ProductService interface and uses it in the UpsertPlans convergence loop. This keeps the replace behavior that #2047 says must remain for plan-file convergence. billing/product/service_test.go adds regression tests for the union, which support the fix. The comment-only and process-comment activity described in the PR objectives does not appear in the whole-PR diff, so it adds no out-of-scope change. The UpdateFeature and RemoveFeatureFromProduct functions are unchanged, as the PR description states. No unrelated changes were found.
Finishing Touches
Fix failing CI checks
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.
Reviewing files that changed from the base of the PR and between d3fa025 and cbd7cf4.
Files selected for processing: 2
billing/product/service.go
billing/product/service_test.go
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
Reviewing files that changed from the base of the PR and between 4e33f29 and 05d8731.
Files selected for processing: 3
billing/plan/service.go
billing/product/service.go
billing/product/service_test.go
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Major · Data Integrity & Integration · Heavy lift
UpsertFeature can lose product links under concurrent calls.
UpsertFeature reads the stored product IDs, adds the incoming IDs in memory, and then writes the full array with UpdateByName. The write does not depend on the earlier read, and no transaction or lock covers both steps. If two calls for the same feature run concurrently, the call that commits last overwrites the other call's added link. For example, both calls read [A], one adds B, the other adds C, and the final stored value is either [A,B] or [A,C]. The lost link is not reported as an error.
The union in this change does not remove this race. The earlier full-replacement write had the same race, so the change does not introduce it. The union also does not prevent it, because the merge still happens outside the write.
Make the link merge part of the persisted write. Use one statement that unions the stored product_ids with the incoming IDs, or serialize the read and write for each feature name. Keep UpdateByName replace semantics for UpdateFeature and RemoveFeatureFromProduct, which remove links on purpose.
Done in d3fa025 — removed the four-line comment above the UpdateFeature call in UpsertPlans. Comment-only change, no code touched. go build and go vet on ./billing/plan/... pass, gofmt clean.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix. The change does what it says, and I could not find anything outside the stated change.
What I checked
The diff touches only 3 files. There are no new deps, network calls, auth changes, or CI or build changes.
go build ./..., go vet and go test for billing/product, billing/plan and internal/api/v1beta1connect pass.
The new unit test fails on main and passes on this branch.
I ran real API calls on a local Frontier (Postgres, SpiceDB, stripe-mock), once with main (9c302eb) and once with this PR (05d8731). Feature audit_logs is shared by the products.
Step
main
this PR
Create basic with audit_logs
[basic]
[basic]
Create pro with audit_logs
[pro]
[basic,pro]
Link to both with UpdateFeature, then create team with audit_logs
[team]
[basic,pro,team]
CreateFeature audit_logs again with no product ids
[]
[basic,pro,team]
UpdateFeature with [basic,team]
[basic,team]
[basic,team]
UpdateProduct team with no features
[basic]
[basic]
CreatePlan with new product growth using audit_logs
[growth]
[growth]
So the #2047 case is fixed. Removing a feature from a product still works, through UpdateFeature and through UpdateProduct.
Still open
CreatePlan still drops the other links (last row). It was the same before this PR, so it is not a regression. Details are inline.
One behavior change worth noting in the PR description: CreateFeature on an existing name can no longer shrink the links. It only adds now.
A few inline notes on tests and a race that was already there.
The reason will be displayed to describe this comment to others. Learn more.
CreatePlan is the only caller of UpsertPlans. It passes a file with only the new plan's products. So this loop sets each feature's links to just those products.
In my local run, the links were [basic,pro,team]. Then I called CreatePlan with a new product growth that uses audit_logs. The links became [growth]. Main does the same, so this is not a regression. But it is the #2047 bug through another API.
The links also change when CreatePlan fails later. A plan with no price for its interval returned 500, and the feature had already moved to the new product.
The reason will be displayed to describe this comment to others. Learn more.
We can get rid of this comment too. It explains how we got to this change, which belongs in the PR description. A small helper like unionProductIDs would say the same thing in code.
Removed the process comment in billing/product/service.go and extracted the union into a small unionProductIDs helper, as suggested.
Added the two test cases: empty incoming IDs leave the stored links untouched, and stored [a,b] + incoming [b,c] merges to [a,b,c] with no duplicates. Both fail on the old code and pass with the fix.
Added the behavior note to the PR description: CreateFeature on an existing name can no longer shrink the links, it only adds now.
Verified: gofmt clean, go build ./..., go vet ./billing/product/... ./billing/plan/..., and go test ./billing/product/... ./billing/plan/... all pass.
On your CreatePlan question — whether the UpsertPlans convergence loop should union too: still considering it on my side, I'll answer that separately rather than fold it into this follow-up.
This branch has not been deployed
No deployments
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.
Summary
Creating a product (or a feature) that references an already-existing feature replaced the feature's stored product links with only the incoming product, silently unlinking the feature from every other product it belonged to.
UpsertFeaturenow unions the incoming product IDs with the stored ones, so an upsert only ever adds links.Fixes #2047
Changes
billing/product:UpsertFeatureunions the existing and incomingProductIDs(deduplicated) instead of replacing the stored set. The union lives in a smallunionProductIDshelper. This covers both create paths that funnel through it:Service.Create(product create with features) and theCreateFeatureAPI handler.billing/plan: the final convergence loop inUpsertPlansnow callsUpdateFeatureinstead ofUpsertFeature(andUpdateFeaturejoins theProductServiceinterface). That loop deliberately converges the stored set to exactly what the plan file states, including removing the feature from products that no longer list it;UpdateFeaturekeeps those replace semantics.billing/producttests: regression case for the union — a feature stored with products A+B, upserted with product C, ends up stored with A+B+C. Two more cases added in review follow-up: empty incoming IDs leave the stored links untouched, and overlapping IDs (stored [a,b], incoming [b,c]) merge to [a,b,c] with no duplicates.Technical Details
Shrinking a feature's product set stays possible and deliberate:
UpdateFeature(full replace) andRemoveFeatureFromProductare unchanged. The BillingFeature reconciler is unaffected — it creates new features throughCreateFeaturewithout product IDs and routes updates throughUpdateFeature.Behavior note
CreateFeature on an existing name can no longer shrink the links. It only adds now. An upsert unions the stored product set with the incoming one, so creating a product or a feature never unlinks the feature from the other products it already belongs to. Shrinking the set is a deliberate act and goes through
UpdateFeatureorRemoveFeatureFromProduct.Test Plan
go build ./...passesgo test ./billing/product/ ./billing/plan/passesunionProductIDsextraction is behavior-identical;go vetclean; the two new cases fail on the old code and pass with the fix