1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Configuration; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use DateTimeZone; |
10
|
|
|
use Doctrine\Common\EventArgs; |
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Doctrine\DBAL\Connections\MasterSlaveConnection; |
13
|
|
|
use Doctrine\DBAL\Schema\Column; |
14
|
|
|
use Doctrine\DBAL\Schema\Table; |
15
|
|
|
use Doctrine\DBAL\Types\Type; |
16
|
|
|
use Doctrine\Migrations\Configuration\Exception\MigrationsNamespaceRequired; |
17
|
|
|
use Doctrine\Migrations\Configuration\Exception\ParameterIncompatibleWithFinder; |
18
|
|
|
use Doctrine\Migrations\EventDispatcher; |
19
|
|
|
use Doctrine\Migrations\Exception\DuplicateMigrationVersion; |
20
|
|
|
use Doctrine\Migrations\Exception\MigrationClassNotFound; |
21
|
|
|
use Doctrine\Migrations\Exception\MigrationException; |
22
|
|
|
use Doctrine\Migrations\Exception\MigrationsDirectoryRequired; |
23
|
|
|
use Doctrine\Migrations\Exception\UnknownMigrationVersion; |
24
|
|
|
use Doctrine\Migrations\FileQueryWriter; |
25
|
|
|
use Doctrine\Migrations\Finder\MigrationDeepFinder; |
26
|
|
|
use Doctrine\Migrations\Finder\MigrationFinder; |
27
|
|
|
use Doctrine\Migrations\Finder\RecursiveRegexFinder; |
28
|
|
|
use Doctrine\Migrations\MigrationFileBuilder; |
29
|
|
|
use Doctrine\Migrations\OutputWriter; |
30
|
|
|
use Doctrine\Migrations\ParameterFormatter; |
31
|
|
|
use Doctrine\Migrations\Provider\LazySchemaDiffProvider; |
32
|
|
|
use Doctrine\Migrations\Provider\SchemaDiffProvider; |
33
|
|
|
use Doctrine\Migrations\Provider\SchemaDiffProviderInterface; |
34
|
|
|
use Doctrine\Migrations\QueryWriter; |
35
|
|
|
use Doctrine\Migrations\Version; |
36
|
|
|
use Doctrine\Migrations\VersionExecutor; |
37
|
|
|
use Doctrine\Migrations\VersionExecutorInterface; |
38
|
|
|
use const SORT_STRING; |
39
|
|
|
use function array_combine; |
40
|
|
|
use function array_keys; |
41
|
|
|
use function array_map; |
42
|
|
|
use function array_reverse; |
43
|
|
|
use function array_search; |
44
|
|
|
use function array_unshift; |
45
|
|
|
use function array_values; |
46
|
|
|
use function class_exists; |
47
|
|
|
use function count; |
48
|
|
|
use function end; |
49
|
|
|
use function get_class; |
50
|
|
|
use function implode; |
51
|
|
|
use function in_array; |
52
|
|
|
use function ksort; |
53
|
|
|
use function sprintf; |
54
|
|
|
use function str_replace; |
55
|
|
|
use function substr; |
56
|
|
|
|
57
|
|
|
class Configuration |
58
|
|
|
{ |
59
|
|
|
public const VERSIONS_ORGANIZATION_BY_YEAR = 'year'; |
60
|
|
|
|
61
|
|
|
public const VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH = 'year_and_month'; |
62
|
|
|
|
63
|
|
|
public const VERSION_FORMAT = 'YmdHis'; |
64
|
|
|
|
65
|
|
|
/** @var Connection */ |
66
|
|
|
private $connection; |
67
|
|
|
|
68
|
|
|
/** @var OutputWriter|null */ |
69
|
|
|
private $outputWriter; |
70
|
|
|
|
71
|
|
|
/** @var MigrationFinder */ |
72
|
|
|
private $migrationFinder; |
73
|
|
|
|
74
|
|
|
/** @var QueryWriter|null */ |
75
|
|
|
private $queryWriter; |
76
|
|
|
|
77
|
|
|
/** @var string|null */ |
78
|
|
|
private $name; |
79
|
|
|
|
80
|
|
|
/** @var bool */ |
81
|
|
|
private $migrationTableCreated = false; |
82
|
|
|
|
83
|
|
|
/** @var EventDispatcher|null */ |
84
|
|
|
private $eventDispatcher; |
85
|
|
|
|
86
|
|
|
/** @var SchemaDiffProviderInterface|null */ |
87
|
|
|
private $schemaDiffProvider; |
88
|
|
|
|
89
|
|
|
/** @var VersionExecutorInterface|null */ |
90
|
|
|
private $versionExecutor; |
91
|
|
|
|
92
|
|
|
/** @var MigrationFileBuilder|null */ |
93
|
|
|
private $migrationFileBuilder; |
94
|
|
|
|
95
|
|
|
/** @var ParameterFormatter|null */ |
96
|
|
|
private $parameterFormatter; |
97
|
|
|
|
98
|
|
|
/** @var string */ |
99
|
|
|
private $migrationsTableName = 'doctrine_migration_versions'; |
100
|
|
|
|
101
|
|
|
/** @var string */ |
102
|
|
|
private $migrationsColumnName = 'version'; |
103
|
|
|
|
104
|
|
|
/** @var string|null */ |
105
|
|
|
private $migrationsDirectory; |
106
|
|
|
|
107
|
|
|
/** @var string|null */ |
108
|
|
|
private $migrationsNamespace; |
109
|
|
|
|
110
|
|
|
/** @var Version[] */ |
111
|
|
|
private $migrations = []; |
112
|
|
|
|
113
|
|
|
/** @var bool */ |
114
|
|
|
private $migrationsAreOrganizedByYear = false; |
115
|
|
|
|
116
|
|
|
/** @var bool */ |
117
|
|
|
private $migrationsAreOrganizedByYearAndMonth = false; |
118
|
|
|
|
119
|
|
|
/** @var string|null */ |
120
|
|
|
private $customTemplate; |
121
|
|
|
|
122
|
|
|
/** @var bool */ |
123
|
|
|
private $isDryRun = false; |
124
|
|
|
|
125
|
259 |
|
public function __construct( |
126
|
|
|
Connection $connection, |
127
|
|
|
?OutputWriter $outputWriter = null, |
128
|
|
|
?MigrationFinder $migrationFinder = null, |
129
|
|
|
?QueryWriter $queryWriter = null |
130
|
|
|
) { |
131
|
259 |
|
$this->connection = $connection; |
132
|
259 |
|
$this->outputWriter = $outputWriter; |
133
|
259 |
|
$this->migrationFinder = $migrationFinder ?? new RecursiveRegexFinder(); |
134
|
259 |
|
$this->queryWriter = $queryWriter; |
135
|
259 |
|
} |
136
|
|
|
|
137
|
22 |
|
public function areMigrationsOrganizedByYear() : bool |
138
|
|
|
{ |
139
|
22 |
|
return $this->migrationsAreOrganizedByYear; |
140
|
|
|
} |
141
|
|
|
|
142
|
22 |
|
public function areMigrationsOrganizedByYearAndMonth() : bool |
143
|
|
|
{ |
144
|
22 |
|
return $this->migrationsAreOrganizedByYearAndMonth; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** @throws MigrationException */ |
148
|
130 |
|
public function validate() : void |
149
|
|
|
{ |
150
|
130 |
|
if ($this->migrationsNamespace === null) { |
151
|
1 |
|
throw MigrationsNamespaceRequired::new(); |
152
|
|
|
} |
153
|
|
|
|
154
|
129 |
|
if ($this->migrationsDirectory === null) { |
155
|
1 |
|
throw MigrationsDirectoryRequired::new(); |
156
|
|
|
} |
157
|
128 |
|
} |
158
|
|
|
|
159
|
63 |
|
public function setName(string $name) : void |
160
|
|
|
{ |
161
|
63 |
|
$this->name = $name; |
162
|
63 |
|
} |
163
|
|
|
|
164
|
16 |
|
public function getName() : ?string |
165
|
|
|
{ |
166
|
16 |
|
return $this->name; |
167
|
|
|
} |
168
|
|
|
|
169
|
5 |
|
public function setOutputWriter(OutputWriter $outputWriter) : void |
170
|
|
|
{ |
171
|
5 |
|
$this->outputWriter = $outputWriter; |
172
|
5 |
|
} |
173
|
|
|
|
174
|
123 |
|
public function getOutputWriter() : OutputWriter |
175
|
|
|
{ |
176
|
123 |
|
if ($this->outputWriter === null) { |
177
|
82 |
|
$this->outputWriter = new OutputWriter(); |
178
|
|
|
} |
179
|
|
|
|
180
|
123 |
|
return $this->outputWriter; |
181
|
|
|
} |
182
|
|
|
|
183
|
15 |
|
public function getDateTime(string $version) : string |
184
|
|
|
{ |
185
|
15 |
|
$datetime = str_replace('Version', '', $version); |
186
|
15 |
|
$datetime = DateTime::createFromFormat('YmdHis', $datetime); |
187
|
|
|
|
188
|
15 |
|
if ($datetime === false) { |
189
|
5 |
|
return ''; |
190
|
|
|
} |
191
|
|
|
|
192
|
11 |
|
return $datetime->format('Y-m-d H:i:s'); |
193
|
|
|
} |
194
|
|
|
|
195
|
126 |
|
public function getConnection() : Connection |
196
|
|
|
{ |
197
|
126 |
|
return $this->connection; |
198
|
|
|
} |
199
|
|
|
|
200
|
86 |
|
public function setMigrationsTableName(string $tableName) : void |
201
|
|
|
{ |
202
|
86 |
|
$this->migrationsTableName = $tableName; |
203
|
86 |
|
} |
204
|
|
|
|
205
|
61 |
|
public function getMigrationsTableName() : string |
206
|
|
|
{ |
207
|
61 |
|
return $this->migrationsTableName; |
208
|
|
|
} |
209
|
|
|
|
210
|
57 |
|
public function setMigrationsColumnName(string $columnName) : void |
211
|
|
|
{ |
212
|
57 |
|
$this->migrationsColumnName = $columnName; |
213
|
57 |
|
} |
214
|
|
|
|
215
|
14 |
|
public function getMigrationsColumnName() : string |
216
|
|
|
{ |
217
|
14 |
|
return $this->migrationsColumnName; |
218
|
|
|
} |
219
|
|
|
|
220
|
70 |
|
public function getQuotedMigrationsColumnName() : string |
221
|
|
|
{ |
222
|
70 |
|
return $this->getMigrationsColumn()->getQuotedName($this->connection->getDatabasePlatform()); |
223
|
|
|
} |
224
|
|
|
|
225
|
179 |
|
public function setMigrationsDirectory(string $migrationsDirectory) : void |
226
|
|
|
{ |
227
|
179 |
|
$this->migrationsDirectory = $migrationsDirectory; |
228
|
179 |
|
} |
229
|
|
|
|
230
|
19 |
|
public function getMigrationsDirectory() : ?string |
231
|
|
|
{ |
232
|
19 |
|
return $this->migrationsDirectory; |
233
|
|
|
} |
234
|
|
|
|
235
|
182 |
|
public function setMigrationsNamespace(string $migrationsNamespace) : void |
236
|
|
|
{ |
237
|
182 |
|
$this->migrationsNamespace = $migrationsNamespace; |
238
|
182 |
|
} |
239
|
|
|
|
240
|
93 |
|
public function getMigrationsNamespace() : ?string |
241
|
|
|
{ |
242
|
93 |
|
return $this->migrationsNamespace; |
243
|
|
|
} |
244
|
|
|
|
245
|
5 |
|
public function setCustomTemplate(?string $customTemplate) : void |
246
|
|
|
{ |
247
|
5 |
|
$this->customTemplate = $customTemplate; |
248
|
5 |
|
} |
249
|
|
|
|
250
|
9 |
|
public function getCustomTemplate() : ?string |
251
|
|
|
{ |
252
|
9 |
|
return $this->customTemplate; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** @throws MigrationException */ |
256
|
8 |
|
public function setMigrationsFinder(MigrationFinder $migrationFinder) : void |
257
|
|
|
{ |
258
|
8 |
|
if (($this->migrationsAreOrganizedByYear || $this->migrationsAreOrganizedByYearAndMonth) |
259
|
8 |
|
&& ! ($migrationFinder instanceof MigrationDeepFinder)) { |
260
|
4 |
|
throw ParameterIncompatibleWithFinder::new( |
261
|
4 |
|
'organize-migrations', |
262
|
4 |
|
$migrationFinder |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
4 |
|
$this->migrationFinder = $migrationFinder; |
267
|
4 |
|
} |
268
|
|
|
|
269
|
|
|
/** @return Version[] */ |
270
|
88 |
|
public function registerMigrationsFromDirectory(string $path) : array |
271
|
|
|
{ |
272
|
88 |
|
$this->validate(); |
273
|
|
|
|
274
|
88 |
|
return $this->registerMigrations($this->findMigrations($path)); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** @throws MigrationException */ |
278
|
71 |
|
public function registerMigration(string $version, string $class) : Version |
279
|
|
|
{ |
280
|
71 |
|
$this->ensureMigrationClassExists($class); |
281
|
|
|
|
282
|
70 |
|
if (isset($this->migrations[$version])) { |
283
|
1 |
|
throw DuplicateMigrationVersion::new( |
284
|
1 |
|
$version, |
285
|
1 |
|
get_class($this->migrations[$version]) |
286
|
|
|
); |
287
|
|
|
} |
288
|
|
|
|
289
|
70 |
|
$version = new Version( |
290
|
70 |
|
$this, |
291
|
70 |
|
$version, |
292
|
70 |
|
$class, |
293
|
70 |
|
$this->getVersionExecutor() |
294
|
|
|
); |
295
|
|
|
|
296
|
70 |
|
$this->migrations[$version->getVersion()] = $version; |
297
|
|
|
|
298
|
70 |
|
ksort($this->migrations, SORT_STRING); |
299
|
|
|
|
300
|
70 |
|
return $version; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param string[] $migrations |
305
|
|
|
* |
306
|
|
|
* @return Version[] |
307
|
|
|
*/ |
308
|
92 |
|
public function registerMigrations(array $migrations) : array |
309
|
|
|
{ |
310
|
92 |
|
$versions = []; |
311
|
|
|
|
312
|
92 |
|
foreach ($migrations as $version => $class) { |
313
|
35 |
|
$versions[] = $this->registerMigration((string) $version, $class); |
314
|
|
|
} |
315
|
|
|
|
316
|
91 |
|
return $versions; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return Version[] |
321
|
|
|
*/ |
322
|
35 |
|
public function getMigrations() : array |
323
|
|
|
{ |
324
|
35 |
|
return $this->migrations; |
325
|
|
|
} |
326
|
|
|
|
327
|
21 |
|
public function getVersion(string $version) : Version |
328
|
|
|
{ |
329
|
21 |
|
$this->loadMigrationsFromDirectory(); |
330
|
|
|
|
331
|
21 |
|
if (! isset($this->migrations[$version])) { |
332
|
2 |
|
throw UnknownMigrationVersion::new($version); |
333
|
|
|
} |
334
|
|
|
|
335
|
19 |
|
return $this->migrations[$version]; |
336
|
|
|
} |
337
|
|
|
|
338
|
20 |
|
public function hasVersion(string $version) : bool |
339
|
|
|
{ |
340
|
20 |
|
$this->loadMigrationsFromDirectory(); |
341
|
|
|
|
342
|
20 |
|
return isset($this->migrations[$version]); |
343
|
|
|
} |
344
|
|
|
|
345
|
21 |
|
public function hasVersionMigrated(Version $version) : bool |
346
|
|
|
{ |
347
|
21 |
|
$this->connect(); |
348
|
21 |
|
$this->createMigrationTable(); |
349
|
|
|
|
350
|
21 |
|
$version = $this->connection->fetchColumn( |
351
|
21 |
|
'SELECT ' . $this->getQuotedMigrationsColumnName() . ' FROM ' . $this->migrationsTableName . ' WHERE ' . $this->getQuotedMigrationsColumnName() . ' = ?', |
352
|
21 |
|
[$version->getVersion()] |
353
|
|
|
); |
354
|
|
|
|
355
|
21 |
|
return $version !== false; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** @return string[] */ |
359
|
42 |
|
public function getMigratedVersions() : array |
360
|
|
|
{ |
361
|
42 |
|
$this->createMigrationTable(); |
362
|
|
|
|
363
|
42 |
|
if (! $this->migrationTableCreated && $this->isDryRun) { |
364
|
1 |
|
return []; |
365
|
|
|
} |
366
|
|
|
|
367
|
42 |
|
$this->connect(); |
368
|
|
|
|
369
|
42 |
|
$sql = sprintf( |
370
|
42 |
|
'SELECT %s FROM %s', |
371
|
42 |
|
$this->getQuotedMigrationsColumnName(), |
372
|
42 |
|
$this->migrationsTableName |
373
|
|
|
); |
374
|
|
|
|
375
|
42 |
|
$result = $this->connection->fetchAll($sql); |
376
|
|
|
|
377
|
42 |
|
return array_map('current', $result); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** @return string[] */ |
381
|
14 |
|
public function getAvailableVersions() : array |
382
|
|
|
{ |
383
|
14 |
|
$availableVersions = []; |
384
|
|
|
|
385
|
14 |
|
$this->loadMigrationsFromDirectory(); |
386
|
|
|
|
387
|
14 |
|
foreach ($this->migrations as $migration) { |
388
|
14 |
|
$availableVersions[] = $migration->getVersion(); |
389
|
|
|
} |
390
|
|
|
|
391
|
14 |
|
return $availableVersions; |
392
|
|
|
} |
393
|
|
|
|
394
|
42 |
|
public function getCurrentVersion() : string |
395
|
|
|
{ |
396
|
42 |
|
$this->createMigrationTable(); |
397
|
|
|
|
398
|
42 |
|
if (! $this->migrationTableCreated && $this->isDryRun) { |
399
|
1 |
|
return '0'; |
400
|
|
|
} |
401
|
|
|
|
402
|
42 |
|
$this->connect(); |
403
|
|
|
|
404
|
42 |
|
$this->loadMigrationsFromDirectory(); |
405
|
|
|
|
406
|
42 |
|
$where = null; |
407
|
|
|
|
408
|
42 |
|
if (! empty($this->migrations)) { |
409
|
38 |
|
$migratedVersions = []; |
410
|
|
|
|
411
|
38 |
|
foreach ($this->migrations as $migration) { |
412
|
38 |
|
$migratedVersions[] = sprintf("'%s'", $migration->getVersion()); |
413
|
|
|
} |
414
|
|
|
|
415
|
38 |
|
$where = sprintf( |
416
|
38 |
|
' WHERE %s IN (%s)', |
417
|
38 |
|
$this->getQuotedMigrationsColumnName(), |
418
|
38 |
|
implode(', ', $migratedVersions) |
419
|
|
|
); |
420
|
|
|
} |
421
|
|
|
|
422
|
42 |
|
$sql = sprintf( |
423
|
42 |
|
'SELECT %s FROM %s%s ORDER BY %s DESC', |
424
|
42 |
|
$this->getQuotedMigrationsColumnName(), |
425
|
42 |
|
$this->migrationsTableName, |
426
|
42 |
|
$where, |
427
|
42 |
|
$this->getQuotedMigrationsColumnName() |
428
|
|
|
); |
429
|
|
|
|
430
|
42 |
|
$sql = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, 1); |
431
|
42 |
|
$result = $this->connection->fetchColumn($sql); |
432
|
|
|
|
433
|
42 |
|
return $result !== false ? (string) $result : '0'; |
434
|
|
|
} |
435
|
|
|
|
436
|
10 |
|
public function getPrevVersion() : ?string |
437
|
|
|
{ |
438
|
10 |
|
return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
439
|
|
|
} |
440
|
|
|
|
441
|
11 |
|
public function getNextVersion() : ?string |
442
|
|
|
{ |
443
|
11 |
|
return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
444
|
|
|
} |
445
|
|
|
|
446
|
15 |
|
public function getRelativeVersion(string $version, int $delta) : ?string |
447
|
|
|
{ |
448
|
15 |
|
$this->loadMigrationsFromDirectory(); |
449
|
|
|
|
450
|
15 |
|
$versions = array_map('strval', array_keys($this->migrations)); |
451
|
|
|
|
452
|
15 |
|
array_unshift($versions, '0'); |
453
|
|
|
|
454
|
15 |
|
$offset = array_search($version, $versions, true); |
455
|
|
|
|
456
|
15 |
|
if ($offset === false || ! isset($versions[$offset + $delta])) { |
457
|
|
|
// Unknown version or delta out of bounds. |
458
|
11 |
|
return null; |
459
|
|
|
} |
460
|
|
|
|
461
|
13 |
|
return $versions[$offset + $delta]; |
462
|
|
|
} |
463
|
|
|
|
464
|
1 |
|
public function getDeltaVersion(string $delta) : ?string |
465
|
|
|
{ |
466
|
1 |
|
$symbol = substr($delta, 0, 1); |
467
|
1 |
|
$number = (int) substr($delta, 1); |
468
|
|
|
|
469
|
1 |
|
if ($number <= 0) { |
470
|
|
|
return null; |
471
|
|
|
} |
472
|
|
|
|
473
|
1 |
|
if ($symbol === '+' || $symbol === '-') { |
474
|
1 |
|
return $this->getRelativeVersion($this->getCurrentVersion(), (int) $delta); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
return null; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Returns the version number from an alias. |
482
|
|
|
* |
483
|
|
|
* Supported aliases are: |
484
|
|
|
* |
485
|
|
|
* - first: The very first version before any migrations have been run. |
486
|
|
|
* - current: The current version. |
487
|
|
|
* - prev: The version prior to the current version. |
488
|
|
|
* - next: The version following the current version. |
489
|
|
|
* - latest: The latest available version. |
490
|
|
|
* |
491
|
|
|
* If an existing version number is specified, it is returned verbatimly. |
492
|
|
|
*/ |
493
|
9 |
|
public function resolveVersionAlias(string $alias) : ?string |
494
|
|
|
{ |
495
|
9 |
|
if ($this->hasVersion($alias)) { |
496
|
1 |
|
return $alias; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
switch ($alias) { |
500
|
9 |
|
case 'first': |
501
|
1 |
|
return '0'; |
502
|
|
|
|
503
|
9 |
|
case 'current': |
504
|
9 |
|
return $this->getCurrentVersion(); |
505
|
|
|
|
506
|
9 |
|
case 'prev': |
507
|
9 |
|
return $this->getPrevVersion(); |
508
|
|
|
|
509
|
9 |
|
case 'next': |
510
|
9 |
|
return $this->getNextVersion(); |
511
|
|
|
|
512
|
9 |
|
case 'latest': |
513
|
9 |
|
return $this->getLatestVersion(); |
514
|
|
|
|
515
|
|
|
default: |
516
|
1 |
|
if (substr($alias, 0, 7) === 'current') { |
517
|
|
|
return $this->getDeltaVersion(substr($alias, 7)); |
518
|
|
|
} |
519
|
|
|
|
520
|
1 |
|
return null; |
521
|
|
|
} |
522
|
|
|
} |
523
|
|
|
|
524
|
1 |
|
public function getNumberOfExecutedMigrations() : int |
525
|
|
|
{ |
526
|
1 |
|
$this->connect(); |
527
|
1 |
|
$this->createMigrationTable(); |
528
|
|
|
|
529
|
1 |
|
$sql = sprintf( |
530
|
1 |
|
'SELECT COUNT(%s) FROM %s', |
531
|
1 |
|
$this->getQuotedMigrationsColumnName(), |
532
|
1 |
|
$this->migrationsTableName |
533
|
|
|
); |
534
|
|
|
|
535
|
1 |
|
$result = $this->connection->fetchColumn($sql); |
536
|
|
|
|
537
|
1 |
|
return $result !== false ? (int) $result : 0; |
538
|
|
|
} |
539
|
|
|
|
540
|
3 |
|
public function getNumberOfAvailableMigrations() : int |
541
|
|
|
{ |
542
|
3 |
|
$this->loadMigrationsFromDirectory(); |
543
|
|
|
|
544
|
3 |
|
return count($this->migrations); |
545
|
|
|
} |
546
|
|
|
|
547
|
26 |
|
public function getLatestVersion() : string |
548
|
|
|
{ |
549
|
26 |
|
$this->loadMigrationsFromDirectory(); |
550
|
|
|
|
551
|
26 |
|
$versions = array_keys($this->migrations); |
552
|
26 |
|
$latest = end($versions); |
553
|
|
|
|
554
|
26 |
|
return $latest !== false ? (string) $latest : '0'; |
555
|
|
|
} |
556
|
|
|
|
557
|
64 |
|
public function createMigrationTable() : bool |
558
|
|
|
{ |
559
|
64 |
|
$this->validate(); |
560
|
|
|
|
561
|
64 |
|
if ($this->migrationTableCreated) { |
562
|
52 |
|
return false; |
563
|
|
|
} |
564
|
|
|
|
565
|
64 |
|
$this->connect(); |
566
|
|
|
|
567
|
64 |
|
if ($this->connection->getSchemaManager()->tablesExist([$this->migrationsTableName])) { |
568
|
4 |
|
$this->migrationTableCreated = true; |
569
|
|
|
|
570
|
4 |
|
return false; |
571
|
|
|
} |
572
|
|
|
|
573
|
63 |
|
if ($this->isDryRun) { |
574
|
1 |
|
return false; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
$columns = [ |
578
|
63 |
|
$this->migrationsColumnName => $this->getMigrationsColumn(), |
579
|
|
|
]; |
580
|
|
|
|
581
|
63 |
|
$table = new Table($this->migrationsTableName, $columns); |
582
|
63 |
|
$table->setPrimaryKey([$this->migrationsColumnName]); |
583
|
|
|
|
584
|
63 |
|
$this->connection->getSchemaManager()->createTable($table); |
585
|
|
|
|
586
|
63 |
|
$this->migrationTableCreated = true; |
587
|
|
|
|
588
|
63 |
|
return true; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** @return Version[] */ |
592
|
33 |
|
public function getMigrationsToExecute(string $direction, string $to) : array |
593
|
|
|
{ |
594
|
33 |
|
$this->loadMigrationsFromDirectory(); |
595
|
|
|
|
596
|
33 |
|
if ($direction === Version::DIRECTION_DOWN) { |
597
|
7 |
|
if (count($this->migrations) !== 0) { |
598
|
7 |
|
$allVersions = array_reverse(array_keys($this->migrations)); |
599
|
7 |
|
$classes = array_reverse(array_values($this->migrations)); |
600
|
7 |
|
$allVersions = array_combine($allVersions, $classes); |
601
|
|
|
} else { |
602
|
7 |
|
$allVersions = []; |
603
|
|
|
} |
604
|
|
|
} else { |
605
|
31 |
|
$allVersions = $this->migrations; |
606
|
|
|
} |
607
|
|
|
|
608
|
33 |
|
$versions = []; |
609
|
33 |
|
$migrated = $this->getMigratedVersions(); |
610
|
|
|
|
611
|
33 |
|
foreach ($allVersions as $version) { |
612
|
31 |
|
if (! $this->shouldExecuteMigration($direction, $version, $to, $migrated)) { |
613
|
18 |
|
continue; |
614
|
|
|
} |
615
|
|
|
|
616
|
28 |
|
$versions[$version->getVersion()] = $version; |
617
|
|
|
} |
618
|
|
|
|
619
|
33 |
|
return $versions; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* @return string[] |
624
|
|
|
*/ |
625
|
88 |
|
protected function findMigrations(string $path) : array |
626
|
|
|
{ |
627
|
88 |
|
return $this->migrationFinder->findMigrations($path, $this->getMigrationsNamespace()); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* @throws MigrationException |
632
|
|
|
*/ |
633
|
9 |
|
public function setMigrationsAreOrganizedByYear(bool $migrationsAreOrganizedByYear = true) : void |
634
|
|
|
{ |
635
|
9 |
|
$this->ensureOrganizeMigrationsIsCompatibleWithFinder(); |
636
|
|
|
|
637
|
5 |
|
$this->migrationsAreOrganizedByYear = $migrationsAreOrganizedByYear; |
638
|
5 |
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @throws MigrationException |
642
|
|
|
*/ |
643
|
10 |
|
public function setMigrationsAreOrganizedByYearAndMonth(bool $migrationsAreOrganizedByYearAndMonth = true) : void |
644
|
|
|
{ |
645
|
10 |
|
$this->ensureOrganizeMigrationsIsCompatibleWithFinder(); |
646
|
|
|
|
647
|
10 |
|
$this->migrationsAreOrganizedByYear = $migrationsAreOrganizedByYearAndMonth; |
648
|
10 |
|
$this->migrationsAreOrganizedByYearAndMonth = $migrationsAreOrganizedByYearAndMonth; |
649
|
10 |
|
} |
650
|
|
|
|
651
|
9 |
|
public function generateVersionNumber(?DateTimeInterface $now = null) : string |
652
|
|
|
{ |
653
|
9 |
|
$now = $now ?: new DateTime('now', new DateTimeZone('UTC')); |
654
|
|
|
|
655
|
9 |
|
return $now->format(self::VERSION_FORMAT); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Explicitely opens the database connection. This is done to play nice |
660
|
|
|
* with DBAL's MasterSlaveConnection. Which, in some cases, connects to a |
661
|
|
|
* follower when fetching the executed migrations. If a follower is lagging |
662
|
|
|
* significantly behind that means the migrations system may see unexecuted |
663
|
|
|
* migrations that were actually executed earlier. |
664
|
|
|
*/ |
665
|
64 |
|
protected function connect() : bool |
666
|
|
|
{ |
667
|
64 |
|
if ($this->connection instanceof MasterSlaveConnection) { |
668
|
1 |
|
return $this->connection->connect('master'); |
669
|
|
|
} |
670
|
|
|
|
671
|
63 |
|
return $this->connection->connect(); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* @throws MigrationException |
676
|
|
|
*/ |
677
|
19 |
|
private function ensureOrganizeMigrationsIsCompatibleWithFinder() : void |
678
|
|
|
{ |
679
|
19 |
|
if (! ($this->migrationFinder instanceof MigrationDeepFinder)) { |
680
|
4 |
|
throw ParameterIncompatibleWithFinder::new( |
681
|
4 |
|
'organize-migrations', |
682
|
4 |
|
$this->migrationFinder |
683
|
|
|
); |
684
|
|
|
} |
685
|
15 |
|
} |
686
|
|
|
|
687
|
|
|
/** @param string[] $migrated */ |
688
|
31 |
|
private function shouldExecuteMigration( |
689
|
|
|
string $direction, |
690
|
|
|
Version $version, |
691
|
|
|
string $to, |
692
|
|
|
array $migrated |
693
|
|
|
) : bool { |
694
|
31 |
|
$to = (int) $to; |
695
|
|
|
|
696
|
31 |
|
if ($direction === Version::DIRECTION_DOWN) { |
697
|
7 |
|
if (! in_array($version->getVersion(), $migrated, true)) { |
698
|
4 |
|
return false; |
699
|
|
|
} |
700
|
|
|
|
701
|
5 |
|
return $version->getVersion() > $to; |
702
|
|
|
} |
703
|
|
|
|
704
|
29 |
|
if ($direction === Version::DIRECTION_UP) { |
705
|
29 |
|
if (in_array($version->getVersion(), $migrated, true)) { |
706
|
8 |
|
return false; |
707
|
|
|
} |
708
|
|
|
|
709
|
28 |
|
return $version->getVersion() <= $to; |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
return false; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** @throws MigrationException */ |
716
|
71 |
|
private function ensureMigrationClassExists(string $class) : void |
717
|
|
|
{ |
718
|
71 |
|
if (! class_exists($class)) { |
719
|
1 |
|
throw MigrationClassNotFound::new( |
720
|
1 |
|
$class, |
721
|
1 |
|
$this->getMigrationsNamespace() |
722
|
|
|
); |
723
|
|
|
} |
724
|
70 |
|
} |
725
|
|
|
|
726
|
8 |
|
public function getQueryWriter() : QueryWriter |
727
|
|
|
{ |
728
|
8 |
|
if ($this->queryWriter === null) { |
729
|
7 |
|
$this->queryWriter = new FileQueryWriter( |
730
|
7 |
|
$this->getOutputWriter(), |
731
|
7 |
|
$this->getMigrationFileBuilder() |
732
|
|
|
); |
733
|
|
|
} |
734
|
|
|
|
735
|
8 |
|
return $this->queryWriter; |
736
|
|
|
} |
737
|
|
|
|
738
|
26 |
|
public function dispatchMigrationEvent(string $eventName, string $direction, bool $dryRun) : void |
739
|
|
|
{ |
740
|
26 |
|
$this->getEventDispatcher()->dispatchMigrationEvent($eventName, $direction, $dryRun); |
741
|
26 |
|
} |
742
|
|
|
|
743
|
51 |
|
public function dispatchVersionEvent( |
744
|
|
|
Version $version, |
745
|
|
|
string $eventName, |
746
|
|
|
string $direction, |
747
|
|
|
bool $dryRun |
748
|
|
|
) : void { |
749
|
51 |
|
$this->getEventDispatcher()->dispatchVersionEvent( |
750
|
51 |
|
$version, |
751
|
51 |
|
$eventName, |
752
|
51 |
|
$direction, |
753
|
51 |
|
$dryRun |
754
|
|
|
); |
755
|
51 |
|
} |
756
|
|
|
|
757
|
1 |
|
public function dispatchEvent(string $eventName, ?EventArgs $args = null) : void |
758
|
|
|
{ |
759
|
1 |
|
$this->getEventDispatcher()->dispatchEvent($eventName, $args); |
760
|
1 |
|
} |
761
|
|
|
|
762
|
2 |
|
public function setIsDryRun(bool $isDryRun) : void |
763
|
|
|
{ |
764
|
2 |
|
$this->isDryRun = $isDryRun; |
765
|
2 |
|
} |
766
|
|
|
|
767
|
70 |
|
private function loadMigrationsFromDirectory() : void |
768
|
|
|
{ |
769
|
70 |
|
if (count($this->migrations) !== 0 || $this->migrationsDirectory === null) { |
770
|
49 |
|
return; |
771
|
|
|
} |
772
|
|
|
|
773
|
32 |
|
$this->registerMigrationsFromDirectory($this->migrationsDirectory); |
774
|
32 |
|
} |
775
|
|
|
|
776
|
52 |
|
private function getEventDispatcher() : EventDispatcher |
777
|
|
|
{ |
778
|
52 |
|
if ($this->eventDispatcher === null) { |
779
|
52 |
|
$this->eventDispatcher = new EventDispatcher($this, $this->connection->getEventManager()); |
780
|
|
|
} |
781
|
|
|
|
782
|
52 |
|
return $this->eventDispatcher; |
783
|
|
|
} |
784
|
|
|
|
785
|
70 |
|
private function getSchemaDiffProvider() : SchemaDiffProviderInterface |
786
|
|
|
{ |
787
|
70 |
|
if ($this->schemaDiffProvider === null) { |
788
|
70 |
|
$this->schemaDiffProvider = LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration( |
789
|
70 |
|
new SchemaDiffProvider( |
790
|
70 |
|
$this->connection->getSchemaManager(), |
791
|
70 |
|
$this->connection->getDatabasePlatform() |
792
|
|
|
) |
793
|
|
|
); |
794
|
|
|
} |
795
|
|
|
|
796
|
70 |
|
return $this->schemaDiffProvider; |
797
|
|
|
} |
798
|
|
|
|
799
|
70 |
|
private function getVersionExecutor() : VersionExecutorInterface |
800
|
|
|
{ |
801
|
70 |
|
if ($this->versionExecutor === null) { |
802
|
70 |
|
$this->versionExecutor = new VersionExecutor( |
803
|
70 |
|
$this, |
804
|
70 |
|
$this->connection, |
805
|
70 |
|
$this->getSchemaDiffProvider(), |
806
|
70 |
|
$this->getOutputWriter(), |
807
|
70 |
|
$this->getParameterFormatter() |
808
|
|
|
); |
809
|
|
|
} |
810
|
|
|
|
811
|
70 |
|
return $this->versionExecutor; |
812
|
|
|
} |
813
|
|
|
|
814
|
7 |
|
private function getMigrationFileBuilder() : MigrationFileBuilder |
815
|
|
|
{ |
816
|
7 |
|
if ($this->migrationFileBuilder === null) { |
817
|
7 |
|
$this->migrationFileBuilder = new MigrationFileBuilder( |
818
|
7 |
|
$this->migrationsTableName, |
819
|
7 |
|
$this->getQuotedMigrationsColumnName() |
820
|
|
|
); |
821
|
|
|
} |
822
|
|
|
|
823
|
7 |
|
return $this->migrationFileBuilder; |
824
|
|
|
} |
825
|
|
|
|
826
|
70 |
|
private function getParameterFormatter() : ParameterFormatter |
827
|
|
|
{ |
828
|
70 |
|
if ($this->parameterFormatter === null) { |
829
|
70 |
|
$this->parameterFormatter = new ParameterFormatter($this->connection); |
830
|
|
|
} |
831
|
|
|
|
832
|
70 |
|
return $this->parameterFormatter; |
833
|
|
|
} |
834
|
|
|
|
835
|
70 |
|
private function getMigrationsColumn() : Column |
836
|
|
|
{ |
837
|
70 |
|
return new Column( |
838
|
70 |
|
$this->migrationsColumnName, |
839
|
70 |
|
Type::getType('string'), |
840
|
70 |
|
['length' => 255] |
841
|
|
|
); |
842
|
|
|
} |
843
|
|
|
} |
844
|
|
|
|