-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathBakeMigrationDiffCommand.php
More file actions
647 lines (567 loc) · 23.6 KB
/
BakeMigrationDiffCommand.php
File metadata and controls
647 lines (567 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Migrations\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Database\Connection;
use Cake\Database\Schema\CachedCollection;
use Cake\Database\Schema\CheckConstraint;
use Cake\Database\Schema\CollectionInterface;
use Cake\Database\Schema\Column;
use Cake\Database\Schema\Constraint;
use Cake\Database\Schema\ForeignKey;
use Cake\Database\Schema\Index;
use Cake\Database\Schema\TableSchema;
use Cake\Database\Schema\UniqueKey;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Migrations\Migration\ManagerFactory;
use Migrations\Util\TableFinder;
use Migrations\Util\UtilTrait;
/**
* Task class for generating migration diff files.
*/
class BakeMigrationDiffCommand extends BakeSimpleMigrationCommand
{
use SnapshotTrait;
use UtilTrait;
/**
* Array of migrations that have already been migrated
*
* @var array
*/
protected array $migratedItems = [];
/**
* Path to the migration files
*
* @var string
*/
protected string $migrationsPath;
/**
* Migration files that are stored in the self::migrationsPath
*
* @var array
*/
protected array $migrationsFiles = [];
/**
* Name of the phinx log table
*
* @var string
*/
protected string $phinxTable;
/**
* List the tables the connection currently holds
*
* @var array<string>
*/
protected array $tables = [];
/**
* Array of \Cake\Database\Schema\TableSchemaInterface objects from the dump file which
* represents the state of the database after the last migrate / rollback command
*
* @var array<string, \Cake\Database\Schema\TableSchemaInterface>
*/
protected array $dumpSchema;
/**
* Array of \Cake\Database\Schema\TableSchemaInterface objects from the current state of the database
*
* @var array<string, \Cake\Database\Schema\TableSchemaInterface>
*/
protected array $currentSchema;
/**
* List of the tables that are commonly found in the dump schema and the current schema
*
* @var array<string, \Cake\Database\Schema\TableSchemaInterface>
*/
protected array $commonTables;
/**
* @var array<string, array>
*/
protected array $templateData = [];
/**
* @inheritDoc
*/
public static function defaultName(): string
{
return 'bake migration_diff';
}
/**
* @inheritDoc
*/
public function bake(string $name, Arguments $args, ConsoleIo $io): void
{
$this->setup($args);
if (!$this->checkSync()) {
$io->abort('Your migrations history is not in sync with your migrations files. ' .
'Make sure all your migrations have been migrated before baking a diff.');
}
if (!$this->migrationsFiles && !$this->migratedItems) {
$this->bakeSnapshot($name, $args, $io);
}
$collection = $this->getCollection($this->connection);
$connection = ConnectionManager::get($this->connection);
assert($connection instanceof Connection);
EventManager::instance()->on('Bake.initialize', function (Event $event) use ($collection, $connection): void {
/** @var \Bake\View\BakeView $view */
$view = $event->getSubject();
$view->loadHelper('Migrations.Migration', [
'collection' => $collection,
'connection' => $connection,
]);
});
parent::bake($name, $args, $io);
}
/**
* Sets up everything the baking process needs
*
* @param \Cake\Console\Arguments $args The command arguments.
* @return void
*/
protected function setup(Arguments $args): void
{
$this->migrationsPath = $this->getPath($args);
$this->migrationsFiles = glob($this->migrationsPath . '*.php') ?: [];
$this->phinxTable = $this->getPhinxTable($this->plugin);
$connection = ConnectionManager::get($this->connection);
assert($connection instanceof Connection);
$this->tables = $connection->getSchemaCollection()->listTables();
$tableExists = in_array($this->phinxTable, $this->tables, true);
$migratedItems = [];
if ($tableExists) {
$query = $connection->selectQuery();
/** @var array $migratedItems */
$migratedItems = $query
->select(['version'])
->from($this->phinxTable)
->orderBy(['version DESC'])
->execute()->fetchAll('assoc');
}
$this->migratedItems = $migratedItems;
}
/**
* Get a collection from a database.
*
* @param string $connection Database connection name.
* @return \Cake\Database\Schema\CollectionInterface
*/
public function getCollection(string $connection): CollectionInterface
{
$connection = ConnectionManager::get($connection);
assert($connection instanceof Connection);
return $connection->getSchemaCollection();
}
/**
* @inheritDoc
*/
public function templateData(Arguments $arguments): array
{
$this->dumpSchema = $this->getDumpSchema($arguments);
$this->currentSchema = $this->getCurrentSchema();
$this->commonTables = array_intersect_key($this->currentSchema, $this->dumpSchema);
$this->calculateDiff();
return [
'data' => $this->templateData,
'dumpSchema' => $this->dumpSchema,
'currentSchema' => $this->currentSchema,
];
}
/**
* This method runs the various methods needed to calculate a diff between the current
* state of the database and the schema dump file.
*
* @return void
*/
protected function calculateDiff(): void
{
$this->getConstraints();
$this->getIndexes();
$this->getColumns();
$this->getTables();
}
/**
* Calculate the diff between the current state of the database and the schema dump
* by returning an array containing the full \Cake\Database\Schema\TableSchemaInterface definitions
* of tables to be created and removed in the diff file.
*
* The method directly sets the diff in a property of the class.
*
* @return void
*/
protected function getTables(): void
{
$this->templateData['fullTables'] = [
'add' => array_diff_key($this->currentSchema, $this->dumpSchema),
'remove' => array_diff_key($this->dumpSchema, $this->currentSchema),
];
}
/**
* Calculate the diff between columns in existing tables.
* This will look for columns addition, columns removal and changes in columns metadata
* such as change of types or property such as length.
*
* Note that the method is not able to detect columns name change.
* The method directly sets the diff in a property of the class.
*
* @return void
*/
protected function getColumns(): void
{
foreach ($this->commonTables as $table => $currentSchema) {
$currentColumns = $currentSchema->columns();
$oldColumns = $this->dumpSchema[$table]->columns();
// brand new columns
$addedColumns = array_diff($currentColumns, $oldColumns);
foreach ($addedColumns as $columnName) {
$column = $currentSchema->getColumn($columnName);
/** @var int $key */
$key = array_search($columnName, $currentColumns);
if ($key > 0) {
$column['after'] = $currentColumns[$key - 1];
}
if (isset($column['unsigned'])) {
$column['signed'] = !$column['unsigned'];
unset($column['unsigned']);
}
$this->templateData[$table]['columns']['add'][$columnName] = $column;
}
// changes in columns meta-data
foreach ($currentColumns as $columnName) {
$column = $currentSchema->getColumn($columnName);
$oldColumn = $this->dumpSchema[$table]->getColumn($columnName);
unset(
$column['collate'],
$column['fixed'],
$oldColumn['collate'],
$oldColumn['fixed'],
);
if (
in_array($columnName, $oldColumns, true) &&
$column !== $oldColumn
) {
$changedAttributes = array_diff_assoc($column, $oldColumn);
foreach (['type', 'length', 'null', 'default'] as $attribute) {
$phinxAttributeName = $attribute;
if ($attribute === 'length') {
$phinxAttributeName = 'limit';
}
if (!isset($changedAttributes[$phinxAttributeName])) {
$changedAttributes[$phinxAttributeName] = $column[$attribute];
}
}
// Only convert unsigned to signed if it actually changed
if (isset($changedAttributes['unsigned'])) {
$changedAttributes['signed'] = !$changedAttributes['unsigned'];
unset($changedAttributes['unsigned']);
}
// For decimal columns, handle CakePHP schema -> migration attribute mapping
if ($column['type'] === 'decimal') {
// In CakePHP schema: 'length' = precision, 'precision' = scale
// In migrations: 'precision' = precision, 'scale' = scale
// Convert CakePHP schema's 'precision' (which is scale) to migration's 'scale'
if (isset($changedAttributes['precision'])) {
$changedAttributes['scale'] = $changedAttributes['precision'];
unset($changedAttributes['precision']);
}
// Convert CakePHP schema's 'length' (which is precision) to migration's 'precision'
if (isset($changedAttributes['length'])) {
$changedAttributes['precision'] = $changedAttributes['length'];
unset($changedAttributes['length']);
}
// Ensure both precision and scale are always set for decimal columns
if (!isset($changedAttributes['precision']) && isset($column['length'])) {
$changedAttributes['precision'] = $column['length'];
}
if (!isset($changedAttributes['scale']) && isset($column['precision'])) {
$changedAttributes['scale'] = $column['precision'];
}
// Remove 'limit' for decimal columns as they use precision/scale instead
unset($changedAttributes['limit']);
} elseif (isset($changedAttributes['length'])) {
// For non-decimal columns, convert 'length' to 'limit'
if (!isset($changedAttributes['limit'])) {
$changedAttributes['limit'] = $changedAttributes['length'];
}
unset($changedAttributes['length']);
}
$this->templateData[$table]['columns']['changed'][$columnName] = $changedAttributes;
}
}
// columns deletion
if (!isset($this->templateData[$table]['columns']['remove'])) {
$this->templateData[$table]['columns']['remove'] = [];
}
$removedColumns = array_diff($oldColumns, $currentColumns);
if ($removedColumns) {
foreach ($removedColumns as $columnName) {
$column = $this->dumpSchema[$table]->getColumn($columnName);
/** @var int $key */
$key = array_search($columnName, $oldColumns);
if ($key > 0) {
$column['after'] = $oldColumns[$key - 1];
}
$this->templateData[$table]['columns']['remove'][$columnName] = $column;
}
}
}
}
/**
* Calculate the diff between constraints in existing tables.
* This will look for constraints addition, constraints removal and changes in constraints metadata
* such as change of referenced columns if the old constraints and the new one have the same name.
*
* The method directly sets the diff in a property of the class.
*
* @return void
*/
protected function getConstraints(): void
{
foreach ($this->commonTables as $table => $currentSchema) {
$currentConstraints = $currentSchema->constraints();
$oldConstraints = $this->dumpSchema[$table]->constraints();
// brand new constraints
$addedConstraints = array_diff($currentConstraints, $oldConstraints);
foreach ($addedConstraints as $constraintName) {
$this->templateData[$table]['constraints']['add'][$constraintName] =
$currentSchema->getConstraint($constraintName);
$constraint = $currentSchema->getConstraint($constraintName);
if ($constraint['type'] === TableSchema::CONSTRAINT_FOREIGN) {
$this->templateData[$table]['constraints']['add'][$constraintName] = $constraint;
} else {
$this->templateData[$table]['indexes']['add'][$constraintName] = $constraint;
}
}
// constraints having the same name between new and old schema
// if present in both, check if they are the same : if not, remove the old one and add the new one
foreach ($currentConstraints as $constraintName) {
$constraint = $currentSchema->getConstraint($constraintName);
if (
in_array($constraintName, $oldConstraints, true) &&
$constraint !== $this->dumpSchema[$table]->getConstraint($constraintName)
) {
$this->templateData[$table]['constraints']['remove'][$constraintName] =
$this->dumpSchema[$table]->getConstraint($constraintName);
$this->templateData[$table]['constraints']['add'][$constraintName] =
$constraint;
}
}
// removed constraints
$removedConstraints = array_diff($oldConstraints, $currentConstraints);
foreach ($removedConstraints as $constraintName) {
$constraint = $this->dumpSchema[$table]->getConstraint($constraintName);
if ($constraint['type'] === TableSchema::CONSTRAINT_FOREIGN) {
$this->templateData[$table]['constraints']['remove'][$constraintName] = $constraint;
} else {
$this->templateData[$table]['indexes']['remove'][$constraintName] = $constraint;
}
}
}
}
/**
* Calculate the diff between indexes in existing tables.
* This will look for indexes addition, indexes removal and changes in indexes metadata
* such as change of referenced columns if the old indexes and the new one have the same name.
*
* The method directly sets the diff in a property of the class.
*
* @return void
*/
protected function getIndexes(): void
{
foreach ($this->commonTables as $table => $currentSchema) {
$currentIndexes = $currentSchema->indexes();
$oldIndexes = $this->dumpSchema[$table]->indexes();
sort($currentIndexes);
sort($oldIndexes);
// brand new indexes
$addedIndexes = array_diff($currentIndexes, $oldIndexes);
foreach ($addedIndexes as $indexName) {
$this->templateData[$table]['indexes']['add'][$indexName] = $currentSchema->getIndex($indexName);
}
// indexes having the same name between new and old schema
// if present in both, check if they are the same : if not, remove the old one and add the new one
foreach ($currentIndexes as $indexName) {
$index = $currentSchema->getIndex($indexName);
if (
in_array($indexName, $oldIndexes, true) &&
$index !== $this->dumpSchema[$table]->getIndex($indexName)
) {
$this->templateData[$table]['indexes']['remove'][$indexName] =
$this->dumpSchema[$table]->getIndex($indexName);
$this->templateData[$table]['indexes']['add'][$indexName] = $index;
}
}
// indexes deletion
if (!isset($this->templateData[$table]['indexes']['remove'])) {
$this->templateData[$table]['indexes']['remove'] = [];
}
$removedIndexes = array_diff($oldIndexes, $currentIndexes);
$parts = [];
if ($removedIndexes) {
foreach ($removedIndexes as $index) {
$parts[$index] = $this->dumpSchema[$table]->getIndex($index);
}
}
$this->templateData[$table]['indexes']['remove'] = array_merge(
$this->templateData[$table]['indexes']['remove'],
$parts,
);
}
}
/**
* Checks that the migrations history is in sync with the migrations files
*
* @return bool Whether migrations history is sync or not
*/
protected function checkSync(): bool
{
if (!$this->migrationsFiles && !$this->migratedItems) {
return true;
}
if ($this->migratedItems) {
$lastVersion = $this->migratedItems[0]['version'];
$lastFile = end($this->migrationsFiles);
return $lastFile && str_contains($lastFile, (string)$lastVersion);
}
return false;
}
/**
* Fallback method called to bake a snapshot when the phinx log history is empty and
* there are no migration files.
*
* @param string $name Name.
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int|null Value of the snapshot baking dispatch process
*/
protected function bakeSnapshot(string $name, Arguments $args, ConsoleIo $io): ?int
{
$io->out('Your migrations history is empty and you do not have any migrations files.');
$io->out('Falling back to baking a snapshot...');
$newArgs = [];
$newArgs[] = $name;
$newArgs = array_merge($newArgs, $this->parseOptions($args));
$exitCode = $this->executeCommand(BakeMigrationSnapshotCommand::class, $newArgs, $io);
if ($exitCode === 1) {
$io->abort('Something went wrong during the snapshot baking. Please try again.');
}
return $exitCode;
}
/**
* Fetch the correct schema dump based on the arguments and options passed to the shell call
* and returns it as an array
*
* @param \Cake\Console\Arguments $args The command arguments.
* @return array<string, \Cake\Database\Schema\TableSchemaInterface> Full database schema.
*/
protected function getDumpSchema(Arguments $args): array
{
$options = [];
$connectionName = 'default';
if ($args->getOption('connection')) {
$connectionName = $inputArgs['--connection'] = $args->getOption('connection');
}
$options['connection'] = $connectionName;
$options['source'] = $args->getOption('source');
$options['plugin'] = $args->getOption('plugin');
$factory = new ManagerFactory($options);
$config = $factory->createConfig();
$path = $config->getMigrationPath() . DS . 'schema-dump-' . $connectionName . '.lock';
if (!file_exists($path)) {
$msg = 'Unable to retrieve the schema dump file. You can create a dump file using ' .
'the `cake migrations dump` command';
$this->io->abort($msg);
}
$contents = (string)file_get_contents($path);
// Use allowed_classes to restrict deserialization to safe CakePHP schema classes
return unserialize($contents, [
'allowed_classes' => [
TableSchema::class,
CachedCollection::class,
Column::class,
Index::class,
Constraint::class,
UniqueKey::class,
ForeignKey::class,
CheckConstraint::class,
],
]);
}
/**
* Reflects the current database schema.
*
* @return array<string, \Cake\Database\Schema\TableSchemaInterface> Full database schema.
*/
protected function getCurrentSchema(): array
{
$schema = [];
if (!$this->tables) {
return $schema;
}
$connection = ConnectionManager::get($this->connection);
assert($connection instanceof Connection);
$connection->cacheMetadata(false);
$collection = $connection->getSchemaCollection();
// Filter tables based on plugin option
$tablesToDescribe = $this->tables;
if ($this->plugin) {
$tableFinder = new TableFinder($this->connection);
$options = [
'plugin' => $this->plugin,
'require-table' => true,
];
$tablesToDescribe = $tableFinder->getTablesToBake($collection, $options);
}
foreach ($tablesToDescribe as $table) {
if (preg_match('/^.*phinxlog$/', $table) === 1) {
continue;
}
if ($table === 'cake_migrations' || $table === 'cake_seeds') {
continue;
}
$schema[$table] = $collection->describe($table);
}
return $schema;
}
/**
* @inheritDoc
*/
public function template(): string
{
return 'Migrations.config/diff';
}
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser(): ConsoleOptionParser
{
$parser = parent::getOptionParser();
$parser->setDescription(
'Create a migration that captures the difference between ' .
'the migration state is expected to be and what the schema ' .
'reflection contains.',
)->addArgument('name', [
'help' => 'Name of the migration to bake. Can use Plugin.name to bake migration files into plugins.',
'required' => true,
])->addOption('generate-only', [
'help' => 'Only generate the migration file without marking it as applied',
'boolean' => true,
]);
return $parser;
}
}