Skip to content

fix(billing): creating a product no longer removes a shared feature from its other products - #2064

Open
0xABCD01 wants to merge 3 commits into
raystack:mainfrom
0xABCD01:fix/2047-feature-product-union
Open

0xABCD01 wants to merge 3 commits into
raystack:mainfrom
0xABCD01:fix/2047-feature-product-union

Conversation

@0xABCD01

@0xABCD01 0xABCD01 commented Nov 25, 2026 •

Copy link
Copy Markdown

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. UpsertFeature now unions the incoming product IDs with the stored ones, so an upsert only ever adds links.

Fixes #2047

Changes

  • 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

@vercel

vercel Bot commented Nov 25, 2026

Copy link
Copy Markdown

@0xABCD01 is attempting to deploy a commit to the Raystack Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Changes: Feature product links

Layer / File(s) Summary
Additive feature upserts
billing/product/service.go, billing/product/service_test.go
UpsertFeature merges incoming product IDs with stored IDs and preserves stored order. Tests cover adding links, empty input, and overlapping IDs.
Plan link updates
billing/plan/service.go
ProductService declares UpdateFeature. UpsertPlans calls it with the final product IDs and returns any update error.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • 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: Passed 2

Check name Status Explanation
Linked Issues check Passed 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 Passed 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.

Share

Recent review info

Run configuration

Commits

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.

@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

Review info

Run configuration

Commits

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.

Comment thread billing/product/service.go Outdated
// feature from the other products it already belongs to. Shrinking the
// set is a deliberate act and goes through UpdateFeature or
// RemoveFeatureFromProduct.
existingFeature.ProductIDs = utils.Deduplicate(append(existingFeature.ProductIDs, feature.ProductIDs...))

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.

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.

Comment thread billing/plan/service.go Outdated
}
featureOb.ProductIDs = productIDs
if _, err = s.productService.UpsertFeature(ctx, featureOb); err != nil {
// UpdateFeature, not UpsertFeature: this loop converges the stored set

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can get rid of this comment, it is a process that was used to reach here and not something we will need for future.

@0xABCD01

0xABCD01 commented Dec 2, 2026

Copy link
Copy Markdown
Author

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.

@rohilsurana rohilsurana left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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.

Comment thread billing/plan/service.go
}
featureOb.ProductIDs = productIDs
if _, err = s.productService.UpsertFeature(ctx, featureOb); err != nil {
if _, err = s.productService.UpdateFeature(ctx, featureOb); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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.

Could this loop union too?

Comment thread billing/product/service.go Outdated
}

existingFeature.ProductIDs = feature.ProductIDs
// An upsert only ever adds product links: the stored set is unioned with

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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.

return product.NewService(stripeClient, mockProductRepo, mockPriceRepo, mockFeatureRepo)
},
},
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we add a few more cases?

  • Empty incoming ids. This is the usual CreateFeature call. Links should stay.
  • Overlap: stored [a,b] plus incoming [b,c] gives [a,b,c] with no repeats.

@0xABCD01

0xABCD01 commented Dec 3, 2026

Copy link
Copy Markdown
Author

Follow-up pushed in cbd7cf4:

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating a product removes a shared feature from its other products

2 participants