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 LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\DBAL\Schema; |
---|
21 | |
---|
22 | /** |
---|
23 | * Compare to Schemas and return an instance of SchemaDiff |
---|
24 | * |
---|
25 | * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. |
---|
26 | * @license http://ez.no/licenses/new_bsd New BSD License |
---|
27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
28 | * @link www.doctrine-project.org |
---|
29 | * @since 2.0 |
---|
30 | * @version $Revision$ |
---|
31 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
32 | */ |
---|
33 | class Comparator |
---|
34 | { |
---|
35 | /** |
---|
36 | * @param Schema $fromSchema |
---|
37 | * @param Schema $toSchema |
---|
38 | * @return SchemaDiff |
---|
39 | */ |
---|
40 | static public function compareSchemas( Schema $fromSchema, Schema $toSchema ) |
---|
41 | { |
---|
42 | $c = new self(); |
---|
43 | return $c->compare($fromSchema, $toSchema); |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema. |
---|
48 | * |
---|
49 | * The returned diferences are returned in such a way that they contain the |
---|
50 | * operations to change the schema stored in $fromSchema to the schema that is |
---|
51 | * stored in $toSchema. |
---|
52 | * |
---|
53 | * @param Schema $fromSchema |
---|
54 | * @param Schema $toSchema |
---|
55 | * |
---|
56 | * @return SchemaDiff |
---|
57 | */ |
---|
58 | public function compare(Schema $fromSchema, Schema $toSchema) |
---|
59 | { |
---|
60 | $diff = new SchemaDiff(); |
---|
61 | |
---|
62 | $foreignKeysToTable = array(); |
---|
63 | |
---|
64 | foreach ( $toSchema->getTables() AS $table ) { |
---|
65 | $tableName = $table->getShortestName($toSchema->getName()); |
---|
66 | if ( ! $fromSchema->hasTable($tableName)) { |
---|
67 | $diff->newTables[$tableName] = $toSchema->getTable($tableName); |
---|
68 | } else { |
---|
69 | $tableDifferences = $this->diffTable($fromSchema->getTable($tableName), $toSchema->getTable($tableName)); |
---|
70 | if ($tableDifferences !== false) { |
---|
71 | $diff->changedTables[$tableName] = $tableDifferences; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /* Check if there are tables removed */ |
---|
77 | foreach ($fromSchema->getTables() AS $table) { |
---|
78 | $tableName = $table->getShortestName($fromSchema->getName()); |
---|
79 | |
---|
80 | $table = $fromSchema->getTable($tableName); |
---|
81 | if ( ! $toSchema->hasTable($tableName) ) { |
---|
82 | $diff->removedTables[$tableName] = $table; |
---|
83 | } |
---|
84 | |
---|
85 | // also remember all foreign keys that point to a specific table |
---|
86 | foreach ($table->getForeignKeys() AS $foreignKey) { |
---|
87 | $foreignTable = strtolower($foreignKey->getForeignTableName()); |
---|
88 | if (!isset($foreignKeysToTable[$foreignTable])) { |
---|
89 | $foreignKeysToTable[$foreignTable] = array(); |
---|
90 | } |
---|
91 | $foreignKeysToTable[$foreignTable][] = $foreignKey; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | foreach ($diff->removedTables AS $tableName => $table) { |
---|
96 | if (isset($foreignKeysToTable[$tableName])) { |
---|
97 | $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | foreach ($toSchema->getSequences() AS $sequence) { |
---|
102 | $sequenceName = $sequence->getShortestName($toSchema->getName()); |
---|
103 | if (!$fromSchema->hasSequence($sequenceName)) { |
---|
104 | $diff->newSequences[] = $sequence; |
---|
105 | } else { |
---|
106 | if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) { |
---|
107 | $diff->changedSequences[] = $toSchema->getSequence($sequenceName); |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | foreach ($fromSchema->getSequences() AS $sequence) { |
---|
113 | $sequenceName = $sequence->getShortestName($fromSchema->getName()); |
---|
114 | if (!$toSchema->hasSequence($sequenceName)) { |
---|
115 | $diff->removedSequences[] = $sequence; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | return $diff; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * |
---|
124 | * @param Sequence $sequence1 |
---|
125 | * @param Sequence $sequence2 |
---|
126 | */ |
---|
127 | public function diffSequence(Sequence $sequence1, Sequence $sequence2) |
---|
128 | { |
---|
129 | if($sequence1->getAllocationSize() != $sequence2->getAllocationSize()) { |
---|
130 | return true; |
---|
131 | } |
---|
132 | |
---|
133 | if($sequence1->getInitialValue() != $sequence2->getInitialValue()) { |
---|
134 | return true; |
---|
135 | } |
---|
136 | |
---|
137 | return false; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * Returns the difference between the tables $table1 and $table2. |
---|
142 | * |
---|
143 | * If there are no differences this method returns the boolean false. |
---|
144 | * |
---|
145 | * @param Table $table1 |
---|
146 | * @param Table $table2 |
---|
147 | * |
---|
148 | * @return bool|TableDiff |
---|
149 | */ |
---|
150 | public function diffTable(Table $table1, Table $table2) |
---|
151 | { |
---|
152 | $changes = 0; |
---|
153 | $tableDifferences = new TableDiff($table1->getName()); |
---|
154 | |
---|
155 | $table1Columns = $table1->getColumns(); |
---|
156 | $table2Columns = $table2->getColumns(); |
---|
157 | |
---|
158 | /* See if all the fields in table 1 exist in table 2 */ |
---|
159 | foreach ( $table2Columns as $columnName => $column ) { |
---|
160 | if ( !$table1->hasColumn($columnName) ) { |
---|
161 | $tableDifferences->addedColumns[$columnName] = $column; |
---|
162 | $changes++; |
---|
163 | } |
---|
164 | } |
---|
165 | /* See if there are any removed fields in table 2 */ |
---|
166 | foreach ( $table1Columns as $columnName => $column ) { |
---|
167 | if ( !$table2->hasColumn($columnName) ) { |
---|
168 | $tableDifferences->removedColumns[$columnName] = $column; |
---|
169 | $changes++; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | foreach ( $table1Columns as $columnName => $column ) { |
---|
174 | if ( $table2->hasColumn($columnName) ) { |
---|
175 | $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) ); |
---|
176 | if (count($changedProperties) ) { |
---|
177 | $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); |
---|
178 | $tableDifferences->changedColumns[$column->getName()] = $columnDiff; |
---|
179 | $changes++; |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | $this->detectColumnRenamings($tableDifferences); |
---|
185 | |
---|
186 | $table1Indexes = $table1->getIndexes(); |
---|
187 | $table2Indexes = $table2->getIndexes(); |
---|
188 | |
---|
189 | foreach ($table2Indexes AS $index2Name => $index2Definition) { |
---|
190 | foreach ($table1Indexes AS $index1Name => $index1Definition) { |
---|
191 | if ($this->diffIndex($index1Definition, $index2Definition) === false) { |
---|
192 | unset($table1Indexes[$index1Name]); |
---|
193 | unset($table2Indexes[$index2Name]); |
---|
194 | } else { |
---|
195 | if ($index1Name == $index2Name) { |
---|
196 | $tableDifferences->changedIndexes[$index2Name] = $table2Indexes[$index2Name]; |
---|
197 | unset($table1Indexes[$index1Name]); |
---|
198 | unset($table2Indexes[$index2Name]); |
---|
199 | $changes++; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | foreach ($table1Indexes AS $index1Name => $index1Definition) { |
---|
206 | $tableDifferences->removedIndexes[$index1Name] = $index1Definition; |
---|
207 | $changes++; |
---|
208 | } |
---|
209 | |
---|
210 | foreach ($table2Indexes AS $index2Name => $index2Definition) { |
---|
211 | $tableDifferences->addedIndexes[$index2Name] = $index2Definition; |
---|
212 | $changes++; |
---|
213 | } |
---|
214 | |
---|
215 | $fromFkeys = $table1->getForeignKeys(); |
---|
216 | $toFkeys = $table2->getForeignKeys(); |
---|
217 | |
---|
218 | foreach ($fromFkeys AS $key1 => $constraint1) { |
---|
219 | foreach ($toFkeys AS $key2 => $constraint2) { |
---|
220 | if($this->diffForeignKey($constraint1, $constraint2) === false) { |
---|
221 | unset($fromFkeys[$key1]); |
---|
222 | unset($toFkeys[$key2]); |
---|
223 | } else { |
---|
224 | if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) { |
---|
225 | $tableDifferences->changedForeignKeys[] = $constraint2; |
---|
226 | $changes++; |
---|
227 | unset($fromFkeys[$key1]); |
---|
228 | unset($toFkeys[$key2]); |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | foreach ($fromFkeys AS $key1 => $constraint1) { |
---|
235 | $tableDifferences->removedForeignKeys[] = $constraint1; |
---|
236 | $changes++; |
---|
237 | } |
---|
238 | |
---|
239 | foreach ($toFkeys AS $key2 => $constraint2) { |
---|
240 | $tableDifferences->addedForeignKeys[] = $constraint2; |
---|
241 | $changes++; |
---|
242 | } |
---|
243 | |
---|
244 | return $changes ? $tableDifferences : false; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop |
---|
249 | * however ambiguouties between different possibilites should not lead to renaming at all. |
---|
250 | * |
---|
251 | * @param TableDiff $tableDifferences |
---|
252 | */ |
---|
253 | private function detectColumnRenamings(TableDiff $tableDifferences) |
---|
254 | { |
---|
255 | $renameCandidates = array(); |
---|
256 | foreach ($tableDifferences->addedColumns AS $addedColumnName => $addedColumn) { |
---|
257 | foreach ($tableDifferences->removedColumns AS $removedColumnName => $removedColumn) { |
---|
258 | if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) { |
---|
259 | $renameCandidates[$addedColumn->getName()][] = array($removedColumn, $addedColumn, $addedColumnName); |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | foreach ($renameCandidates AS $candidate => $candidateColumns) { |
---|
265 | if (count($candidateColumns) == 1) { |
---|
266 | list($removedColumn, $addedColumn) = $candidateColumns[0]; |
---|
267 | $removedColumnName = strtolower($removedColumn->getName()); |
---|
268 | $addedColumnName = strtolower($addedColumn->getName()); |
---|
269 | |
---|
270 | $tableDifferences->renamedColumns[$removedColumnName] = $addedColumn; |
---|
271 | unset($tableDifferences->addedColumns[$addedColumnName]); |
---|
272 | unset($tableDifferences->removedColumns[$removedColumnName]); |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | /** |
---|
278 | * @param ForeignKeyConstraint $key1 |
---|
279 | * @param ForeignKeyConstraint $key2 |
---|
280 | * @return bool |
---|
281 | */ |
---|
282 | public function diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2) |
---|
283 | { |
---|
284 | if (array_map('strtolower', $key1->getLocalColumns()) != array_map('strtolower', $key2->getLocalColumns())) { |
---|
285 | return true; |
---|
286 | } |
---|
287 | |
---|
288 | if (array_map('strtolower', $key1->getForeignColumns()) != array_map('strtolower', $key2->getForeignColumns())) { |
---|
289 | return true; |
---|
290 | } |
---|
291 | |
---|
292 | if ($key1->onUpdate() != $key2->onUpdate()) { |
---|
293 | return true; |
---|
294 | } |
---|
295 | |
---|
296 | if ($key1->onDelete() != $key2->onDelete()) { |
---|
297 | return true; |
---|
298 | } |
---|
299 | |
---|
300 | return false; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * Returns the difference between the fields $field1 and $field2. |
---|
305 | * |
---|
306 | * If there are differences this method returns $field2, otherwise the |
---|
307 | * boolean false. |
---|
308 | * |
---|
309 | * @param Column $column1 |
---|
310 | * @param Column $column2 |
---|
311 | * |
---|
312 | * @return array |
---|
313 | */ |
---|
314 | public function diffColumn(Column $column1, Column $column2) |
---|
315 | { |
---|
316 | $changedProperties = array(); |
---|
317 | if ( $column1->getType() != $column2->getType() ) { |
---|
318 | $changedProperties[] = 'type'; |
---|
319 | } |
---|
320 | |
---|
321 | if ($column1->getNotnull() != $column2->getNotnull()) { |
---|
322 | $changedProperties[] = 'notnull'; |
---|
323 | } |
---|
324 | |
---|
325 | if ($column1->getDefault() != $column2->getDefault()) { |
---|
326 | $changedProperties[] = 'default'; |
---|
327 | } |
---|
328 | |
---|
329 | if ($column1->getUnsigned() != $column2->getUnsigned()) { |
---|
330 | $changedProperties[] = 'unsigned'; |
---|
331 | } |
---|
332 | |
---|
333 | if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) { |
---|
334 | // check if value of length is set at all, default value assumed otherwise. |
---|
335 | $length1 = $column1->getLength() ?: 255; |
---|
336 | $length2 = $column2->getLength() ?: 255; |
---|
337 | if ($length1 != $length2) { |
---|
338 | $changedProperties[] = 'length'; |
---|
339 | } |
---|
340 | |
---|
341 | if ($column1->getFixed() != $column2->getFixed()) { |
---|
342 | $changedProperties[] = 'fixed'; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) { |
---|
347 | if (($column1->getPrecision()?:10) != ($column2->getPrecision()?:10)) { |
---|
348 | $changedProperties[] = 'precision'; |
---|
349 | } |
---|
350 | if ($column1->getScale() != $column2->getScale()) { |
---|
351 | $changedProperties[] = 'scale'; |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | if ($column1->getAutoincrement() != $column2->getAutoincrement()) { |
---|
356 | $changedProperties[] = 'autoincrement'; |
---|
357 | } |
---|
358 | |
---|
359 | // only allow to delete comment if its set to '' not to null. |
---|
360 | if ($column1->getComment() !== null && $column1->getComment() != $column2->getComment()) { |
---|
361 | $changedProperties[] = 'comment'; |
---|
362 | } |
---|
363 | |
---|
364 | $options1 = $column1->getCustomSchemaOptions(); |
---|
365 | $options2 = $column2->getCustomSchemaOptions(); |
---|
366 | |
---|
367 | $commonKeys = array_keys(array_intersect_key($options1, $options2)); |
---|
368 | |
---|
369 | foreach ($commonKeys as $key) { |
---|
370 | if ($options1[$key] !== $options2[$key]) { |
---|
371 | $changedProperties[] = $key; |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | $diffKeys = array_keys(array_diff_key($options1, $options2) + array_diff_key($options2, $options1)); |
---|
376 | |
---|
377 | $changedProperties = array_merge($changedProperties, $diffKeys); |
---|
378 | |
---|
379 | return $changedProperties; |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Finds the difference between the indexes $index1 and $index2. |
---|
384 | * |
---|
385 | * Compares $index1 with $index2 and returns $index2 if there are any |
---|
386 | * differences or false in case there are no differences. |
---|
387 | * |
---|
388 | * @param Index $index1 |
---|
389 | * @param Index $index2 |
---|
390 | * @return bool |
---|
391 | */ |
---|
392 | public function diffIndex(Index $index1, Index $index2) |
---|
393 | { |
---|
394 | if ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1)) { |
---|
395 | return false; |
---|
396 | } |
---|
397 | return true; |
---|
398 | } |
---|
399 | } |
---|