MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Comparator.php
Ir para a documentação deste ficheiro.
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
20namespace Doctrine\DBAL\Schema;
21
34{
40 static public function compareSchemas( Schema $fromSchema, Schema $toSchema )
41 {
42 $c = new self();
43 return $c->compare($fromSchema, $toSchema);
44 }
45
58 public function compare(Schema $fromSchema, Schema $toSchema)
59 {
60 $diff = new SchemaDiff();
61
62 $foreignKeysToTable = array();
63
64 foreach ( $toSchema->getTables() AS $tableName => $table ) {
65 if ( !$fromSchema->hasTable($tableName) ) {
66 $diff->newTables[$tableName] = $table;
67 } else {
68 $tableDifferences = $this->diffTable( $fromSchema->getTable($tableName), $table );
69 if ( $tableDifferences !== false ) {
70 $diff->changedTables[$tableName] = $tableDifferences;
71 }
72 }
73 }
74
75 /* Check if there are tables removed */
76 foreach ( $fromSchema->getTables() AS $tableName => $table ) {
77 if ( !$toSchema->hasTable($tableName) ) {
78 $diff->removedTables[$tableName] = $table;
79 }
80
81 // also remember all foreign keys that point to a specific table
82 foreach ($table->getForeignKeys() AS $foreignKey) {
83 $foreignTable = strtolower($foreignKey->getForeignTableName());
84 if (!isset($foreignKeysToTable[$foreignTable])) {
85 $foreignKeysToTable[$foreignTable] = array();
86 }
87 $foreignKeysToTable[$foreignTable][] = $foreignKey;
88 }
89 }
90
91 foreach ($diff->removedTables AS $tableName => $table) {
92 if (isset($foreignKeysToTable[$tableName])) {
93 $diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
94 }
95 }
96
97 foreach ( $toSchema->getSequences() AS $sequenceName => $sequence) {
98 if (!$fromSchema->hasSequence($sequenceName)) {
99 $diff->newSequences[] = $sequence;
100 } else {
101 if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
102 $diff->changedSequences[] = $fromSchema->getSequence($sequenceName);
103 }
104 }
105 }
106
107 foreach ($fromSchema->getSequences() AS $sequenceName => $sequence) {
108 if (!$toSchema->hasSequence($sequenceName)) {
109 $diff->removedSequences[] = $sequence;
110 }
111 }
112
113 return $diff;
114 }
115
121 public function diffSequence(Sequence $sequence1, Sequence $sequence2)
122 {
123 if($sequence1->getAllocationSize() != $sequence2->getAllocationSize()) {
124 return true;
125 }
126
127 if($sequence1->getInitialValue() != $sequence2->getInitialValue()) {
128 return true;
129 }
130
131 return false;
132 }
133
144 public function diffTable(Table $table1, Table $table2)
145 {
146 $changes = 0;
147 $tableDifferences = new TableDiff($table1->getName());
148
149 $table1Columns = $table1->getColumns();
150 $table2Columns = $table2->getColumns();
151
152 /* See if all the fields in table 1 exist in table 2 */
153 foreach ( $table2Columns as $columnName => $column ) {
154 if ( !$table1->hasColumn($columnName) ) {
155 $tableDifferences->addedColumns[$columnName] = $column;
156 $changes++;
157 }
158 }
159 /* See if there are any removed fields in table 2 */
160 foreach ( $table1Columns as $columnName => $column ) {
161 if ( !$table2->hasColumn($columnName) ) {
162 $tableDifferences->removedColumns[$columnName] = $column;
163 $changes++;
164 }
165 }
166 foreach ( $table1Columns as $columnName => $column ) {
167 if ( $table2->hasColumn($columnName) ) {
168 $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) );
169 if (count($changedProperties) ) {
170 $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
171 $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
172 $changes++;
173 }
174 }
175 }
176
177 $this->detectColumnRenamings($tableDifferences);
178
179 $table1Indexes = $table1->getIndexes();
180 $table2Indexes = $table2->getIndexes();
181
182 foreach ($table2Indexes AS $index2Name => $index2Definition) {
183 foreach ($table1Indexes AS $index1Name => $index1Definition) {
184 if ($this->diffIndex($index1Definition, $index2Definition) === false) {
185 unset($table1Indexes[$index1Name]);
186 unset($table2Indexes[$index2Name]);
187 } else {
188 if ($index1Name == $index2Name) {
189 $tableDifferences->changedIndexes[$index2Name] = $table2Indexes[$index2Name];
190 unset($table1Indexes[$index1Name]);
191 unset($table2Indexes[$index2Name]);
192 $changes++;
193 }
194 }
195 }
196 }
197
198 foreach ($table1Indexes AS $index1Name => $index1Definition) {
199 $tableDifferences->removedIndexes[$index1Name] = $index1Definition;
200 $changes++;
201 }
202
203 foreach ($table2Indexes AS $index2Name => $index2Definition) {
204 $tableDifferences->addedIndexes[$index2Name] = $index2Definition;
205 $changes++;
206 }
207
208 $fromFkeys = $table1->getForeignKeys();
209 $toFkeys = $table2->getForeignKeys();
210
211 foreach ($fromFkeys AS $key1 => $constraint1) {
212 foreach ($toFkeys AS $key2 => $constraint2) {
213 if($this->diffForeignKey($constraint1, $constraint2) === false) {
214 unset($fromFkeys[$key1]);
215 unset($toFkeys[$key2]);
216 } else {
217 if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) {
218 $tableDifferences->changedForeignKeys[] = $constraint2;
219 $changes++;
220 unset($fromFkeys[$key1]);
221 unset($toFkeys[$key2]);
222 }
223 }
224 }
225 }
226
227 foreach ($fromFkeys AS $key1 => $constraint1) {
228 $tableDifferences->removedForeignKeys[] = $constraint1;
229 $changes++;
230 }
231
232 foreach ($toFkeys AS $key2 => $constraint2) {
233 $tableDifferences->addedForeignKeys[] = $constraint2;
234 $changes++;
235 }
236
237 return $changes ? $tableDifferences : false;
238 }
239
246 private function detectColumnRenamings(TableDiff $tableDifferences)
247 {
248 $renameCandidates = array();
249 foreach ($tableDifferences->addedColumns AS $addedColumnName => $addedColumn) {
250 foreach ($tableDifferences->removedColumns AS $removedColumnName => $removedColumn) {
251 if (count($this->diffColumn($addedColumn, $removedColumn)) == 0) {
252 $renameCandidates[$addedColumn->getName()][] = array($removedColumn, $addedColumn);
253 }
254 }
255 }
256
257 foreach ($renameCandidates AS $candidate => $candidateColumns) {
258 if (count($candidateColumns) == 1) {
259 list($removedColumn, $addedColumn) = $candidateColumns[0];
260
261 $tableDifferences->renamedColumns[$removedColumn->getName()] = $addedColumn;
262 unset($tableDifferences->addedColumns[$addedColumnName]);
263 unset($tableDifferences->removedColumns[$removedColumnName]);
264 }
265 }
266 }
267
274 {
275 if (array_map('strtolower', $key1->getLocalColumns()) != array_map('strtolower', $key2->getLocalColumns())) {
276 return true;
277 }
278
279 if (array_map('strtolower', $key1->getForeignColumns()) != array_map('strtolower', $key2->getForeignColumns())) {
280 return true;
281 }
282
283 if ($key1->onUpdate() != $key2->onUpdate()) {
284 return true;
285 }
286
287 if ($key1->onDelete() != $key2->onDelete()) {
288 return true;
289 }
290
291 return false;
292 }
293
305 public function diffColumn(Column $column1, Column $column2)
306 {
307 $changedProperties = array();
308 if ( $column1->getType() != $column2->getType() ) {
309 $changedProperties[] = 'type';
310 }
311
312 if ($column1->getNotnull() != $column2->getNotnull()) {
313 $changedProperties[] = 'notnull';
314 }
315
316 if ($column1->getDefault() != $column2->getDefault()) {
317 $changedProperties[] = 'default';
318 }
319
320 if ($column1->getUnsigned() != $column2->getUnsigned()) {
321 $changedProperties[] = 'unsigned';
322 }
323
324 if ($column1->getType() instanceof \Doctrine\DBAL\Types\StringType) {
325 if ($column1->getLength() != $column2->getLength()) {
326 $changedProperties[] = 'length';
327 }
328
329 if ($column1->getFixed() != $column2->getFixed()) {
330 $changedProperties[] = 'fixed';
331 }
332 }
333
334 if ($column1->getType() instanceof \Doctrine\DBAL\Types\DecimalType) {
335 if ($column1->getPrecision() != $column2->getPrecision()) {
336 $changedProperties[] = 'precision';
337 }
338 if ($column1->getScale() != $column2->getScale()) {
339 $changedProperties[] = 'scale';
340 }
341 }
342
343 if ($column1->getAutoincrement() != $column2->getAutoincrement()) {
344 $changedProperties[] = 'autoincrement';
345 }
346
347 return $changedProperties;
348 }
349
360 public function diffIndex(Index $index1, Index $index2)
361 {
362 if ($index1->isFullfilledBy($index2) && $index2->isFullfilledBy($index1)) {
363 return false;
364 }
365 return true;
366 }
367}
diffColumn(Column $column1, Column $column2)
Definição Comparator.php:305
diffTable(Table $table1, Table $table2)
Definição Comparator.php:144
diffIndex(Index $index1, Index $index2)
Definição Comparator.php:360
static compareSchemas(Schema $fromSchema, Schema $toSchema)
Definição Comparator.php:40
compare(Schema $fromSchema, Schema $toSchema)
Definição Comparator.php:58
diffForeignKey(ForeignKeyConstraint $key1, ForeignKeyConstraint $key2)
Definição Comparator.php:273
diffSequence(Sequence $sequence1, Sequence $sequence2)
Definição Comparator.php:121
isFullfilledBy(Index $other)
Definição Index.php:131
hasSequence($sequenceName)
Definição Schema.php:150
getSequence($sequenceName)
Definição Schema.php:161
hasColumn($columnName)
Definição Table.php:497
getColumn($columnName)
Definição Table.php:509