Completed
Pull Request — 3.0.x (#3917)
by Yanick
62:37
created

Comparator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Types;
6
use function array_intersect_key;
7
use function array_key_exists;
8
use function array_keys;
9
use function array_map;
10
use function array_merge;
11
use function array_shift;
12
use function array_unique;
13
use function assert;
14
use function count;
15
use function get_class;
16
use function strtolower;
17
18
/**
19
 * Compares two Schemas and return an instance of SchemaDiff.
20
 */
21
class Comparator
22
{
23
    public const DETECT_COLUMN_RENAMINGS = 0b0001;
24
    public const DETECT_INDEX_RENAMINGS  = 0b0010;
25
26
    /** @var int */
27
    private $flags;
28
29
    public function __construct(int $flags = self::DETECT_COLUMN_RENAMINGS | self::DETECT_INDEX_RENAMINGS)
30
    {
31
        $this->flags = $flags;
32
    }
33
34
    /**
35
     * @return SchemaDiff
36
     */
37
    public static function compareSchemas(Schema $fromSchema, Schema $toSchema)
38
    {
39
        $c = new self();
40
41
        return $c->compare($fromSchema, $toSchema);
42
    }
43
44
    /**
45
     * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
46
     *
47
     * The returned differences are returned in such a way that they contain the
48
     * operations to change the schema stored in $fromSchema to the schema that is
49
     * stored in $toSchema.
50
     *
51
     * @return SchemaDiff
52
     */
53
    public function compare(Schema $fromSchema, Schema $toSchema)
54
    {
55
        $diff             = new SchemaDiff();
56
        $diff->fromSchema = $fromSchema;
57
58
        $foreignKeysToTable = [];
59
60
        foreach ($toSchema->getNamespaces() as $namespace) {
61
            if ($fromSchema->hasNamespace($namespace)) {
62
                continue;
63
            }
64
65
            $diff->newNamespaces[$namespace] = $namespace;
66
        }
67
68
        foreach ($fromSchema->getNamespaces() as $namespace) {
69
            if ($toSchema->hasNamespace($namespace)) {
70
                continue;
71
            }
72
73
            $diff->removedNamespaces[$namespace] = $namespace;
74
        }
75
76
        foreach ($toSchema->getTables() as $table) {
77
            $tableName = $table->getShortestName($toSchema->getName());
78
            if (! $fromSchema->hasTable($tableName)) {
79
                $diff->newTables[$tableName] = $toSchema->getTable($tableName);
80
            } else {
81
                $tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $toSchema->getTable($tableName));
82
                if ($tableDifferences !== false) {
83
                    $diff->changedTables[$tableName] = $tableDifferences;
84
                }
85
            }
86
        }
87
88
        /* Check if there are tables removed */
89
        foreach ($fromSchema->getTables() as $table) {
90
            $tableName = $table->getShortestName($fromSchema->getName());
91
92
            $table = $fromSchema->getTable($tableName);
93
            if (! $toSchema->hasTable($tableName)) {
94
                $diff->removedTables[$tableName] = $table;
95
            }
96
97
            // also remember all foreign keys that point to a specific table
98
            foreach ($table->getForeignKeys() as $foreignKey) {
99
                $foreignTable = strtolower($foreignKey->getForeignTableName());
100
                if (! isset($foreignKeysToTable[$foreignTable])) {
101
                    $foreignKeysToTable[$foreignTable] = [];
102
                }
103
                $foreignKeysToTable[$foreignTable][] = $foreignKey;
104
            }
105
        }
106
107
        foreach ($diff->removedTables as $tableName => $table) {
108
            if (! isset($foreignKeysToTable[$tableName])) {
109
                continue;
110
            }
111
112
            $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
113
114
            // deleting duplicated foreign keys present on both on the orphanedForeignKey
115
            // and the removedForeignKeys from changedTables
116
            foreach ($foreignKeysToTable[$tableName] as $foreignKey) {
117
                // strtolower the table name to make if compatible with getShortestName
118
                $localTableName = strtolower($foreignKey->getLocalTableName());
119
                if (! isset($diff->changedTables[$localTableName])) {
120
                    continue;
121
                }
122
123
                foreach ($diff->changedTables[$localTableName]->removedForeignKeys as $key => $removedForeignKey) {
124
                    assert($removedForeignKey instanceof ForeignKeyConstraint);
125
126
                    // We check if the key is from the removed table if not we skip.
127
                    if ($tableName !== strtolower($removedForeignKey->getForeignTableName())) {
128
                        continue;
129
                    }
130
                    unset($diff->changedTables[$localTableName]->removedForeignKeys[$key]);
131
                }
132
            }
133
        }
134
135
        foreach ($toSchema->getSequences() as $sequence) {
136
            $sequenceName = $sequence->getShortestName($toSchema->getName());
137
            if (! $fromSchema->hasSequence($sequenceName)) {
138
                if (! $this->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) {
139
                    $diff->newSequences[] = $sequence;
140
                }
141
            } else {
142
                if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
143
                    $diff->changedSequences[] = $toSchema->getSequence($sequenceName);
144
                }
145
            }
146
        }
147
148
        foreach ($fromSchema->getSequences() as $sequence) {
149
            if ($this->isAutoIncrementSequenceInSchema($toSchema, $sequence)) {
150
                continue;
151
            }
152
153
            $sequenceName = $sequence->getShortestName($fromSchema->getName());
154
155
            if ($toSchema->hasSequence($sequenceName)) {
156
                continue;
157
            }
158
159
            $diff->removedSequences[] = $sequence;
160
        }
161
162
        return $diff;
163
    }
164
165
    /**
166
     * @param Schema   $schema
167
     * @param Sequence $sequence
168
     *
169
     * @return bool
170
     */
171
    private function isAutoIncrementSequenceInSchema($schema, $sequence)
172
    {
173
        foreach ($schema->getTables() as $table) {
174
            if ($sequence->isAutoIncrementsFor($table)) {
175
                return true;
176
            }
177
        }
178
179
        return false;
180
    }
181
182
    /**
183
     * @return bool
184
     */
185
    public function diffSequence(Sequence $sequence1, Sequence $sequence2)
186
    {
187
        if ($sequence1->getAllocationSize() !== $sequence2->getAllocationSize()) {
188
            return true;
189
        }
190
191
        return $sequence1->getInitialValue() !== $sequence2->getInitialValue();
192
    }
193
194
    /**
195
     * Returns the difference between the tables $table1 and $table2.
196
     *
197
     * If there are no differences this method returns the boolean false.
198
     *
199
     * @return TableDiff|false
200
     */
201
    public function diffTable(Table $table1, Table $table2)
202
    {
203
        $changes                     = 0;
204
        $tableDifferences            = new TableDiff($table1->getName());
205
        $tableDifferences->fromTable = $table1;
206
207
        $table1Columns = $table1->getColumns();
208
        $table2Columns = $table2->getColumns();
209
210
        /* See if all the fields in table 1 exist in table 2 */
211
        foreach ($table2Columns as $columnName => $column) {
212
            if ($table1->hasColumn($columnName)) {
213
                continue;
214
            }
215
216
            $tableDifferences->addedColumns[$columnName] = $column;
217
            $changes++;
218
        }
219
        /* See if there are any removed fields in table 2 */
220
        foreach ($table1Columns as $columnName => $column) {
221
            // See if column is removed in table 2.
222
            if (! $table2->hasColumn($columnName)) {
223
                $tableDifferences->removedColumns[$columnName] = $column;
224
                $changes++;
225
                continue;
226
            }
227
228
            // See if column has changed properties in table 2.
229
            $changedProperties = $this->diffColumn($column, $table2->getColumn($columnName));
230
231
            if (empty($changedProperties)) {
232
                continue;
233
            }
234
235
            $columnDiff                                           = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
236
            $columnDiff->fromColumn                               = $column;
237
            $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
238
            $changes++;
239
        }
240
241
        if ($this->flags & self::DETECT_COLUMN_RENAMINGS) {
242
            $this->detectColumnRenamings($tableDifferences);
243
        }
244
245
        $table1Indexes = $table1->getIndexes();
246
        $table2Indexes = $table2->getIndexes();
247
248
        /* See if all the indexes in table 1 exist in table 2 */
249
        foreach ($table2Indexes as $indexName => $index) {
250
            if (($index->isPrimary() && $table1->hasPrimaryKey()) || $table1->hasIndex($indexName)) {
251
                continue;
252
            }
253
254
            $tableDifferences->addedIndexes[$indexName] = $index;
255
            $changes++;
256
        }
257
        /* See if there are any removed indexes in table 2 */
258
        foreach ($table1Indexes as $indexName => $index) {
259
            // See if index is removed in table 2.
260
            if (($index->isPrimary() && ! $table2->hasPrimaryKey()) ||
261
                ! $index->isPrimary() && ! $table2->hasIndex($indexName)
262
            ) {
263
                $tableDifferences->removedIndexes[$indexName] = $index;
264
                $changes++;
265
                continue;
266
            }
267
268
            // See if index has changed in table 2.
269
            $table2Index = $index->isPrimary() ? $table2->getPrimaryKey() : $table2->getIndex($indexName);
270
            assert($table2Index instanceof Index);
271
272
            if (! $this->diffIndex($index, $table2Index)) {
273
                continue;
274
            }
275
276
            $tableDifferences->changedIndexes[$indexName] = $table2Index;
277
            $changes++;
278
        }
279
280
        if ($this->flags & self::DETECT_INDEX_RENAMINGS) {
281
            $this->detectIndexRenamings($tableDifferences);
282
        }
283
284
        $fromFkeys = $table1->getForeignKeys();
285
        $toFkeys   = $table2->getForeignKeys();
286
287
        foreach ($fromFkeys as $key1 => $constraint1) {
288
            foreach ($toFkeys as $key2 => $constraint2) {
289
                if ($this->diffForeignKey($constraint1, $constraint2) === false) {
290
                    unset($fromFkeys[$key1], $toFkeys[$key2]);
291
                } else {
292
                    if (strtolower($constraint1->getName()) === strtolower($constraint2->getName())) {
293
                        $tableDifferences->changedForeignKeys[] = $constraint2;
294
                        $changes++;
295
                        unset($fromFkeys[$key1], $toFkeys[$key2]);
296
                    }
297
                }
298
            }
299
        }
300
301
        foreach ($fromFkeys as $constraint1) {
302
            $tableDifferences->removedForeignKeys[] = $constraint1;
303
            $changes++;
304
        }
305
306
        foreach ($toFkeys as $constraint2) {
307
            $tableDifferences->addedForeignKeys[] = $constraint2;
308
            $changes++;
309
        }
310
311
        return $changes ? $tableDifferences : false;
312
    }
