Skip to content

fix(app): prevent uninstall of apps required by billing - #4924

Merged
turip merged 2 commits into
mainfrom
feat/prevent-app-uninstall-with-open-invoices
Aug 14, 2026
Merged

turip merged 2 commits into
mainfrom
feat/prevent-app-uninstall-with-open-invoices

Conversation

@turip

@turip turip commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add synchronous typed app-uninstall hooks and invoke them inside the uninstall transaction before provider-specific cleanup
  • register a billing validator that prevents uninstall while an app is referenced by an active billing profile or a non-final standard invoice
  • treat invoice app references as immutable lifecycle history and bound conflict diagnostics
  • add PostgreSQL-backed billing and Stripe regressions for deleted-profile invoice references, final invoice release, and provider cleanup veto

Root cause

App usage validation lived at the HTTP boundary, so service-level uninstall callers could bypass it. The invoice usage query also enumerated selected active statuses and omitted draft states. After a billing profile was replaced or deleted, its draft invoices still owned snapshotted app references, but those references did not prevent the Stripe app from being uninstalled. Later invoice advancement then failed while resolving the missing app.

The guard now runs at the app service boundary before provider resources are removed, and billing blocks on every non-final standard invoice instead of maintaining an incomplete active-state allowlist.

Impact

Apps required to complete non-final invoices cannot be uninstalled. Once the referencing invoices reach a final state, uninstall is allowed again.

Validation

  • POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./test/billing ./test/app/stripe -count=1
  • make lint-go-fast for affected packages
  • make lint-go-style for affected packages
  • full configured make lint-go

Context

Maintenance follow-up to #4923. No Jira ticket.

Summary by CodeRabbit

  • New Features

    • Added lifecycle validation for app uninstall operations.
    • Apps cannot be uninstalled while referenced by active billing profiles or non-final invoices.
    • Uninstall checks now run before removal, with changes finalized transactionally.
  • Bug Fixes

    • Improved conflict messages by listing affected billing references and additional items.
  • Tests

    • Added coverage for protected app uninstall scenarios and invoice/profile usage behavior.

Greptile Summary

The PR moves billing app-usage enforcement into the app service’s transactional uninstall lifecycle and protects apps referenced by active profiles or non-final standard invoices.

  • Adds typed synchronous lifecycle hooks and registers the billing uninstall validator during application wiring.
  • Checks immutable invoice app references against final invoice states and bounds conflict diagnostics.
  • Adds PostgreSQL-backed regression coverage for deleted profiles, invoice finalization, and provider-cleanup vetoes.

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains within the eligible follow-up-review scope.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/app/service/app.go Runs uninstall hooks, provider cleanup, and app deletion within the uninstall transaction, then publishes the delete event.
openmeter/billing/adapter/invoice.go Blocks uninstall for app references held by every non-final standard invoice and limits diagnostic reference enumeration.
openmeter/billing/adapter/profile.go Preserves active-profile usage checks while bounding the profile references included in conflict errors.
openmeter/billing/validators/appuninstall/validator.go Implements the typed billing lifecycle hook that validates app usage before uninstall.
app/common/billing.go Registers the billing uninstall validator with the app service during dependency construction.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant AppService
    participant Hooks
    participant Billing
    participant Provider
    participant Database
    Caller->>AppService: UninstallApp(app ID)
    AppService->>Database: Begin transaction and load app
    AppService->>Hooks: Invoke uninstall lifecycle event
    Hooks->>Billing: IsAppUsed(app ID)
    Billing->>Database: Check active profiles and non-final invoices
    alt App is referenced
        Billing-->>Hooks: Conflict
        Hooks-->>AppService: Veto uninstall
        AppService->>Database: Roll back
        AppService-->>Caller: Conflict
    else App is unused
        Billing-->>Hooks: Allowed
        AppService->>Provider: Remove provider resources
        AppService->>Database: Soft-delete app and commit
        AppService-->>Caller: Success
    end
Loading

Reviews (4): Last reviewed commit: "refactor(app): generalize lifecycle hook" | Re-trigger Greptile

Context used (3)

@coderabbitai

coderabbitai Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 2375438b-380c-47c4-9c1f-b1edc677bba3

📥 Commits

Reviewing files that changed from the base of the PR and between 20c0efe and 3cd4bc1.

📒 Files selected for processing (1)
  • openmeter/billing/README.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • openmeter/billing/README.md

📝 Walkthrough

Walkthrough

App uninstall now uses synchronous lifecycle hooks. The billing validator blocks removal when active billing profiles or non-final invoices reference the app. Deletion and deletion-event publication are sequenced around a transaction, with Stripe and invoice snapshot integration tests.

Changes

App uninstall lifecycle

Layer / File(s) Summary
Lifecycle hook contracts
openmeter/app/service.go, openmeter/app/service/service.go, openmeter/server/server_test.go
App services now expose typed lifecycle events, the uninstall operation, and hook registration.
Transactional uninstall orchestration
openmeter/app/httpdriver/app.go, openmeter/app/service/app.go
The handler delegates directly to UninstallApp. The service invokes uninstall hooks before deletion and publishes the deletion event after transaction commit.
Billing usage validation
openmeter/billing/validators/appuninstall/*, openmeter/billing/adapter/profile.go, openmeter/billing/adapter/invoice.go, openmeter/billing/README.md
The validator checks app usage. Profile and invoice queries report active references and limit displayed conflict details.
Validator registration and integration coverage
app/common/billing.go, test/app/stripe/*, test/billing/profile_test.go
Billing setup registers the validator. Tests cover blocked Stripe app removal and usage retained by non-final invoice snapshots.

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

Mergeability Score: ⚪ Minimal · up to 3cd4b

The PR adds transactional billing checks that prevent uninstalling apps still needed by non-final invoices or active billing profiles, with regression coverage for the affected flows. No actionable merge-blocking risk remains beyond normal checks and review.

Possibly related PRs

Suggested labels: kind/bug

Sequence Diagram(s)

sequenceDiagram
  participant AppHTTPDriver
  participant AppService
  participant BillingAppUninstallValidator
  participant BillingService
  AppHTTPDriver->>AppService: UninstallApp
  AppService->>BillingAppUninstallValidator: Handle(LifecycleEvent)
  BillingAppUninstallValidator->>BillingService: IsAppUsed(app ID)
  BillingService-->>BillingAppUninstallValidator: usage result
  BillingAppUninstallValidator-->>AppService: validation result
  AppService->>AppService: delete in transaction
  AppService-->>AppHTTPDriver: result after commit
Loading
🚥 Pre-merge checks | ✅ 4
✅ 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: preventing uninstall of apps required by billing.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/prevent-app-uninstall-with-open-invoices

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.

@turip turip added release-note/bug-fix Release note: Bug Fixes area/billing labels Aug 13, 2026
@turip
turip marked this pull request as ready for review August 13, 2026 09:31
@turip
turip requested a review from a team as a code owner August 13, 2026 09:31
@turip
turip enabled auto-merge (squash) August 13, 2026 10:01
@turip
turip force-pushed the feat/prevent-app-uninstall-with-open-invoices branch from 20c0efe to 3cd4bc1 Compare August 13, 2026 14:50
@turip
turip force-pushed the feat/prevent-app-uninstall-with-open-invoices branch from 3cd4bc1 to 8ec918a Compare August 14, 2026 06:03
@turip
turip merged commit 506684e into main Aug 14, 2026
27 checks passed
@turip
turip deleted the feat/prevent-app-uninstall-with-open-invoices branch August 14, 2026 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants