Skip to content

feat(billing): mark subscription changes in sync state - #5554

Merged
turip merged 1 commit into
mainfrom
feat/subscription-sync-revision-hooks
Nov 17, 2026
Merged

turip merged 1 commit into
mainfrom
feat/subscription-sync-revision-hooks

Conversation

@turip

@turip turip commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Overview

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.

RetriggerView in GreptileConfidence 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
Loading

Reviews (1) · Last reviewed commit: "feat(billing): track subscription change..." · Reviewed by Greptile

@turip
turip requested a review from a team as a code owner November 16, 2026 19:24
@turip turip added the kind/feature New feature or request label Nov 16, 2026
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Changes: Subscription sync state

Layer / File(s) Summary
Unrecorded sync-state contract
openmeter/billing/worker/subscriptionsync/syncstatev2.go, openmeter/ent/schema/subscriptionsyncstatev2.go, tools/migrate/migrations/*, tools/migrate/subscription_sync_revisions_test.go, openmeter/billing/worker/subscriptionsync/README.md
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.
Transactional subscription hooks
openmeter/subscription/hook.go, openmeter/subscription/service/*, openmeter/billing/worker/subscriptionsync/hooks/hook.go, app/common/billing.go, openmeter/billing/worker/subscriptionsync/service/*
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.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Other

Sequence Diagram(s)

sequenceDiagram
  participant SubscriptionService
  participant Hook
  participant adapter
  participant SyncStateV2
  SubscriptionService->>Hook: Run subscription command hook
  Hook->>adapter: MarkSubscriptionChangedV2(subscription ID)
  adapter->>SyncStateV2: Initialize or advance current revision
  SyncStateV2-->>adapter: Persisted state
  adapter-->>Hook: Return result
  Hook-->>SubscriptionService: Return result
Loading

Possibly related PRs

Suggested labels: area/billing

Suggested reviewers: galexihu, borosr, tothandras

Merge Risk: Minimal · up to 462f8

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 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.
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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f305c47 and 462f84c.

Files ignored due to path filters: 9

  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • openmeter/ent/db/mutation.go is excluded by !**/ent/db/**
  • openmeter/ent/db/setorclear.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionsyncstatev2.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionsyncstatev2/subscriptionsyncstatev2.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionsyncstatev2/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionsyncstatev2_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionsyncstatev2_update.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing: 18

  • app/common/billing.go
  • openmeter/billing/worker/subscriptionsync/README.md
  • openmeter/billing/worker/subscriptionsync/adapter/syncstatev2.go
  • openmeter/billing/worker/subscriptionsync/adapter/syncstatev2_test.go
  • openmeter/billing/worker/subscriptionsync/hooks/hook.go
  • openmeter/billing/worker/subscriptionsync/reconciler/reconciler_test.go
  • openmeter/billing/worker/subscriptionsync/reconciliaton_test.go
  • openmeter/billing/worker/subscriptionsync/service/base_test.go
  • openmeter/billing/worker/subscriptionsync/service/sync_test.go
  • openmeter/billing/worker/subscriptionsync/syncstatev2.go
  • openmeter/billing/worker/subscriptionsync/syncstatev2_test.go
  • openmeter/ent/schema/subscriptionsyncstatev2.go
  • openmeter/subscription/hook.go
  • openmeter/subscription/service/README.md
  • openmeter/subscription/service/service.go
  • tools/migrate/migrations/20261116154613_subscription_sync_change_hooks.down.sql
  • tools/migrate/migrations/20261116154613_subscription_sync_change_hooks.up.sql
  • tools/migrate/subscription_sync_revisions_test.go
Files with no reviewable changes: 1

  • tools/migrate/subscription_sync_revisions_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@greptile-apps

greptile-apps Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P2 Migration checks disappear tools/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.

@turip

turip commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Comments Outside Diff

These findings could not be posted inline.

  • P2 Migration checks disappear tools/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.

@turip
turip merged commit ed6df78 into main Nov 17, 2026
33 of 35 checks passed
@turip
turip deleted the feat/subscription-sync-revision-hooks branch November 17, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants