Skip to content

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

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

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

Conversation

@0xABCD01

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. 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.

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.

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

@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 →

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Updating a feature’s product associations now preserves existing links when adding another product, instead of replacing them.
    • Plan updates apply the product associations provided for each feature.

Feature upserts now preserve existing product links and add incoming links. Plan reconciliation uses UpdateFeature to apply the product IDs derived from plan files.

Changes: Feature product links

Layer / File(s) Summary
Additive upserts and plan link replacement
billing/plan/service.go, billing/product/service.go, billing/product/service_test.go
UpsertFeature deduplicates existing and incoming product IDs. Plan reconciliation calls UpdateFeature with plan-derived product IDs. The test checks that an upsert retains prior product links and adds the incoming link.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • raystack/frontier#2046: Adds the BillingFeature reconcile kind and references the product-link behavior changed here.

Suggested reviewers: kushsharma, rohanchkrabrty

Merge Risk: Low · up to 05d87

Creating a product with an existing feature now adds the link instead of removing the feature's other product links, and plan files still replace the full set. A remaining edge case is two simultaneous upserts of the same feature, where one added link could be lost. This race was already present and is bounded, so the change is mergeable with a follow-up to make the merge atomic.

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 link without removing existing links. It also requires that CreateFeature on an existing name either fail wit…
Out of Scope Changes check Passed All changes connect to #2047. The UpsertFeature union in billing/product/service.go is the core fix. The billing/plan/service.go change adds UpdateFeature to the ProductService interface and switches …
Full details: Linked Issues check

Explanation

Issue #2047 requires that creating a product with an existing feature adds the new product link without removing existing links. It also requires that CreateFeature on an existing name either fail with "already exists" or keep existing links. The PR changes UpsertFeature in billing/product/service.go to union the incoming ProductIDs with the stored ones, deduplicated (per the whole-PR AI summary and PR description). Service.Create calls UpsertFeature for each product feature, so the product-create path now adds links. The CreateFeature handler also goes through UpsertFeature, so an existing name keeps its links and adds the request's product IDs, which satisfies the second expected behavior. The regression test in billing/product/service_test.go covers the A+B plus C case. The UpsertPlans convergence loop now calls UpdateFeature, which keeps replace semantics. The issue states that UpsertPlans intentionally rebuilds the full link list, so this preserves its behavior. Limitation: the raw diff blobs were unavailable in this review, so the verdict relies on the supplied summary and PR description, not a direct read of the changed hunks. The UpdateFeature behavior for a feature that does not yet exist was not verified.

Full details: Out of Scope Changes check

Explanation

All changes connect to #2047. The UpsertFeature union in billing/product/service.go is the core fix. The billing/plan/service.go change adds UpdateFeature to the ProductService interface and switches the final UpsertPlans convergence loop to UpdateFeature. This is required so the fix does not change plan-file convergence, which the issue says must keep rebuilding the full link list. The billing/product/service_test.go addition is a regression test for the union. No unrelated changes were identified in the supplied summary. Limitation: the raw diff blobs were unavailable, so the assessment relies on the supplied summary and PR description for the full change list.

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

@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.

// 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.

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

1 participant