1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Schema; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Types; |
23
|
|
|
use function array_intersect_key; |
24
|
|
|
use function array_key_exists; |
25
|
|
|
use function array_keys; |
26
|
|
|
use function array_map; |
27
|
|
|
use function array_merge; |
28
|
|
|
use function array_shift; |
29
|
|
|
use function array_unique; |
30
|
|
|
use function count; |
31
|
|
|
use function strtolower; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Compares two Schemas and return an instance of SchemaDiff. |
35
|
|
|
* |
36
|
|
|
* @link www.doctrine-project.org |
37
|
|
|
* @since 2.0 |
38
|
|
|
* @author Benjamin Eberlei <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
class Comparator |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $fromSchema |
44
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $toSchema |
45
|
|
|
* |
46
|
|
|
* @return \Doctrine\DBAL\Schema\SchemaDiff |
47
|
|
|
*/ |
48
|
340 |
|
public static function compareSchemas(Schema $fromSchema, Schema $toSchema) |
49
|
|
|
{ |
50
|
340 |
|
$c = new self(); |
51
|
|
|
|
52
|
340 |
|
return $c->compare($fromSchema, $toSchema); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema. |
57
|
|
|
* |
58
|
|
|
* The returned differences are returned in such a way that they contain the |
59
|
|
|
* operations to change the schema stored in $fromSchema to the schema that is |
60
|
|
|
* stored in $toSchema. |
61
|
|
|
* |
62
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $fromSchema |
63
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $toSchema |
64
|
|
|
* |
65
|
|
|
* @return \Doctrine\DBAL\Schema\SchemaDiff |
66
|
|
|
*/ |
67
|
600 |
|
public function compare(Schema $fromSchema, Schema $toSchema) |
68
|
|
|
{ |
69
|
600 |
|
$diff = new SchemaDiff(); |
70
|
600 |
|
$diff->fromSchema = $fromSchema; |
71
|
|
|
|
72
|
600 |
|
$foreignKeysToTable = []; |
73
|
|
|
|
74
|
600 |
|
foreach ($toSchema->getNamespaces() as $namespace) { |
75
|
40 |
|
if ( ! $fromSchema->hasNamespace($namespace)) { |
76
|
40 |
|
$diff->newNamespaces[$namespace] = $namespace; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
600 |
|
foreach ($fromSchema->getNamespaces() as $namespace) { |
81
|
40 |
|
if ( ! $toSchema->hasNamespace($namespace)) { |
82
|
40 |
|
$diff->removedNamespaces[$namespace] = $namespace; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
600 |
|
foreach ($toSchema->getTables() as $table) { |
87
|
420 |
|
$tableName = $table->getShortestName($toSchema->getName()); |
88
|
420 |
|
if ( ! $fromSchema->hasTable($tableName)) { |
89
|
100 |
|
$diff->newTables[$tableName] = $toSchema->getTable($tableName); |
90
|
|
|
} else { |
91
|
380 |
|
$tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $toSchema->getTable($tableName)); |
92
|
380 |
|
if ($tableDifferences !== false) { |
93
|
420 |
|
$diff->changedTables[$tableName] = $tableDifferences; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/* Check if there are tables removed */ |
99
|
600 |
|
foreach ($fromSchema->getTables() as $table) { |
100
|
400 |
|
$tableName = $table->getShortestName($fromSchema->getName()); |
101
|
|
|
|
102
|
400 |
|
$table = $fromSchema->getTable($tableName); |
103
|
400 |
|
if ( ! $toSchema->hasTable($tableName)) { |
104
|
100 |
|
$diff->removedTables[$tableName] = $table; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// also remember all foreign keys that point to a specific table |
108
|
400 |
|
foreach ($table->getForeignKeys() as $foreignKey) { |
109
|
40 |
|
$foreignTable = strtolower($foreignKey->getForeignTableName()); |
110
|
40 |
|
if (!isset($foreignKeysToTable[$foreignTable])) { |
111
|
40 |
|
$foreignKeysToTable[$foreignTable] = []; |
112
|
|
|
} |
113
|
400 |
|
$foreignKeysToTable[$foreignTable][] = $foreignKey; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
600 |
|
foreach ($diff->removedTables as $tableName => $table) { |
118
|
100 |
|
if (isset($foreignKeysToTable[$tableName])) { |
119
|
40 |
|
$diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]); |
120
|
|
|
|
121
|
|
|
// deleting duplicated foreign keys present on both on the orphanedForeignKey |
122
|
|
|
// and the removedForeignKeys from changedTables |
123
|
40 |
|
foreach ($foreignKeysToTable[$tableName] as $foreignKey) { |
124
|
|
|
// strtolower the table name to make if compatible with getShortestName |
125
|
40 |
|
$localTableName = strtolower($foreignKey->getLocalTableName()); |
126
|
40 |
|
if (isset($diff->changedTables[$localTableName])) { |
127
|
40 |
|
foreach ($diff->changedTables[$localTableName]->removedForeignKeys as $key => $removedForeignKey) { |
128
|
|
|
// We check if the key is from the removed table if not we skip. |
129
|
40 |
|
if ($tableName !== strtolower($removedForeignKey->getForeignTableName())) { |
130
|
20 |
|
continue; |
131
|
|
|
} |
132
|
100 |
|
unset($diff->changedTables[$localTableName]->removedForeignKeys[$key]); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
600 |
|
foreach ($toSchema->getSequences() as $sequence) { |
140
|
80 |
|
$sequenceName = $sequence->getShortestName($toSchema->getName()); |
141
|
80 |
|
if ( ! $fromSchema->hasSequence($sequenceName)) { |
142
|
60 |
|
if ( ! $this->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) { |
143
|
60 |
|
$diff->newSequences[] = $sequence; |
144
|
|
|
} |
145
|
|
|
} else { |
146
|
40 |
|
if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) { |
147
|
80 |
|
$diff->changedSequences[] = $toSchema->getSequence($sequenceName); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
600 |
|
foreach ($fromSchema->getSequences() as $sequence) { |
153
|
80 |
|
if ($this->isAutoIncrementSequenceInSchema($toSchema, $sequence)) { |
154
|
20 |
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
60 |
|
$sequenceName = $sequence->getShortestName($fromSchema->getName()); |
158
|
|
|
|
159
|
60 |
|
if ( ! $toSchema->hasSequence($sequenceName)) { |
160
|
60 |
|
$diff->removedSequences[] = $sequence; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
600 |
|
foreach ($toSchema->getViews() as $view) { |
165
|
40 |
|
$viewName = $view->getShortestName($toSchema->getName()); |
166
|
40 |
|
if ( ! $fromSchema->hasView($viewName)) { |
167
|
20 |
|
$diff->newViews[$viewName] = $view; |
168
|
20 |
|
} elseif ($fromSchema->getView($viewName)->isSameAs($view)) { |
169
|
40 |
|
$diff->changedViews[$viewName] = $view; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
600 |
|
foreach ($fromSchema->getViews() as $view) { |
174
|
40 |
|
$viewName = $view->getShortestName($fromSchema->getName()); |
175
|
40 |
|
if ( ! $toSchema->hasView($viewName)) { |
176
|
40 |
|
$diff->removedViews[$viewName] = $view; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
600 |
|
return $diff; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $schema |
185
|
|
|
* @param \Doctrine\DBAL\Schema\Sequence $sequence |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
120 |
|
private function isAutoIncrementSequenceInSchema($schema, $sequence) |
190
|
|
|
{ |
191
|
120 |
|
foreach ($schema->getTables() as $table) { |
192
|
40 |
|
if ($sequence->isAutoIncrementsFor($table)) { |
193
|
40 |
|
return true; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
80 |
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param \Doctrine\DBAL\Schema\Sequence $sequence1 |
202
|
|
|
* @param \Doctrine\DBAL\Schema\Sequence $sequence2 |
203
|
|
|
* |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
70 |
|
public function diffSequence(Sequence $sequence1, Sequence $sequence2) |
207
|
|
|
{ |
208
|
70 |
|
if ($sequence1->getAllocationSize() != $sequence2->getAllocationSize()) { |
209
|
40 |
|
return true; |
210
|
|
|
} |
211
|
|
|
|
212
|
50 |
|
return $sequence1->getInitialValue() !== $sequence2->getInitialValue(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns the difference between the tables $table1 and $table2. |
217
|
|
|
* |
218
|
|
|
* If there are no differences this method returns the boolean false. |
219
|
|
|
* |
220
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table1 |
221
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table2 |
222
|
|
|
* |
223
|
|
|
* @return TableDiff|false |
224
|
|
|
*/ |
225
|
2603 |
|
public function diffTable(Table $table1, Table $table2) |
226
|
|
|
{ |
227
|
2603 |
|
$changes = 0; |
228
|
2603 |
|
$tableDifferences = new TableDiff($table1->getName()); |
229
|
2603 |
|
$tableDifferences->fromTable = $table1; |
230
|
|
|
|
231
|
2603 |
|
$table1Columns = $table1->getColumns(); |
232
|
2603 |
|
$table2Columns = $table2->getColumns(); |
233
|
|
|
|
234
|
|
|
/* See if all the fields in table 1 exist in table 2 */ |
235
|
2603 |
|
foreach ($table2Columns as $columnName => $column) { |
236
|
2503 |
|
if ( !$table1->hasColumn($columnName)) { |
237
|
619 |
|
$tableDifferences->addedColumns[$columnName] = $column; |
238
|
2503 |
|
$changes++; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
/* See if there are any removed fields in table 2 */ |
242
|
2603 |
|
foreach ($table1Columns as $columnName => $column) { |
243
|
|
|
// See if column is removed in table 2. |
244
|
2503 |
|
if ( ! $table2->hasColumn($columnName)) { |
245
|
619 |
|
$tableDifferences->removedColumns[$columnName] = $column; |
246
|
619 |
|
$changes++; |
247
|
619 |
|
continue; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// See if column has changed properties in table 2. |
251
|
2083 |
|
$changedProperties = $this->diffColumn($column, $table2->getColumn($columnName)); |
252
|
|
|
|
253
|
2083 |
|
if ( ! empty($changedProperties)) { |
254
|
858 |
|
$columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); |
255
|
858 |
|
$columnDiff->fromColumn = $column; |
256
|
858 |
|
$tableDifferences->changedColumns[$column->getName()] = $columnDiff; |
257
|
2083 |
|
$changes++; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
2603 |
|
$this->detectColumnRenamings($tableDifferences); |
262
|
|
|
|
263
|
2603 |
|
$table1Indexes = $table1->getIndexes(); |
264
|
2603 |
|
$table2Indexes = $table2->getIndexes(); |
265
|
|
|
|
266
|
|
|
/* See if all the indexes in table 1 exist in table 2 */ |
267
|
2603 |
|
foreach ($table2Indexes as $indexName => $index) { |
268
|
969 |
|
if (($index->isPrimary() && $table1->hasPrimaryKey()) || $table1->hasIndex($indexName)) { |
269
|
614 |
|
continue; |
270
|
|
|
} |
271
|
|
|
|
272
|
355 |
|
$tableDifferences->addedIndexes[$indexName] = $index; |
273
|
355 |
|
$changes++; |
274
|
|
|
} |
275
|
|
|
/* See if there are any removed indexes in table 2 */ |
276
|
2603 |
|
foreach ($table1Indexes as $indexName => $index) { |
277
|
|
|
// See if index is removed in table 2. |
278
|
909 |
|
if (($index->isPrimary() && ! $table2->hasPrimaryKey()) || |
279
|
909 |
|
! $index->isPrimary() && ! $table2->hasIndex($indexName) |
280
|
|
|
) { |
281
|
395 |
|
$tableDifferences->removedIndexes[$indexName] = $index; |
282
|
395 |
|
$changes++; |
283
|
395 |
|
continue; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// See if index has changed in table 2. |
287
|
614 |
|
$table2Index = $index->isPrimary() ? $table2->getPrimaryKey() : $table2->getIndex($indexName); |
288
|
|
|
|
289
|
614 |
|
if ($this->diffIndex($index, $table2Index)) { |
|
|
|
|
290
|
319 |
|
$tableDifferences->changedIndexes[$indexName] = $table2Index; |
291
|
614 |
|
$changes++; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
2603 |
|
$this->detectIndexRenamings($tableDifferences); |
296
|
|
|
|
297
|
2603 |
|
$fromFkeys = $table1->getForeignKeys(); |
298
|
2603 |
|
$toFkeys = $table2->getForeignKeys(); |
299
|
|
|
|
300
|
2603 |
|
foreach ($fromFkeys as $key1 => $constraint1) { |
301
|
265 |
|
foreach ($toFkeys as $key2 => $constraint2) { |
302
|
125 |
|
if ($this->diffForeignKey($constraint1, $constraint2) === false) { |
303
|
46 |
|
unset($fromFkeys[$key1], $toFkeys[$key2]); |
304
|
|
|
} else { |
305
|
79 |
|
if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) { |
306
|
40 |
|
$tableDifferences->changedForeignKeys[] = $constraint2; |
307
|
40 |
|
$changes++; |
308
|
265 |
|
unset($fromFkeys[$key1], $toFkeys[$key2]); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
2603 |
|
foreach ($fromFkeys as $constraint1) { |
315
|
179 |
|
$tableDifferences->removedForeignKeys[] = $constraint1; |
316
|
179 |
|
$changes++; |
317
|
|
|
} |
318
|
|
|
|
319
|
2603 |
|
foreach ($toFkeys as $constraint2) { |
320
|
59 |
|
$tableDifferences->addedForeignKeys[] = $constraint2; |
321
|
59 |
|
$changes++; |
322
|
|
|
} |
323
|
|
|
|
324
|
2603 |
|
return $changes ? $tableDifferences : false; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Try to find columns that only changed their name, rename operations maybe cheaper than add/drop |
329
|
|
|
* however ambiguities between different possibilities should not lead to renaming at all. |
330
|
|
|
* |
331
|
|
|
* @return void |
332
|
|
|
*/ |
333
|
2603 |
|
private function detectColumnRenamings(TableDiff $tableDifferences) |
334
|
|
|
{ |
335
|
2603 |
|
$renameCandidates = []; |
336
|
2603 |
|
foreach ($tableDifferences->addedColumns as $addedColumnName => $addedColumn) { |
337
|
619 |
|
foreach ($tableDifferences->removedColumns as $removedColumn) { |
338
|
479 |
|
if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) { |
339
|
619 |
|
$renameCandidates[$addedColumn->getName()][] = [$removedColumn, $addedColumn, $addedColumnName]; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
2603 |
|
foreach ($renameCandidates as $candidateColumns) { |
345
|
479 |
|
if (count($candidateColumns) == 1) { |
346
|
459 |
|
list($removedColumn, $addedColumn) = $candidateColumns[0]; |
347
|
459 |
|
$removedColumnName = strtolower($removedColumn->getName()); |
348
|
459 |
|
$addedColumnName = strtolower($addedColumn->getName()); |
349
|
|
|
|
350
|
459 |
|
if ( ! isset($tableDifferences->renamedColumns[$removedColumnName])) { |
351
|
459 |
|
$tableDifferences->renamedColumns[$removedColumnName] = $addedColumn; |
352
|
|
|
unset( |
353
|
459 |
|
$tableDifferences->addedColumns[$addedColumnName], |
354
|
479 |
|
$tableDifferences->removedColumns[$removedColumnName] |
355
|
|
|
); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
} |
359
|
2603 |
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Try to find indexes that only changed their name, rename operations maybe cheaper than add/drop |
363
|
|
|
* however ambiguities between different possibilities should not lead to renaming at all. |
364
|
|
|
* |
365
|
|
|
* @return void |
366
|
|
|
*/ |
367
|
2603 |
|
private function detectIndexRenamings(TableDiff $tableDifferences) |
368
|
|
|
{ |
369
|
2603 |
|
$renameCandidates = []; |
370
|
|
|
|
371
|
|
|
// Gather possible rename candidates by comparing each added and removed index based on semantics. |
372
|
2603 |
|
foreach ($tableDifferences->addedIndexes as $addedIndexName => $addedIndex) { |
373
|
355 |
|
foreach ($tableDifferences->removedIndexes as $removedIndex) { |
374
|
167 |
|
if (! $this->diffIndex($addedIndex, $removedIndex)) { |
375
|
355 |
|
$renameCandidates[$addedIndex->getName()][] = [$removedIndex, $addedIndex, $addedIndexName]; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
2603 |
|
foreach ($renameCandidates as $candidateIndexes) { |
381
|
|
|
// If the current rename candidate contains exactly one semantically equal index, |
382
|
|
|
// we can safely rename it. |
383
|
|
|
// Otherwise it is unclear if a rename action is really intended, |
384
|
|
|
// therefore we let those ambiguous indexes be added/dropped. |
385
|
79 |
|
if (count($candidateIndexes) === 1) { |
386
|
59 |
|
list($removedIndex, $addedIndex) = $candidateIndexes[0]; |
387
|
|
|
|
388
|
59 |
|
$removedIndexName = strtolower($removedIndex->getName()); |
389
|
59 |
|
$addedIndexName = strtolower($addedIndex->getName()); |
390
|
|
|
|
391
|
59 |
|
if (! isset($tableDifferences->renamedIndexes[$removedIndexName])) { |
392
|
59 |
|
$tableDifferences->renamedIndexes[$removedIndexName] = $addedIndex; |
393
|
|
|
unset( |
394
|
59 |
|
$tableDifferences->addedIndexes[$addedIndexName], |
395
|
79 |
|
$tableDifferences->removedIndexes[$removedIndexName] |
396
|
|
|
); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
} |
400
|
2603 |
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $key1 |
404
|
|
|
* @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $key2 |
405
|
|
|
* |
406
|
|
|
* @return bool |
407
|
|
|
*/ |
408
|
185 |
|
public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2) |
409
|
|
|
{ |
410
|
185 |
|
if (array_map('strtolower', $key1->getUnquotedLocalColumns()) != array_map('strtolower', $key2->getUnquotedLocalColumns())) { |
411
|
39 |
|
return true; |
412
|
|
|
} |
413
|
|
|
|
414
|
146 |
|
if (array_map('strtolower', $key1->getUnquotedForeignColumns()) != array_map('strtolower', $key2->getUnquotedForeignColumns())) { |
415
|
|
|
return true; |
416
|
|
|
} |
417
|
|
|
|
418
|
146 |
|
if ($key1->getUnqualifiedForeignTableName() !== $key2->getUnqualifiedForeignTableName()) { |
419
|
20 |
|
return true; |
420
|
|
|
} |
421
|
|
|
|
422
|
126 |
|
if ($key1->onUpdate() != $key2->onUpdate()) { |
423
|
20 |
|
return true; |
424
|
|
|
} |
425
|
|
|
|
426
|
106 |
|
return $key1->onDelete() !== $key2->onDelete(); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Returns the difference between the fields $field1 and $field2. |
431
|
|
|
* |
432
|
|
|
* If there are differences this method returns $field2, otherwise the |
433
|
|
|
* boolean false. |
434
|
|
|
* |
435
|
|
|
* @param \Doctrine\DBAL\Schema\Column $column1 |
436
|
|
|
* @param \Doctrine\DBAL\Schema\Column $column2 |
437
|
|
|
* |
438
|
|
|
* @return array |
439
|
|
|
*/ |
440
|
2943 |
|
public function diffColumn(Column $column1, Column $column2) |
441
|
|
|
{ |
442
|
2943 |
|
$properties1 = $column1->toArray(); |
443
|
2943 |
|
$properties2 = $column2->toArray(); |
444
|
|
|
|
445
|
2943 |
|
$changedProperties = []; |
446
|
|
|
|
447
|
2943 |
|
foreach (['type', 'notnull', 'unsigned', 'autoincrement'] as $property) { |
448
|
2943 |
|
if ($properties1[$property] != $properties2[$property]) { |
449
|
2943 |
|
$changedProperties[] = $property; |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
// This is a very nasty hack to make comparator work with the legacy json_array type, which should be killed in v3 |
454
|
2943 |
|
if ($this->isALegacyJsonComparison($properties1['type'], $properties2['type'])) { |
|
|
|
|
455
|
38 |
|
array_shift($changedProperties); |
456
|
|
|
|
457
|
38 |
|
$changedProperties[] = 'comment'; |
458
|
|
|
} |
459
|
|
|
|
460
|
2943 |
|
if ($properties1['default'] != $properties2['default'] || |
461
|
|
|
// Null values need to be checked additionally as they tell whether to create or drop a default value. |
462
|
|
|
// null != 0, null != false, null != '' etc. This affects platform's table alteration SQL generation. |
463
|
2877 |
|
(null === $properties1['default'] && null !== $properties2['default']) || |
464
|
2943 |
|
(null === $properties2['default'] && null !== $properties1['default']) |
465
|
|
|
) { |
466
|
86 |
|
$changedProperties[] = 'default'; |
467
|
|
|
} |
468
|
|
|
|
469
|
2943 |
|
if (($properties1['type'] instanceof Types\StringType && ! $properties1['type'] instanceof Types\GuidType) || |
470
|
2943 |
|
$properties1['type'] instanceof Types\BinaryType |
471
|
|
|
) { |
472
|
|
|
// check if value of length is set at all, default value assumed otherwise. |
473
|
622 |
|
$length1 = $properties1['length'] ?: 255; |
474
|
622 |
|
$length2 = $properties2['length'] ?: 255; |
475
|
622 |
|
if ($length1 != $length2) { |
476
|
301 |
|
$changedProperties[] = 'length'; |
477
|
|
|
} |
478
|
|
|
|
479
|
622 |
|
if ($properties1['fixed'] != $properties2['fixed']) { |
480
|
622 |
|
$changedProperties[] = 'fixed'; |
481
|
|
|
} |
482
|
2583 |
|
} elseif ($properties1['type'] instanceof Types\DecimalType) { |
483
|
39 |
|
if (($properties1['precision'] ?: 10) != ($properties2['precision'] ?: 10)) { |
484
|
|
|
$changedProperties[] = 'precision'; |
485
|
|
|
} |
486
|
39 |
|
if ($properties1['scale'] != $properties2['scale']) { |
487
|
|
|
$changedProperties[] = 'scale'; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
// A null value and an empty string are actually equal for a comment so they should not trigger a change. |
492
|
2943 |
|
if ($properties1['comment'] !== $properties2['comment'] && |
493
|
2943 |
|
! (null === $properties1['comment'] && '' === $properties2['comment']) && |
494
|
2943 |
|
! (null === $properties2['comment'] && '' === $properties1['comment']) |
495
|
|
|
) { |
496
|
940 |
|
$changedProperties[] = 'comment'; |
497
|
|
|
} |
498
|
|
|
|
499
|
2943 |
|
$customOptions1 = $column1->getCustomSchemaOptions(); |
500
|
2943 |
|
$customOptions2 = $column2->getCustomSchemaOptions(); |
501
|
|
|
|
502
|
2943 |
|
foreach (array_merge(array_keys($customOptions1), array_keys($customOptions2)) as $key) { |
503
|
40 |
|
if ( ! array_key_exists($key, $properties1) || ! array_key_exists($key, $properties2)) { |
504
|
20 |
|
$changedProperties[] = $key; |
505
|
40 |
|
} elseif ($properties1[$key] !== $properties2[$key]) { |
506
|
40 |
|
$changedProperties[] = $key; |
507
|
|
|
} |
508
|
|
|
} |
509
|
|
|
|
510
|
2943 |
|
$platformOptions1 = $column1->getPlatformOptions(); |
511
|
2943 |
|
$platformOptions2 = $column2->getPlatformOptions(); |
512
|
|
|
|
513
|
2943 |
|
foreach (array_keys(array_intersect_key($platformOptions1, $platformOptions2)) as $key) { |
514
|
40 |
|
if ($properties1[$key] !== $properties2[$key]) { |
515
|
40 |
|
$changedProperties[] = $key; |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
|
519
|
2943 |
|
return array_unique($changedProperties); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* TODO: kill with fire on v3.0 |
524
|
|
|
* |
525
|
|
|
* @deprecated |
526
|
|
|
*/ |
527
|
2943 |
|
private function isALegacyJsonComparison(Types\Type $one, Types\Type $other) : bool |
528
|
|
|
{ |
529
|
2943 |
|
if ( ! $one instanceof Types\JsonType || ! $other instanceof Types\JsonType) { |
530
|
2876 |
|
return false; |
531
|
|
|
} |
532
|
|
|
|
533
|
67 |
|
return ( ! $one instanceof Types\JsonArrayType && $other instanceof Types\JsonArrayType) |
534
|
67 |
|
|| ( ! $other instanceof Types\JsonArrayType && $one instanceof Types\JsonArrayType); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Finds the difference between the indexes $index1 and $index2. |
539
|
|
|
* |
540
|
|
|
* Compares $index1 with $index2 and returns $index2 if there are any |
541
|
|
|
* differences or false in case there are no differences. |
542
|
|
|
* |
543
|
|
|
* @param \Doctrine\DBAL\Schema\Index $index1 |
544
|
|
|
* @param \Doctrine\DBAL\Schema\Index $index2 |
545
|
|
|
* |
546
|
|
|
* @return bool |
547
|
|
|
*/ |
548
|
781 |
|
public function diffIndex(Index $index1, Index $index2) |
549
|
|
|
{ |
550
|
781 |
|
return ! ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1)); |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
|