Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Commands/ReplayCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event as EventFacade;
use Thunk\Verbs\Contracts\BrokersEvents;
use Thunk\Verbs\Contracts\StoresEvents;
use Thunk\Verbs\Event;
use Thunk\Verbs\Models\VerbEvent;

Expand All @@ -18,11 +19,11 @@

class ReplayCommand extends Command
{
protected $signature = 'verbs:replay {--force}';
protected $signature = 'verbs:replay {--force} {--reattach}';

protected $description = 'Replay all Verbs events.';

public function handle(BrokersEvents $broker): int
public function handle(BrokersEvents $broker, StoresEvents $event_store): int
{
if (! $this->confirmed() || ! $this->confirmedAgainIfProduction()) {
return 1;
Expand All @@ -39,12 +40,18 @@ public function handle(BrokersEvents $broker): int
$progress->start();

$broker->replay(
beforeEach: fn (Event $event) => $progress->label(sprintf(
'[%s] %s::%d',
date('i:s', time() - $started_at),
class_basename($event),
$event->id,
)),
beforeEach: function (Event $event) use ($event_store, $progress, $started_at) {
if ($this->option('reattach')) {
$event_store->reattach([$event]);
}

$progress->label(sprintf(
'[%s] %s::%d',
date('i:s', time() - $started_at),
class_basename($event),
$event->id,
));
},
afterEach: fn () => $progress->advance(),
);

Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/StoresEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public function read(

/** @param Event[] $events */
public function write(array $events): bool;

/** @param Event[] $events */
public function reattach(array $events): bool;
}
9 changes: 9 additions & 0 deletions src/Lifecycle/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function write(array $events): bool
&& VerbStateEvent::insert($this->formatRelationshipsForWrite($events));
}

public function reattach(array $events): bool
{
if (empty($events)) {
return true;
}

return VerbStateEvent::insertOrIgnore($this->formatRelationshipsForWrite($events));
}

protected function readEvents(
?State $state,
Bits|UuidInterface|AbstractUid|int|string|null $after_id,
Expand Down
5 changes: 5 additions & 0 deletions src/Testing/EventStoreFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function write(array $events): bool
return true;
}

public function reattach(array $events): bool
{
return true;
}

/** @return Collection<int, Event> */
public function committed(string $class_name, ?Closure $filter = null): Collection
{
Expand Down
36 changes: 36 additions & 0 deletions tests/Feature/ReplayCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Thunk\Verbs\Facades\Verbs;
use Thunk\Verbs\Lifecycle\StateManager;
use Thunk\Verbs\Models\VerbSnapshot;
use Thunk\Verbs\Models\VerbStateEvent;
use Thunk\Verbs\State;

beforeEach(function () {
Expand Down Expand Up @@ -136,6 +137,41 @@
expect($snapshot2->created_at)->toEqual(CarbonImmutable::parse('2024-05-15 18:00:00'));
});

it('can reattach events to states when replaying', function () {
Carbon::setTestNow('2024-04-01 12:00:00');

$state1_id = Id::make();
$state2_id = Id::make();

$state1_event1 = ReplayCommandTestEvent::fire(state_id: $state1_id);
$state2_event1 = ReplayCommandTestEvent::fire(state_id: $state2_id);
$state1_event2 = ReplayCommandTestEvent::fire(state_id: $state1_id);
$state2_event2 = ReplayCommandTestEvent::fire(state_id: $state2_id);

Verbs::commit();

expect(VerbStateEvent::count())->toBe(4);

$this->artisan(ReplayCommand::class);

expect(VerbStateEvent::count())->toBe(4);

VerbStateEvent::truncate();

expect(VerbStateEvent::count())->toBe(0);

$this->artisan(ReplayCommand::class);

expect(VerbStateEvent::count())->toBe(0);

$this->artisan(ReplayCommand::class, ['--reattach' => true]);

expect(VerbStateEvent::where(['state_id' => $state1_id, 'event_id' => $state1_event1->id])->count())->toBe(1)
->and(VerbStateEvent::where(['state_id' => $state1_id, 'event_id' => $state1_event2->id])->count())->toBe(1)
->and(VerbStateEvent::where(['state_id' => $state2_id, 'event_id' => $state2_event1->id])->count())->toBe(1)
->and(VerbStateEvent::where(['state_id' => $state2_id, 'event_id' => $state2_event2->id])->count())->toBe(1);
});

class ReplayCommandTestEvent extends Event
{
public function __construct(
Expand Down
Loading