1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Migration\Version; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
6
|
|
|
use Bdf\Prime\Exception\PrimeException; |
7
|
|
|
use Bdf\Prime\Migration\VersionRepositoryInterface; |
8
|
|
|
use Bdf\Prime\Schema\Manager\TableManagerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* DbVersionRepository |
12
|
|
|
* |
13
|
|
|
* Contains the version of all upgraded migration |
14
|
|
|
*/ |
15
|
|
|
class DbVersionRepository implements VersionRepositoryInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The db connection |
19
|
|
|
* |
20
|
|
|
* @var ConnectionInterface&\Doctrine\DBAL\Connection |
21
|
|
|
*/ |
22
|
|
|
private $connection; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The table name |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $tableName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check whether the table exists |
33
|
|
|
* |
34
|
|
|
* @var boolean |
35
|
|
|
* |
36
|
|
|
* @internal |
37
|
|
|
*/ |
38
|
|
|
private $hasSchema; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Cache of version |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
* |
45
|
|
|
* @internal |
46
|
|
|
*/ |
47
|
|
|
private $cached; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor |
51
|
|
|
* |
52
|
|
|
* @param ConnectionInterface&\Doctrine\DBAL\Connection $connection |
53
|
|
|
* @param string $tableName |
54
|
|
|
*/ |
55
|
49 |
|
public function __construct(ConnectionInterface $connection, string $tableName) |
56
|
|
|
{ |
57
|
49 |
|
$this->connection = $connection; |
58
|
49 |
|
$this->tableName = $tableName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the connection |
63
|
|
|
* |
64
|
|
|
* @return ConnectionInterface&\Doctrine\DBAL\Connection |
65
|
|
|
*/ |
66
|
1 |
|
public function getConnection(): ConnectionInterface |
67
|
|
|
{ |
68
|
1 |
|
return $this->connection; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the table name |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
public function getTable(): string |
77
|
|
|
{ |
78
|
1 |
|
return $this->tableName; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
3 |
|
public function newIdentifier(): string |
85
|
|
|
{ |
86
|
3 |
|
return date('YmdHis'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
30 |
|
public function has(string $version): bool |
93
|
|
|
{ |
94
|
30 |
|
return in_array($version, $this->all()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
12 |
|
public function current(): string |
101
|
|
|
{ |
102
|
12 |
|
$versions = $this->all(); |
103
|
|
|
|
104
|
12 |
|
return (string)end($versions); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
*/ |
110
|
37 |
|
public function all(): array |
111
|
|
|
{ |
112
|
37 |
|
if ($this->cached !== null) { |
113
|
28 |
|
return $this->cached; |
114
|
|
|
} |
115
|
|
|
|
116
|
37 |
|
$this->prepare(); |
117
|
|
|
|
118
|
|
|
/** @psalm-suppress UndefinedInterfaceMethod */ |
119
|
37 |
|
return $this->cached = $this->connection->from($this->tableName)->order('version')->inRows('version'); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
16 |
|
public function add(string $version) |
126
|
|
|
{ |
127
|
16 |
|
$this->prepare(); |
128
|
|
|
|
129
|
16 |
|
$this->connection->insert($this->tableName, [ |
130
|
16 |
|
'version' => $version, |
131
|
16 |
|
]); |
132
|
|
|
|
133
|
16 |
|
$this->cached = null; |
134
|
|
|
|
135
|
16 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritDoc} |
140
|
|
|
*/ |
141
|
12 |
|
public function remove(string $version) |
142
|
|
|
{ |
143
|
12 |
|
$this->prepare(); |
144
|
|
|
|
145
|
12 |
|
$this->connection->delete($this->tableName, [ |
146
|
12 |
|
'version' => $version, |
147
|
12 |
|
]); |
148
|
|
|
|
149
|
12 |
|
$this->cached = null; |
150
|
|
|
|
151
|
12 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Prepare the repository |
156
|
|
|
* @throws PrimeException |
157
|
|
|
*/ |
158
|
37 |
|
public function prepare(): void |
159
|
|
|
{ |
160
|
37 |
|
if (!$this->hasSchema()) { |
161
|
|
|
$this->createSchema(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Is the schema ready? |
167
|
|
|
* |
168
|
|
|
* @return boolean |
169
|
|
|
* @throws PrimeException |
170
|
|
|
*/ |
171
|
41 |
|
public function hasSchema(): bool |
172
|
|
|
{ |
173
|
41 |
|
if (null === $this->hasSchema) { |
174
|
41 |
|
$this->hasSchema = $this->connection->schema()->has($this->tableName); |
175
|
|
|
} |
176
|
|
|
|
177
|
41 |
|
return $this->hasSchema; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Create Schema |
182
|
|
|
* |
183
|
|
|
* @return $this |
184
|
|
|
* @throws PrimeException |
185
|
|
|
*/ |
186
|
1 |
|
public function createSchema() |
187
|
|
|
{ |
188
|
1 |
|
$schemaManager = $this->connection->schema(); |
189
|
|
|
|
190
|
1 |
|
if (!$schemaManager instanceof TableManagerInterface) { |
191
|
|
|
throw new \LogicException('The connection "' . $this->connection->getName() . '" do not supports table configuration'); |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
$schemaManager->table($this->tableName, function ($table) { |
195
|
1 |
|
$table->string('version'); |
196
|
1 |
|
}); |
197
|
|
|
|
198
|
1 |
|
$this->hasSchema = true; |
199
|
|
|
|
200
|
1 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|