313
314
    /**
315
     * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
316
     * however ambiguities between different possibilities should not lead to renaming at all.
317
     *
318
     * @return void
319
     */
320
    private function detectColumnRenamings(TableDiff $tableDifferences)
321
    {
322
        $renameCandidates = [];
323
        foreach ($tableDifferences->addedColumns as $addedColumnName => $addedColumn) {
324
            foreach ($tableDifferences->removedColumns as $removedColumn) {
325
                if (count($this->diffColumn($addedColumn, $removedColumn)) !== 0) {
326
                    continue;
327
                }
328
329
                $renameCandidates[$addedColumn->getName()][] = [$removedColumn, $addedColumn, $addedColumnName];
330
            }
331
        }
332
333
        foreach ($renameCandidates as $candidateColumns) {
334
            if (count($candidateColumns) !== 1) {
335
                continue;
336
            }
337
338
            [$removedColumn, $addedColumn] = $candidateColumns[0];
339
            $removedColumnName             = strtolower($removedColumn->getName());
340
            $addedColumnName               = strtolower($addedColumn->getName());
341
342
            if (isset($tableDifferences->renamedColumns[$removedColumnName])) {
343
                continue;
344
            }
345
346
            $tableDifferences->renamedColumns[$removedColumnName] = $addedColumn;
347
            unset(
348
                $tableDifferences->addedColumns[$addedColumnName],
349
                $tableDifferences->removedColumns[$removedColumnName]
350
            );
351
        }
352
    }
