Skip to content

refactor(billing): require subscription sync revisions - #5545

Merged
turip merged 2 commits into
mainfrom
feat/subscription-sync-revision-backfill
Nov 16, 2026
Merged

turip merged 2 commits into
mainfrom
feat/subscription-sync-revision-backfill

Conversation

@turip

@turip turip commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Overview

Backfill missing subscription sync revisions and require both revision columns so sync can read an existing row's revision directly. The backfill uses om_func_generate_ulid(), fills only NULL fields, and assigns one shared token when both fields are missing. A following migration enforces NOT NULL without adding defaults.

Change the domain revision fields to strings and rename the adapter operation and input to RecordSyncResultV2 and RecordSyncResultV2Input, with no compatibility stubs. Recording cleanup requires the revision observed under the billing lock. Inserts initialize both revisions; conflict updates acknowledge only the captured revision and never overwrite a parallel subscription edit's current revision. Sync generates a ULID only when no v2 row exists.

Notes for reviewer

  • V2 writes remain limited to successful cleanup. Ordinary syncs, dry runs, and failed transactions retain their existing behavior.
  • The data backfill preserves populated revisions and cleanup metadata and is safe to rerun. Downgrade retains backfilled values and removes the NOT NULL constraints.
  • Tests cover all NULL combinations, preservation of existing equal or different revisions, reruns, missing-revision rejection, constraint enforcement, downgrade, and revision changes during cleanup.
  • Validated after rebasing onto origin/main: PostgreSQL-backed subscription-sync tests, focused backfill and upgrade/downgrade tests, scoped fast lint, Ent regeneration, and Atlas validation.

Summary by CodeRabbit

  • Improvements
    • Subscription sync now requires both revision values to be present. Existing records are backfilled while preserving revisions already stored.
    • Sync results record the revision observed during synchronization without overwriting a newer current revision.
  • Bug Fixes
    • Cleanup acknowledgments no longer initialize or overwrite the current revision when recording sync results, helping preserve revision state across cleanup and synchronization.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no blocking issues remain.

Summary

This PR backfills missing subscription revisions, requires both revision columns, and records cleanup results without replacing a newer current revision.

  • Every subscription sync row keeps both revision values.
  • Cleanup records the revision it saw without replacing a newer one.

Reviews (2) · Last reviewed commit: "fix(billing): document revision backfill..." · Reviewed by Greptile

@turip
turip requested a review from a team as a code owner November 16, 2026 12:49
@turip turip added the kind/feature New feature or request label Nov 16, 2026 — with ChatGPT Codex Connector
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The v2 subscription sync contract now requires both revision fields. A migration backfills missing values before adding NOT NULL constraints. Sync records the observed revision through RecordSyncResultV2; new rows receive that revision in both fields, while conflicts preserve current_subs_revision.

Changes: V2 subscription sync revision contract

Layer / File(s) Summary
Backfill and require revision fields
openmeter/ent/schema/subscriptionsyncstatev2.go, tools/migrate/migrations/*subscription_sync_revisions*.sql, tools/migrate/subscription_sync_revisions_test.go
The migration backfills missing revision values, then requires both columns. Migration tests cover backfill behavior, constraints, and rollback.
Define and persist sync results
openmeter/billing/worker/subscriptionsync/syncstatev2.go, openmeter/billing/worker/subscriptionsync/adapter/syncstatev2.go, openmeter/billing/worker/subscriptionsync/adapter/*test.go
The adapter records a supplied observed revision. New rows use it for both revision fields. Conflict updates do not change current_subs_revision.
Use the revision contract in reconciliation
openmeter/billing/worker/subscriptionsync/service/*, openmeter/billing/worker/subscriptionsync/reconciliaton_test.go, openmeter/billing/worker/subscriptionsync/README.md
The service generates a revision when no sync state exists and records cleanup results with RecordSyncResultV2. Tests and the README reflect the required string fields and recording behavior.

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

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant SubscriptionSyncService
  participant SyncStateV2Adapter
  participant subscription_sync_states_v2
  SubscriptionSyncService->>SyncStateV2Adapter: RecordSyncResultV2 with observed revision
  SyncStateV2Adapter->>subscription_sync_states_v2: Insert both revisions or update last-synced revision
Loading

Possibly related PRs

  • openmeterio/openmeter#5538: Adds the same revision columns and establishes the insert and conflict behavior changed here.
  • openmeterio/openmeter#5516: Records subscription deletion in v2 sync state during cleanup, which this PR updates to use the new recording contract.
  • openmeterio/openmeter#5514: Adds the v2 sync-state table and adapter that this PR updates with required revision fields and a new recording operation.

Suggested labels: area/billing, kind/refactor

Suggested reviewers: tothandras, galexihu, chrisgacsal

Merge Risk: Minimal · up to 05790

This change makes subscription sync revisions required. It backfills missing values first and records the observed revision during cleanup without overwriting a concurrent subscription edit. The reviewed evidence shows no concrete merge-blocking risk, and the migrations have tests and a rollback path.

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 names the main change: the subscription sync revision columns become required (NOT NULL), backed by a backfill migration and a string-typed domain model. The refactor(billing) prefix matches…
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 names the main change: the subscription sync revision columns become required (NOT NULL), backed by a backfill migration and a string-typed domain model. The refactor(billing) prefix matches the changed billing worker code and migrations. The title is short, specific, and avoids vague wording, so it is a clear match for the changeset.

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 a30afd0 and 05790be.

Files ignored due to path filters: 1

  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
Files selected for processing: 1

  • tools/migrate/migrations/20261116104138_require_subscription_sync_revisions.up.sql
Files skipped from review as they are similar to previous changes: 1

  • tools/migrate/migrations/20261116104138_require_subscription_sync_revisions.up.sql

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

@turip
turip merged commit 635c88f into main Nov 16, 2026
32 checks passed
@turip
turip deleted the feat/subscription-sync-revision-backfill branch November 16, 2026 13:33
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