Repository navigation
feat(billing): add subscription sync v2 state persistence - #5514
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
tothandras
left a comment
There was a problem hiding this comment.
No blockers from my side. A few things worth considering before the sync service starts building on this.
| func (SubscriptionSyncStateV2) Fields() []ent.Field { | ||
| return []ent.Field{ | ||
| field.String("subscription_id").NotEmpty().Immutable().Unique().SchemaType(map[string]string{dialect.Postgres: "char(26)"}), | ||
| field.Enum("last_success_state").GoType(subscriptionsync.SyncStateV2State("")), |
There was a problem hiding this comment.
This makes ent/schema import the root subscriptionsync package. Since ent/db imports ent/schema, that root package can now never (transitively) import anything that reaches ent/db — and openmeter/subscription does. It works today only because the root package imports just pkg/models and pkg/framework/entutils, but it also holds service.go, so it seems likely to want subscription types at some point.
Could we move the enum to a leaf package to avoid the trap?
There was a problem hiding this comment.
Keeping this in the root domain package follows the existing repository pattern: Ent schemas already use enums from root packages such as billing and ledger. The root package holds contracts and domain types, while the service implementation lives in the service subpackage. The dependency restriction is real, but it is consistent with that layering, so I would keep the enum here.
| } | ||
|
|
||
| func (SubscriptionSyncStateV2) Mixin() []ent.Mixin { | ||
| return []ent.Mixin{entutils.IDMixin{}, entutils.NamespaceMixin{}, entutils.TimeMixin{}} |
There was a problem hiding this comment.
TimeMixin adds a deleted_at column that is never used, and the FK cascade hard-deletes rows anyway. updated_at also nearly duplicates last_success_at.
Would explicit created_at/updated_at fields (or no row timestamps at all) be enough here, rather than shipping a dead column?
There was a problem hiding this comment.
Using the standard TimeMixin is intentional for consistency with the other persisted models, including keeping deleted_at available if we introduce soft deletion later. updated_at and last_success_at have different meanings: the former records the row write, while the latter records reconciliation completion, so a delayed write or retry can make them differ. Keeping the mixin as-is.
| // SyncStateV2Managed includes the sync-state row's identity and timestamps. | ||
| type SyncStateV2Managed struct { | ||
| SyncStateV2 | ||
| models.NamespacedID |
There was a problem hiding this comment.
Embedding models.NamespacedID (the row ID) next to SubscriptionID models.NamespacedID means state.ID / state.NamespacedID are the row's, not the subscription's — easy to pass by mistake where a subscription ID is expected.
Nothing consumes the row ID yet. Could we drop it until something needs it and return SyncStateV2 plus the timestamps?
There was a problem hiding this comment.
I would keep the row identity in SyncStateV2Managed alongside its timestamps, consistent with our managed-model convention. SubscriptionID explicitly identifies the parent, while the embedded NamespacedID identifies the state row. The schema's existing ULID IDMixin is also intentional and stays in place. That schema decision is separate from whether we expose the row ID in the returned Go type; omitting it there would not change ID generation.
| } | ||
|
|
||
| func (s SyncStateV2State) Validate() error { | ||
| if s != SyncStateV2StateSubscriptionDeleted { |
There was a problem hiding this comment.
nit: this hardcodes the single value that Values() already lists, so the next state has to be added in two places. slices.Contains(s.Values(), string(s)) would keep them in sync.
There was a problem hiding this comment.
Fixed in bf21379: validation now uses slices.Contains(s.Values(), string(s)), so Values() is the single source of supported states.
| "github.com/openmeterio/openmeter/pkg/models" | ||
| ) | ||
|
|
||
| func TestSyncStateV2Persistence(t *testing.T) { |
There was a problem hiding this comment.
nit: this covers six scenarios in one function with three FreezeTime calls sharing state. Splitting into subtests would localize failures.
There was a problem hiding this comment.
Fixed in bf21379: split this into six independent subtests, each with its own database fixture and scoped clock reset. The existing persistence, timestamp, namespace, rollback, foreign-key, and cascade coverage is preserved. All six PostgreSQL subtests and scoped formatting/fast lint passed.
Overview
Add a separate subscription sync v2 table to record the last successful reconciliation result and completion time, initially supporting
subscription_deleted. A missing row means no successful v2 reconciliation has been recorded.The Ent schema uses the shared subscriptionsync enum, namespace/subscription uniqueness, and a subscription foreign key with cascading hard deletion.
TimeMixinsupplies row timestamps; itsdeleted_atfield is currently unused. Adapter reads returnSyncStateV2Managedwith row identity and timestamps, and transaction-aware upserts preservecreated_atwhile refreshingupdated_at.Notes for reviewer
Builds on the merged #5512 and is rebased onto current
main. This PR adds persistence only: the sync service and periodic reconciler do not yet use v2 state, and the deleted-subscription filter is unchanged.Validation: PostgreSQL adapter tests cover managed metadata, repeated upserts, namespace isolation, rollback, foreign-key enforcement, and cascading deletion. Ent generation, Atlas migration validation, scoped
go vet,make lint-go-fast, andgit diff --checkpassed; the adapter test and fast lint were rerun after rebasing.Summary by CodeRabbit
subscription_deleted; no record means no successful v2 reconciliation has been recorded.

Confidence Score: 5/5
The PR appears safe to merge; no actionable issues remain in the reviewed changes.Summary
Adds separate storage for the last successful v2 subscription reconciliation.
The latest changes preserve validation behavior and split persistence tests into focused subtests. No new actionable issues were found. Tests were not run during this review.
Reviews (3) · Last reviewed commit: "refactor(billing): address sync state v2..." · Reviewed by Greptile