353
354
    /**
355
     * Try to find indexes that only changed their name, rename operations maybe cheaper than add/drop
356
     * however ambiguities between different possibilities should not lead to renaming at all.
357
     *
358
     * @return void
359
     */
360
    private function detectIndexRenamings(TableDiff $tableDifferences)
361
    {
362
        $renameCandidates = [];
363
364
        // Gather possible rename candidates by comparing each added and removed index based on semantics.
365
        foreach ($tableDifferences->addedIndexes as $addedIndexName => $addedIndex) {
366
            foreach ($tableDifferences->removedIndexes as $removedIndex) {
367
                if ($this->diffIndex($addedIndex, $removedIndex)) {
368
                    continue;
369
                }
370
371
                $renameCandidates[$addedIndex->getName()][] = [$removedIndex, $addedIndex, $addedIndexName];
372
            }
373
        }
374
375
        foreach ($renameCandidates as $candidateIndexes) {
376
            // If the current rename candidate contains exactly one semantically equal index,
377
            // we can safely rename it.
378
            // Otherwise it is unclear if a rename action is really intended,
379
            // therefore we let those ambiguous indexes be added/dropped.
380
            if (count($candidateIndexes) !== 1) {
381
                continue;
382
            }
383
384
            [$removedIndex, $addedIndex] = $candidateIndexes[0];
385
386
            $removedIndexName = strtolower($removedIndex->getName());
387
            $addedIndexName   = strtolower($addedIndex->getName());
388
389
            if (isset($tableDifferences->renamedIndexes[$removedIndexName])) {
390
                continue;
391
            }
392
393
            $tableDifferences->renamedIndexes[$removedIndexName] = $addedIndex;
394
            unset(
395
                $tableDifferences->addedIndexes[$addedIndexName],
396
                $tableDifferences->removedIndexes[$removedIndexName]
397
            );
398
        }
399
    }
400
401
    /**
402
     * @return bool
403
     */
404
    public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2)
405
    {
406
        if (array_map('strtolower', $key1->getUnquotedLocalColumns()) !== array_map('strtolower', $key2->getUnquotedLocalColumns())) {
407
            return true;
408
        }
409
410
        if (array_map('strtolower', $key1->getUnquotedForeignColumns()) !== array_map('strtolower', $key2->getUnquotedForeignColumns())) {
411
            return true;
412
        }
413
414
        if ($key1->getUnqualifiedForeignTableName() !== $key2->getUnqualifiedForeignTableName()) {
415
            return true;
416
        }
417
418
        if ($key1->onUpdate() !== $key2->onUpdate()) {
419
            return true;
420
        }
421
422
        return $key1->onDelete() !== $key2->onDelete();
423
    }
424
425
    /**
426
     * Returns the difference between the fields $field1 and $field2.
427
     *
428
     * If there are differences this method returns $field2, otherwise the
429
     * boolean false.
430
     *
431
     * @return string[]
432
     */
433
    public function diffColumn(Column $column1, Column $column2)
