1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmoothPhp\LaravelAdapter\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
7
|
|
|
use Illuminate\Database\QueryException; |
8
|
|
|
use Illuminate\Database\Schema\Blueprint; |
9
|
|
|
use Schema; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class BuildLaravelEventStore |
13
|
|
|
* @package SmoothPhp\LaravelAdapter\Console |
14
|
|
|
* @author Simon Bennett <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class BuildLaravelEventStore extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'smoothphp:buildeventstore {--force=false}'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Build the Laravel Event Store'; |
31
|
|
|
|
32
|
|
|
/** @var Repository */ |
33
|
|
|
private $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* BuildLaravelEventStore constructor. |
37
|
|
|
* @param Repository $config |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Repository $config) |
40
|
|
|
{ |
41
|
|
|
parent::__construct(); |
42
|
|
|
$this->config = $config; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the console command. |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function handle() |
51
|
|
|
{ |
52
|
|
|
if ($this->option('force') == 'true' || $this->confirm( |
53
|
|
|
"Are you sure you want to make a new table '{$this->config->get('cqrses.eventstore_table')}'" |
54
|
|
|
. " on connection '{$this->config->get('cqrses.eventstore_connection')}'" |
55
|
|
|
. " Do you wish to continue?" |
56
|
|
|
) |
57
|
|
|
) { |
58
|
|
|
try { |
59
|
|
|
return $this->buildEventStoreTable(); |
60
|
|
|
} catch (QueryException $ex) { |
61
|
|
|
$this->error("Error creating table :'{$this->config->get('cqrses.eventstore_table')}'"); |
62
|
|
|
$this->error($ex->getMessage()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$this->line("Stopping"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Build eventstore table |
70
|
|
|
*/ |
71
|
|
|
protected function buildEventStoreTable() |
72
|
|
|
{ |
73
|
|
|
Schema::connection($this->config->get('cqrses.eventstore_connection')) |
74
|
|
|
->create( |
75
|
|
|
$this->config->get('cqrses.eventstore_table'), |
76
|
|
|
function (Blueprint $table) { |
77
|
|
|
$table->increments('id'); |
78
|
|
|
$table->string('uuid', 56); |
79
|
|
|
$table->integer('playhead')->unsigned(); |
80
|
|
|
$table->text('metadata'); |
81
|
|
|
$table->longText('payload'); |
82
|
|
|
$table->timestamp('recorded_on')->nullable()->index(); |
83
|
|
|
$table->string('type', 255)->index(); |
84
|
|
|
$table->unique(['uuid', 'playhead']); |
85
|
|
|
|
86
|
|
|
$table->index(['id', 'type']); |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |