fix: meter delete in tx - #4838
Conversation
📝 WalkthroughWalkthroughChangesMeter lookup now uses a transaction-scoped repository. Meter transaction handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant DeleteMeter
participant Database
participant EventPublisher
Client->>DeleteMeter: delete meter
DeleteMeter->>Database: validate and soft-delete meter
Database-->>DeleteMeter: deleted meter state
DeleteMeter->>EventPublisher: publish deletion event
EventPublisher-->>DeleteMeter: success or error
DeleteMeter->>Database: commit or roll back transaction
DeleteMeter-->>Client: result or publication error
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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/meter/service/manage.go`:
- Around line 86-157: Extract the meter deletion workflow from the anonymous
callback in DeleteMeter into a private named helper method, including lookup,
validation, feature and entitlement checks, deletion, re-fetch, and event
publication. Keep DeleteMeter’s transaction.RunWithNoValue callback as a thin
call-through to that helper, preserving the existing context, error handling,
and behavior.
- Around line 150-154: Move the meter-deleted event publishing out of
DeleteMeter’s transaction: keep the soft delete and event construction within
the existing transaction, then publish only after a successful commit using the
established RunInNewTransaction post-commit pattern or outbox flow. Ensure
transaction rollback or commit failure cannot publish meterDeletedEvent, while
preserving the existing publish error propagation.
🪄 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 Plus
Run ID: db3df8ce-eed6-44e8-ab49-45ce41c66c6c
📒 Files selected for processing (3)
openmeter/meter/adapter/meter.goopenmeter/meter/service/manage.goopenmeter/meter/service/manage_test.go
Overview
Make meter delete to be atomic operation by running it in database transaction alongside the read operations used for lookup.
Greptile Summary
The PR makes meter deletion checks, soft deletion, and the post-delete lookup share one PostgreSQL transaction. It also adds integration coverage for successful commit and rollback when event publication fails, but publishing from inside the transaction permits an acknowledged event to outlive a later database commit failure.
GetMeterByIDOrSlugtransaction-aware.DeleteMeterservice flow in a transaction.Confidence Score: 4/5
This PR should not merge until deletion events are prevented from becoming externally visible before the corresponding database deletion is durably committed.
DeleteMeter now sends an acknowledged Kafka event from inside the transaction callback, so a later PostgreSQL commit failure leaves consumers with a deletion event for a meter that remains active.
Files Needing Attention: openmeter/meter/service/manage.go
Important Files Changed
Sequence Diagram
sequenceDiagram participant S as ManageService participant DB as PostgreSQL transaction participant K as Kafka participant C as Consumer S->>DB: Soft-delete meter S->>DB: Read deleted meter S->>K: Publish meter.deleted K-->>S: Acknowledge event K-->>C: Event becomes observable S->>DB: Commit transaction alt Commit fails DB-->>S: Rollback/error Note over K,C: Published event cannot be rolled back endPrompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix: meter delete in tx" | Re-trigger Greptile