From fcc645f221a4fdc961f8781e4e4176656de2d89c Mon Sep 17 00:00:00 2001 From: Costin Soare Date: Tue, 26 Jul 2022 15:53:09 +0200 Subject: [PATCH 1/4] add uuid as option to the replay command --- src/Console/ReplayCommand.php | 9 +++++---- src/Projectionist.php | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Console/ReplayCommand.php b/src/Console/ReplayCommand.php index f5d099b8..b4ec91e7 100644 --- a/src/Console/ReplayCommand.php +++ b/src/Console/ReplayCommand.php @@ -12,7 +12,8 @@ class ReplayCommand extends Command { protected $signature = 'event-sourcing:replay {projector?*} {--from=0 : Replay events starting from this event number} - {--stored-event-model= : Replay events from this store}'; + {--stored-event-model= : Replay events from this store} + {--uuid= : Replay events for this uuid only}'; protected $description = 'Replay stored events'; @@ -30,7 +31,7 @@ public function handle(Projectionist $projectionist): void return; } - $this->replay($projectors, (int)$this->option('from')); + $this->replay($projectors, (int)$this->option('from'), $this->option('uuid')); } public function selectProjectors(array $projectorClassNames): ?Collection @@ -54,7 +55,7 @@ public function selectProjectors(array $projectorClassNames): ?Collection }); } - public function replay(Collection $projectors, int $startingFrom): void + public function replay(Collection $projectors, int $startingFrom, ?string $uuid = null): void { $repository = app(StoredEventRepository::class); $replayCount = $repository->countAllStartingFrom($startingFrom); @@ -72,7 +73,7 @@ public function replay(Collection $projectors, int $startingFrom): void $bar->advance(); }; - $this->projectionist->replay($projectors, $startingFrom, $onEventReplayed); + $this->projectionist->replay($projectors, $startingFrom, $onEventReplayed, $uuid); $bar->finish(); diff --git a/src/Projectionist.php b/src/Projectionist.php index 2c0eeebf..bcaa57cd 100644 --- a/src/Projectionist.php +++ b/src/Projectionist.php @@ -319,7 +319,8 @@ public function isReplaying(): bool public function replay( Collection $projectors, int $startingFromEventId = 0, - callable $onEventReplayed = null + callable $onEventReplayed = null, + ?string $uuid = null ): void { $projectors = new EventHandlerCollection($projectors); @@ -338,7 +339,7 @@ public function replay( $projectors->call('onStartingEventReplay'); app(StoredEventRepository::class) - ->retrieveAllStartingFrom($startingFromEventId) + ->retrieveAllStartingFrom($startingFromEventId, $uuid) ->each(function (StoredEvent $storedEvent) use ($projectors, $onEventReplayed) { $this->applyStoredEventToProjectors( $storedEvent, From 5a8877b12e7d4cd4d889418e0f006c2c2211e5a8 Mon Sep 17 00:00:00 2001 From: Costin Soare Date: Tue, 26 Jul 2022 15:59:05 +0200 Subject: [PATCH 2/4] support for specific uuid in count events --- src/Console/ReplayCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/ReplayCommand.php b/src/Console/ReplayCommand.php index b4ec91e7..263fb753 100644 --- a/src/Console/ReplayCommand.php +++ b/src/Console/ReplayCommand.php @@ -58,7 +58,7 @@ public function selectProjectors(array $projectorClassNames): ?Collection public function replay(Collection $projectors, int $startingFrom, ?string $uuid = null): void { $repository = app(StoredEventRepository::class); - $replayCount = $repository->countAllStartingFrom($startingFrom); + $replayCount = $repository->countAllStartingFrom($startingFrom, $uuid); if ($replayCount === 0) { $this->warn('There are no events to replay'); From a6647d70a2bbb85396ddc04bb9e6c14a76a9e02c Mon Sep 17 00:00:00 2001 From: Costin Soare Date: Wed, 27 Jul 2022 10:26:52 +0200 Subject: [PATCH 3/4] Add tests --- tests/Console/ReplayCommandTest.php | 32 ++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/Console/ReplayCommandTest.php b/tests/Console/ReplayCommandTest.php index 03dcfcff..f0d61f0f 100644 --- a/tests/Console/ReplayCommandTest.php +++ b/tests/Console/ReplayCommandTest.php @@ -1,16 +1,19 @@ expectsOutput('Replaying 5 events...') ->assertExitCode(0); } + + /** @test */ + public function it_will_replay_events_for_a_specific_aggregate_root_uuid() + { + EloquentStoredEvent::truncate(); + + $uuid1 = Uuid::uuid4(); + $account1 = AccountAggregateRoot::retrieve($uuid1); + $account1->addMoney(1000); + $account1->persist(); + + $uuid2 = Uuid::uuid4(); + $account2 = AccountAggregateRoot::retrieve($uuid2); + $account2->addMoney(1000); + $account2->persist(); + + $projector = app(AccountProjector::class); + + Projectionist::addProjector($projector); + + $this->artisan('event-sourcing:replay', [ + 'projector' => [AccountProjector::class], + '--uuid' => $uuid1 + ]) + ->expectsOutput('Replaying 1 events...') + ->assertExitCode(0); + } } From c4eaa1f8296b04928e675c366beb70747900da55 Mon Sep 17 00:00:00 2001 From: Costin Soare Date: Mon, 22 Aug 2022 15:06:28 +0200 Subject: [PATCH 4/4] update docs and wrap-up --- docs/advanced-usage/replaying-events.md | 6 ++++++ src/Console/ReplayCommand.php | 10 +++++----- src/Projectionist.php | 4 ++-- tests/Console/ReplayCommandTest.php | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/docs/advanced-usage/replaying-events.md b/docs/advanced-usage/replaying-events.md index 04c347d2..9793c248 100644 --- a/docs/advanced-usage/replaying-events.md +++ b/docs/advanced-usage/replaying-events.md @@ -35,6 +35,12 @@ If you are [using your own event storage model](/laravel-event-sourcing/v4/advan php artisan event-sourcing:replay --stored-event-model=App\\Models\\AccountStoredEvent ``` +If you only want to reply events for a specific aggregate only, you can use the `--aggregate-uuid` option. + +```bash +php artisan event-sourcing:replay --aggregate-uuid=12345678-1234-1234-1234-1234567890ab + ``` + ## Detecting event replays If your projector contains an `onStartingEventReplay` method, we'll call it right before the first event is replayed. diff --git a/src/Console/ReplayCommand.php b/src/Console/ReplayCommand.php index 263fb753..1ecd3a92 100644 --- a/src/Console/ReplayCommand.php +++ b/src/Console/ReplayCommand.php @@ -13,7 +13,7 @@ class ReplayCommand extends Command protected $signature = 'event-sourcing:replay {projector?*} {--from=0 : Replay events starting from this event number} {--stored-event-model= : Replay events from this store} - {--uuid= : Replay events for this uuid only}'; + {--aggregate-uuid= : Replay events for this aggregate only}'; protected $description = 'Replay stored events'; @@ -31,7 +31,7 @@ public function handle(Projectionist $projectionist): void return; } - $this->replay($projectors, (int)$this->option('from'), $this->option('uuid')); + $this->replay($projectors, (int)$this->option('from'), $this->option('aggregate-uuid')); } public function selectProjectors(array $projectorClassNames): ?Collection @@ -55,10 +55,10 @@ public function selectProjectors(array $projectorClassNames): ?Collection }); } - public function replay(Collection $projectors, int $startingFrom, ?string $uuid = null): void + public function replay(Collection $projectors, int $startingFrom, ?string $aggregateUuid = null): void { $repository = app(StoredEventRepository::class); - $replayCount = $repository->countAllStartingFrom($startingFrom, $uuid); + $replayCount = $repository->countAllStartingFrom($startingFrom, $aggregateUuid); if ($replayCount === 0) { $this->warn('There are no events to replay'); @@ -73,7 +73,7 @@ public function replay(Collection $projectors, int $startingFrom, ?string $uuid $bar->advance(); }; - $this->projectionist->replay($projectors, $startingFrom, $onEventReplayed, $uuid); + $this->projectionist->replay($projectors, $startingFrom, $onEventReplayed, $aggregateUuid); $bar->finish(); diff --git a/src/Projectionist.php b/src/Projectionist.php index bcaa57cd..45293808 100644 --- a/src/Projectionist.php +++ b/src/Projectionist.php @@ -320,7 +320,7 @@ public function replay( Collection $projectors, int $startingFromEventId = 0, callable $onEventReplayed = null, - ?string $uuid = null + ?string $aggregateUuid = null ): void { $projectors = new EventHandlerCollection($projectors); @@ -339,7 +339,7 @@ public function replay( $projectors->call('onStartingEventReplay'); app(StoredEventRepository::class) - ->retrieveAllStartingFrom($startingFromEventId, $uuid) + ->retrieveAllStartingFrom($startingFromEventId, $aggregateUuid) ->each(function (StoredEvent $storedEvent) use ($projectors, $onEventReplayed) { $this->applyStoredEventToProjectors( $storedEvent, diff --git a/tests/Console/ReplayCommandTest.php b/tests/Console/ReplayCommandTest.php index f0d61f0f..15bfe178 100644 --- a/tests/Console/ReplayCommandTest.php +++ b/tests/Console/ReplayCommandTest.php @@ -156,7 +156,7 @@ public function it_will_replay_events_for_a_specific_aggregate_root_uuid() $this->artisan('event-sourcing:replay', [ 'projector' => [AccountProjector::class], - '--uuid' => $uuid1 + '--aggregate-uuid' => $uuid1 ]) ->expectsOutput('Replaying 1 events...') ->assertExitCode(0);