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
feat(billing): mark subscription changes in sync state - #5554
Mark subscription changes in v2 sync state inside the create, update, cancel, continue, and delete transactions. Missing rows start as new with a NULL last_success_at and initialized revisions. Existing rows advance only current_subs_revision and updated_at, preserving the previous sync result and acknowledged revision.
Add AfterDelete inside the deletion transaction and register the sync hook once in the shared billing initializer used by OpenMeter and Cloud. Failed hook writes roll back the subscription command.
Notes for reviewer
Sync result writes continue to preserve revisions from concurrent subscription edits. Normal active syncs and periodic scheduling keep their existing behavior.
Downgrade fills NULL success timestamps on new rows with now() to satisfy the old schema; upgrading again clears those placeholders while retaining rows and revisions.
Validated with PostgreSQL-backed subscription sync and subscription tests, migration up/down/up, generation, go vet, and scoped fast lint.
Summary by CodeRabbit
New Features
Subscription changes are now tracked immediately across creation, updates, cancellation, continuation, and deletion.
New sync records are clearly distinguished from completed sync results.
Bug Fixes
Subscription deletion and its associated tracking updates now succeed or roll back together, preventing partial changes.
Confidence Score: 4/5
The PR appears safe to merge, with a non-blocking request to restore the deleted migration checks.
Summary
The PR records subscription changes in v2 sync state within each command’s transaction.
Subscription changes now mark billing sync state in the command transaction.
The earlier sync-revision backfill test is removed.
The review used source inspection; no runtime tests were run.
Diagram
sequenceDiagram
participant Command as Subscription command
participant Hook as Sync hook
participant DB as Database
participant Worker as Sync worker
Command->>DB: Begin transaction and change subscription
Command->>Hook: Run After hook
Hook->>DB: Insert new state or advance current revision
alt Hook succeeds
Command->>DB: Publish event and commit
else Hook fails
Command->>DB: Roll back command and marker
end
Worker->>DB: Read current revision under billing lock
Worker->>DB: Record outcome without replacing current revision
No actionable comments were generated in the recent review.
Walkthrough
Subscription commands now mark subscription revisions in v2 sync state through transactional hooks. The state model adds a new outcome with no success timestamp. Adapter, migration, service, and reconciliation tests are updated for these changes.
The state model adds new for rows without a recorded result and allows last_success_at to be NULL for that state. The migration changes the column constraint. The README describes the state semantics.
Revision writes and sync-state persistence openmeter/billing/worker/subscriptionsync/adapter/syncstatev2.go, openmeter/billing/worker/subscriptionsync/adapter/syncstatev2_test.go, openmeter/billing/worker/subscriptionsync/README.md
MarkSubscriptionChangedV2 initializes sync state or advances its current revision. The adapter checks subscription existence and maps nullable timestamps. Tests cover writes, errors, and transaction rollback.
The subscription service invokes AfterDelete hooks before publishing the deletion event. Billing hooks mark subscription changes through the adapter, and billing registers the hook. Service tests cover revision changes and rollback behavior.
Reconciliation state expectations openmeter/billing/worker/subscriptionsync/reconciler/reconciler_test.go, openmeter/billing/worker/subscriptionsync/reconciliaton_test.go
Reconciliation tests expect a new state with a nil success timestamp before a result is recorded. They also update timestamp assertions and state setup for cleanup and periodic reconciliation cases.
Subscription create, update, cancel, continue and delete now mark sync revisions inside the subscription transaction, so a failed marker write rolls the command back. No merge-blocking defects were found in the supplied changes.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "feat(billing): mark subscription changes in sync state" matches the main change. The PR adds MarkSubscriptionChangedV2 and subscription hooks that mark v2 sync state on create, update, canc…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "feat(billing): mark subscription changes in sync state" matches the main change. The PR adds MarkSubscriptionChangedV2 and subscription hooks that mark v2 sync state on create, update, cancel, continue, and delete. The title is concise, specific, and follows conventional commit style. It does not cover every detail, such as the nullable last_success_at migration, but that is expected.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
Deleting TestBackfillSubscriptionSyncRevisions removes the checks that existing rows keep their revisions and recorded results when the backfill runs or is repeated. The new adapter and command tests start with an already-migrated database, so they do not replace these checks. Keep this test, or move its cases into replacement migration tests.
Migration checks disappeartools/migrate/subscription_sync_revisions_test.go:16▶
Deleting TestBackfillSubscriptionSyncRevisions removes the checks that existing rows keep their revisions and recorded results when the backfill runs or is repeated. The new adapter and command tests start with an already-migrated database, so they do not replace these checks. Keep this test, or move its cases into replacement migration tests.
Yeah, I don't like BS tests to be honest. If possible note this to global memory, that I am no fan of recording migrations as tests for the sake of AI satisfaction.
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.
Overview
Mark subscription changes in v2 sync state inside the create, update, cancel, continue, and delete transactions. Missing rows start as
newwith a NULLlast_success_atand initialized revisions. Existing rows advance onlycurrent_subs_revisionandupdated_at, preserving the previous sync result and acknowledged revision.Add
AfterDeleteinside the deletion transaction and register the sync hook once in the shared billing initializer used by OpenMeter and Cloud. Failed hook writes roll back the subscription command.Notes for reviewer
newrows withnow()to satisfy the old schema; upgrading again clears those placeholders while retaining rows and revisions.go vet, and scoped fast lint.Summary by CodeRabbit
The PR appears safe to merge, with a non-blocking request to restore the deleted migration checks.
Summary
The PR records subscription changes in v2 sync state within each command’s transaction.
The review used source inspection; no runtime tests were run.
Diagram
sequenceDiagram participant Command as Subscription command participant Hook as Sync hook participant DB as Database participant Worker as Sync worker Command->>DB: Begin transaction and change subscription Command->>Hook: Run After hook Hook->>DB: Insert new state or advance current revision alt Hook succeeds Command->>DB: Publish event and commit else Hook fails Command->>DB: Roll back command and marker end Worker->>DB: Read current revision under billing lock Worker->>DB: Record outcome without replacing current revisionReviews (1) · Last reviewed commit: "feat(billing): track subscription change..." · Reviewed by Greptile