Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version is not set when recreating Aggregate from SnapShot #414

Open
totemcaf opened this issue Dec 5, 2023 · 1 comment
Open

Version is not set when recreating Aggregate from SnapShot #414

totemcaf opened this issue Dec 5, 2023 · 1 comment

Comments

@totemcaf
Copy link

totemcaf commented Dec 5, 2023

Describe the bug

When an Aggregate is loaded by the AggregateStore, if the aggregate has Snapshots configured, after initializing the Aggregate with the Snapshot data, the Aggregate version is not modified and remains in 0.

The eventhorizon.Snapshot structure contains the Version field, but it is not set in the Aggregate.

If the Aggregate contains more events (not included in the Snapshot), the application of this events will set the Version correctly, and the error is hidden.

To Reproduce
Steps to reproduce the behavior:

  1. Create an Aggregate
  2. Configure it to take a snapshot every 2 events
  3. Send a command that fires 2 events in the aggregate
  4. A Snapshot should be taken and persisted
  5. Send another command that fires one event
  6. The aggregate should be loaded from the Snapshot
  7. The aggregate will have version 0 (it should be 2)
  8. The new event will have version 1, instead of 3
  9. The persistence of events will fail

Expected behavior
In step 6, when the aggregate is loaded from the Snapshot store, it should have Version 2
In step 8, the new event should have version 3, and it is correctly persisted

Screenshots
N/A

Additional context
Add any other context about the problem here.

func (r *AggregateStore) Load(ctx context.Context, aggregateType eh.AggregateType, id uuid.UUID) (eh.Aggregate, error) {
// ...
	fromVersion := 1

	if sa, ok := a.(eh.Snapshotable); ok && r.isSnapshotStore {
		snapshot, err := r.snapshotStore.LoadSnapshot(ctx, id)
		if err != nil {
			return nil, &eh.AggregateStoreError{
				Err:           err,
				Op:            eh.AggregateStoreOpLoad,
				AggregateType: aggregateType,
				AggregateID:   id,
			}
		}

		if snapshot != nil {
			sa.ApplySnapshot(snapshot)
			fromVersion = snapshot.Version + 1
			a.SetAggregateVersion(snapshot.Version) // <--- THIS IS THE MISSING LINE
		}
	}
// ...

@totemcaf
Copy link
Author

totemcaf commented Dec 5, 2023

I will provide a PR soon (hope, this week)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant