Comments (2)
Probably we don't even need all of this logic anymore as we could just update one record each time. We could not do this in earlier versions of laravel as upsert method was introduced only recently. Though for this to work we would also need to add unique index on snapshots.aggregate_uuid and that would be a breaking change. Entire application would need to stop to clear duplicate records and to add a new index, it does not sound easy.
$this->snapshotModel()->upsert([
'aggregate_uuid' => $snapshot->aggregateUuid,
'aggregate_version' => $snapshot->aggregateVersion,
'state' => $snapshot->state,
], ['aggregate_uuid'], ['aggregate_version', 'state', 'updated_at']);
I would be willing to create a pull requests with all of these strategies and tests and make it configurable via config
.
from laravel-event-sourcing.
Would you accept this pull request? It ensures that all old snapshots would get deleted after a new one was created.
EloquentSnapshotRepository.php:
public function persist(Snapshot $snapshot): Snapshot
{
/** @var \Spatie\EventSourcing\Snapshots\EloquentSnapshot $eloquentSnapshot */
$eloquentSnapshot = new $this->snapshotModel();
$eloquentSnapshot->aggregate_uuid = $snapshot->aggregateUuid;
$eloquentSnapshot->aggregate_version = $snapshot->aggregateVersion;
$eloquentSnapshot->state = $snapshot->state;
$eloquentSnapshot->save();
$snapshts = $eloquentSnapshot::query()
->where('aggregate_uuid', $snapshot->aggregateUuid)
->where('aggregate_version', '<', $snapshot->aggregateVersion)
->oldest()
->limit(5)
->pluck('id');
$eloquentSnapshot::query()->whereIn('id', $ids)->delete();
return $eloquentSnapshot->toSnapshot();
}
Notice that I have also added a limit to this query. Probably at this point some aggregate roots might have more than 1000 snapshots and we don't want to timeout these requests.
I am not doing a simple query like below, since mysql performs best, then delete is performed on unique index. I want to avoid gap locking at any cost. Also aggregate_version column has no index and that might create a lock on entire table.
$this->snapshotModel()
->query()
->where('aggregate_uuid', $snapshot->aggregateUuid)
->where('aggregate_version', '<', $snapshot->aggregateVersion)
->delete();
Drawbacks? Migration period might create some temporary latencies while each aggregate root is performing snapshot cleanup.
from laravel-event-sourcing.
Related Issues (20)
- Cannot retrieve() cause of failing instantiate Enum
- Allowed memory size of x bytes exhausted
- Recording events from AggregatePartial
- from v4 to v7 test failed when using mock()->makePartial()
- Search is not working HOT 1
- Replay events HOT 2
- vendor:publish not working
- Lock wait timeout exceeded
- Docs - versions cross linked HOT 2
- Attribute EventSerializer is not used for serialize HOT 1
- Outdated Docs question HOT 1
- Prevent loops when sending events into Aggregate methods HOT 2
- The "auto_discover_projectors_and_reactors" config works regardless of "dont-discover" in composer.json HOT 3
- Deprecation notice: $firedFromAggregateRoot is deprecated HOT 2
- Memory allocated exception on version 7.3.7 HOT 1
- Version 7.3.7 tries to apply events to variadic and untyped functions in the aggregate root HOT 5
- created_at field is always updated when saving a EloquentStoredEvent HOT 1
- Projector and Reactor discovery fails due to incorrect namespace resolution
- this is probably not related to this package, but hear me out
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-event-sourcing.