Skip to content

Commit 1c78f91

Browse files
authored
feat: remove expected revision check (#939)
1 parent ab9269e commit 1c78f91

File tree

1 file changed

+2
-39
lines changed

1 file changed

+2
-39
lines changed

libs/hwes/eventstoredb/aggregate_store.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,16 @@ type AggregateStore struct {
1919
es *esdb.Client
2020
}
2121

22-
// getExpectedRevision is for our strategy pattern to resolve the expected version
23-
// before a commit to EventStore between the aggregate and EventStore.
24-
type getExpectedRevision = func(ctx context.Context, aggregate hwes.Aggregate) (esdb.ExpectedRevision, error)
25-
2622
func NewAggregateStore(es *esdb.Client) *AggregateStore {
2723
return &AggregateStore{es: es}
2824
}
2925

3026
var ErrNoAppliedEvents = errors.New("aggregate has no applied events. " +
3127
"Consider to persist and load the aggregate first")
3228

33-
// getExpectedRevisionByPreviousRead implements a strategy for our getExpectedRevision strategy pattern.
34-
// This function resolves the version by returning the version of the last applied event of our aggregate.
35-
func (as *AggregateStore) getExpectedRevisionByPreviousRead(
36-
_ context.Context,
37-
a hwes.Aggregate,
38-
) (esdb.ExpectedRevision, error) {
39-
if len(a.GetAppliedEvents()) == 0 {
40-
return nil, ErrNoAppliedEvents
41-
}
42-
lastAppliedEvent := a.GetAppliedEvents()[len(a.GetAppliedEvents())-1]
43-
eventNumber := lastAppliedEvent.GetVersion()
44-
return esdb.Revision(eventNumber), nil
45-
}
46-
4729
func (as *AggregateStore) doSave(
4830
ctx context.Context,
4931
a hwes.Aggregate,
50-
getExpectedRevision getExpectedRevision,
5132
) (common.ConsistencyToken, error) {
5233
ctx, span, log := telemetry.StartSpan(ctx, "AggregateStore.doSave")
5334
defer span.End()
@@ -67,24 +48,10 @@ func (as *AggregateStore) doSave(
6748
fmt.Errorf("AggregateStore.doSave: could not convert one uncommitted event to event data: %w", err)
6849
}
6950

70-
var expectedRevision esdb.ExpectedRevision
71-
72-
// If AppliedEvents are empty, we imply that this entity was not loaded from an event store and therefore non-existing.
73-
if len(a.GetAppliedEvents()) == 0 {
74-
// Create a stream
75-
expectedRevision = esdb.NoStream{}
76-
} else {
77-
// We resolve the expectedRevision by the passed strategy of the caller
78-
expectedRevision, err = getExpectedRevision(ctx, a)
79-
if err != nil {
80-
return 0, fmt.Errorf("AggregateStore.doSave: could not resolve expected revision: %w", err)
81-
}
82-
}
83-
8451
r, err := as.es.AppendToStream(
8552
ctx,
8653
a.GetTypeID(),
87-
esdb.AppendToStreamOptions{ExpectedRevision: expectedRevision},
54+
esdb.AppendToStreamOptions{},
8855
eventsData...,
8956
)
9057
if err != nil {
@@ -143,11 +110,7 @@ func (as *AggregateStore) Load(ctx context.Context, aggregate hwes.Aggregate) er
143110
}
144111

145112
func (as *AggregateStore) Save(ctx context.Context, aggregate hwes.Aggregate) (common.ConsistencyToken, error) {
146-
// We can switch out the getExpectedRevision strategy for testing optimistic concurrency.
147-
// It is not intended to switch the strategy in production.
148-
// To ensure consistency and correctly applied events during another read,
149-
// getExpectedRevisionByPreviousRead is the preferred method for production use.
150-
return as.doSave(ctx, aggregate, as.getExpectedRevisionByPreviousRead)
113+
return as.doSave(ctx, aggregate)
151114
}
152115

153116
func (as *AggregateStore) Exists(ctx context.Context, aggregate hwes.Aggregate) (bool, error) {

0 commit comments

Comments
 (0)