|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/elasticsearch7-adapter project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Daikon\Elasticsearch7\Migration; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Dbal\Connector\ConnectorInterface; |
|
12
|
|
|
use Daikon\Dbal\Migration\MigrationAdapterInterface; |
|
13
|
|
|
use Daikon\Dbal\Migration\MigrationList; |
|
14
|
|
|
use Daikon\Elasticsearch7\Connector\Elasticsearch7Connector; |
|
15
|
|
|
use DateTimeImmutable; |
|
16
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
|
17
|
|
|
|
|
18
|
|
|
final class Elasticsearch7MigrationAdapter implements MigrationAdapterInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private Elasticsearch7Connector $connector; |
|
21
|
|
|
|
|
22
|
|
|
private array $settings; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Elasticsearch7Connector $connector, array $settings = []) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->connector = $connector; |
|
27
|
|
|
$this->settings = $settings; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function read(string $identifier): MigrationList |
|
31
|
|
|
{ |
|
32
|
|
|
$client = $this->connector->getConnection(); |
|
33
|
|
|
|
|
34
|
|
|
try { |
|
35
|
|
|
$result = $client->get([ |
|
36
|
|
|
'index' => $this->getIndex(), |
|
37
|
|
|
'id' => $identifier |
|
38
|
|
|
]); |
|
39
|
|
|
} catch (Missing404Exception $error) { |
|
40
|
|
|
return new MigrationList; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->createMigrationList($result['_source']['migrations']); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function write(string $identifier, MigrationList $migrationList): void |
|
47
|
|
|
{ |
|
48
|
|
|
if ($migrationList->isEmpty()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$client = $this->connector->getConnection(); |
|
53
|
|
|
$client->index([ |
|
54
|
|
|
'index' => $this->getIndex(), |
|
55
|
|
|
'id' => $identifier, |
|
56
|
|
|
'body' => [ |
|
57
|
|
|
'target' => $identifier, |
|
58
|
|
|
'migrations' => $migrationList->toNative() |
|
59
|
|
|
] |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getConnector(): ConnectorInterface |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->connector; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function createMigrationList(array $migrationData): MigrationList |
|
69
|
|
|
{ |
|
70
|
|
|
$migrations = []; |
|
71
|
|
|
foreach ($migrationData as $migration) { |
|
72
|
|
|
$migrationClass = $migration['@type']; |
|
73
|
|
|
$migrations[] = new $migrationClass(new DateTimeImmutable($migration['executedAt'])); |
|
74
|
|
|
} |
|
75
|
|
|
return (new MigrationList($migrations))->sortByVersion(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function getIndex(): string |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->settings['index'] ?? $this->connector->getSettings()['index']; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|