434
    {
435
        $properties1 = $column1->toArray();
436
        $properties2 = $column2->toArray();
437
438
        $changedProperties = [];
439
440
        if (get_class($properties1['type']) !== get_class($properties2['type'])) {
441
            $changedProperties[] = 'type';
442
        }
443
444
        foreach (['notnull', 'unsigned', 'autoincrement'] as $property) {
445
            if ($properties1[$property] === $properties2[$property]) {
446
                continue;
447
            }
448
449
            $changedProperties[] = $property;
450
        }
451
452
        // This is a very nasty hack to make comparator work with the legacy json_array type, which should be killed in v3
453
        if ($this->isALegacyJsonComparison($properties1['type'], $properties2['type'])) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Com...ALegacyJsonComparison() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

453
        if (/** @scrutinizer ignore-deprecated */ $this->isALegacyJsonComparison($properties1['type'], $properties2['type'])) {
Loading history...
454
            array_shift($changedProperties);
455
456
            $changedProperties[] = 'comment';
457
        }
458
459
        // Null values need to be checked additionally as they tell whether to create or drop a default value.
460
        // null != 0, null != false, null != '' etc. This affects platform's table alteration SQL generation.
461
        if (($properties1['default'] === null) !== ($properties2['default'] === null)
462
            || $properties1['default'] != $properties2['default']) {
463
            $changedProperties[] = 'default';
464
        }
465
466
        if (($properties1['type'] instanceof Types\StringType && ! $properties1['type'] instanceof Types\GuidType) ||
467
            $properties1['type'] instanceof Types\BinaryType
468
        ) {
469
            // check if value of length is set at all, default value assumed otherwise.
470
            $length1 = $properties1['length'] ?: 255;
471
            $length2 = $properties2['length'] ?: 255;
472
            if ($length1 !== $length2) {
473
                $changedProperties[] = 'length';
474
            }
475
476
            if ($properties1['fixed'] !== $properties2['fixed']) {
477
                $changedProperties[] = 'fixed';
478
            }
479
        } elseif ($properties1['type'] instanceof Types\DecimalType) {
480
            if (($properties1['precision'] ?: 10) !== ($properties2['precision'] ?: 10)) {
481
                $changedProperties[] = 'precision';
482
            }
483
            if ($properties1['scale'] !== $properties2['scale']) {
484
                $changedProperties[] = 'scale';
485
            }
486
        }
487
488
        // A null value and an empty string are actually equal for a comment so they should not trigger a change.
489
        if ($properties1['comment'] !== $properties2['comment'] &&
490
            ! ($properties1['comment'] === null && $properties2['comment'] === '') &&
491
            ! ($properties2['comment'] === null && $properties1['comment'] === '')
492
        ) {
493
            $changedProperties[] = 'comment';
494
        }
495
496
        $customOptions1 = $column1->getCustomSchemaOptions();
497
        $customOptions2 = $column2->getCustomSchemaOptions();
498
499
        foreach (array_merge(array_keys($customOptions1), array_keys($customOptions2)) as $key) {
500
            if (! array_key_exists($key, $properties1) || ! array_key_exists($key, $properties2)) {
501
                $changedProperties[] = $key;
502
            } elseif ($properties1[$key] !== $properties2[$key]) {
503
                $changedProperties[] = $key;
504
            }
505
        }
506
507
        $platformOptions1 = $column1->getPlatformOptions();
508
        $platformOptions2 = $column2->getPlatformOptions();
509
510
        foreach (array_keys(array_intersect_key($platformOptions1, $platformOptions2)) as $key) {
511
            if ($properties1[$key] === $properties2[$key]) {
512
                continue;
513
            }
514
515
            $changedProperties[] = $key;
516
        }
517
518
        return array_unique($changedProperties);
519
    }
520
521
    /**
522
     * TODO: kill with fire on v3.0
523
     *
524
     * @deprecated
525
     */
526
    private function isALegacyJsonComparison(Types\Type $one, Types\Type $other) : bool
527
    {
528
        if (! $one instanceof Types\JsonType || ! $other instanceof Types\JsonType) {
529
            return false;
530
        }
531
532
        return ( ! $one instanceof Types\JsonArrayType && $other instanceof Types\JsonArrayType)
533
            || ( ! $other instanceof Types\JsonArrayType && $one instanceof Types\JsonArrayType);
534
    }
535
536
    /**
537
     * Finds the difference between the indexes $index1 and $index2.
538
     *
539
     * Compares $index1 with $index2 and returns $index2 if there are any
540
     * differences or false in case there are no differences.
541
     *
542
     * @return bool
543
     */
544
    public function diffIndex(Index $index1, Index $index2)
545
    {
546
        return ! ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1));
547
    }
548
}
549