Skip to content

Commit

Permalink
Merge pull request #17 from digitalrisks/reset
Browse files Browse the repository at this point in the history
Add command to reset
  • Loading branch information
Kani Robinson authored Nov 4, 2019
2 parents 9a73727 + 4e10e76 commit 725b153
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Console/Commands/EventStoreReset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace DigitalRisks\LaravelEventStore\Console\Commands;

use DigitalRisks\LaravelEventStore\Client;
use Illuminate\Console\Command;
use GuzzleHttp\Exception\ClientException;

class EventStoreReset extends Command
{
protected $signature = 'eventstore:reset';

protected $description = 'Wipe the database and recreate the persistent subscriptions.';
protected $client;

public function __construct(Client $client)
{
$this->client = $client;

parent::__construct();
}

public function handle()
{
if (! $this->confirm('Please stop all workers first. Continue?')) return;

$this->call('migrate:fresh');

$streams = collect(config('eventstore.subscription_streams'));

$streams->map([$this, 'deleteSubscription']);
$streams->map([$this, 'createSubscription']);
}

public function deleteSubscription($stream)
{
$name = config('eventstore.group');

try {
$this->client->delete("/subscriptions/{$stream}/{$name}");
}
catch (ClientException $e) {
throw_if($e->getCode() !== 404, $e);
}
}

public function createSubscription($stream)
{
$name = config('eventstore.group');

$this->client->put("/subscriptions/{$stream}/{$name}");
}
}
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DigitalRisks\LaravelEventStore;

use DigitalRisks\LaravelEventStore\Console\Commands\EventStoreReset;
use DigitalRisks\LaravelEventStore\Console\Commands\EventStoreWorker;
use DigitalRisks\LaravelEventStore\Contracts\ShouldBeStored;
use DigitalRisks\LaravelEventStore\Listeners\SendToEventStoreListener;
Expand All @@ -22,6 +23,7 @@ public function boot()

$this->commands([
EventStoreWorker::class,
EventStoreReset::class,
]);
}

Expand Down

0 comments on commit 725b153

Please sign in to comment.