Skip to content

Commit

Permalink
Merge pull request #4337 from mvorisek/from_to_instead_1_2
Browse files Browse the repository at this point in the history
Rename Comparator::diffTable() params - from/to instead of 1/2
  • Loading branch information
greg0ire authored Oct 15, 2020
2 parents 908cdc6 + 032b602 commit 29bf315
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions lib/Doctrine/DBAL/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,48 +188,48 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2)
}

/**
* Returns the difference between the tables $table1 and $table2.
* Returns the difference between the tables $fromTable and $toTable.
*
* If there are no differences this method returns the boolean false.
*
* @return TableDiff|false
*/
public function diffTable(Table $table1, Table $table2)
public function diffTable(Table $fromTable, Table $toTable)
{
$changes = 0;
$tableDifferences = new TableDiff($table1->getName());
$tableDifferences->fromTable = $table1;
$tableDifferences = new TableDiff($fromTable->getName());
$tableDifferences->fromTable = $fromTable;

$table1Columns = $table1->getColumns();
$table2Columns = $table2->getColumns();
$fromTableColumns = $fromTable->getColumns();
$toTableColumns = $toTable->getColumns();

/* See if all the columns in table 1 exist in table 2 */
foreach ($table2Columns as $columnName => $column) {
if ($table1->hasColumn($columnName)) {
/* See if all the columns in "from" table exist in "to" table */
foreach ($toTableColumns as $columnName => $column) {
if ($fromTable->hasColumn($columnName)) {
continue;
}

$tableDifferences->addedColumns[$columnName] = $column;
$changes++;
}

/* See if there are any removed columns in table 2 */
foreach ($table1Columns as $columnName => $column) {
// See if column is removed in table 2.
if (! $table2->hasColumn($columnName)) {
/* See if there are any removed columns in "to" table */
foreach ($fromTableColumns as $columnName => $column) {
// See if column is removed in "to" table.
if (! $toTable->hasColumn($columnName)) {
$tableDifferences->removedColumns[$columnName] = $column;
$changes++;
continue;
}

// See if column has changed properties in table 2.
$changedProperties = $this->diffColumn($column, $table2->getColumn($columnName));
// See if column has changed properties in "to" table.
$changedProperties = $this->diffColumn($column, $toTable->getColumn($columnName));

if (empty($changedProperties)) {
continue;
}

$columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
$columnDiff = new ColumnDiff($column->getName(), $toTable->getColumn($columnName), $changedProperties);

$columnDiff->fromColumn = $column;
$tableDifferences->changedColumns[$column->getName()] = $columnDiff;
Expand All @@ -238,69 +238,69 @@ public function diffTable(Table $table1, Table $table2)

$this->detectColumnRenamings($tableDifferences);

$table1Indexes = $table1->getIndexes();
$table2Indexes = $table2->getIndexes();
$fromTableIndexes = $fromTable->getIndexes();
$toTableIndexes = $toTable->getIndexes();

/* See if all the indexes in table 1 exist in table 2 */
foreach ($table2Indexes as $indexName => $index) {
if (($index->isPrimary() && $table1->hasPrimaryKey()) || $table1->hasIndex($indexName)) {
/* See if all the indexes in "from" table exist in "to" table */
foreach ($toTableIndexes as $indexName => $index) {
if (($index->isPrimary() && $fromTable->hasPrimaryKey()) || $fromTable->hasIndex($indexName)) {
continue;
}

$tableDifferences->addedIndexes[$indexName] = $index;
$changes++;
}

/* See if there are any removed indexes in table 2 */
foreach ($table1Indexes as $indexName => $index) {
// See if index is removed in table 2.
/* See if there are any removed indexes in "to" table */
foreach ($fromTableIndexes as $indexName => $index) {
// See if index is removed in "to" table.
if (
($index->isPrimary() && ! $table2->hasPrimaryKey()) ||
! $index->isPrimary() && ! $table2->hasIndex($indexName)
($index->isPrimary() && ! $toTable->hasPrimaryKey()) ||
! $index->isPrimary() && ! $toTable->hasIndex($indexName)
) {
$tableDifferences->removedIndexes[$indexName] = $index;
$changes++;
continue;
}

// See if index has changed in table 2.
$table2Index = $index->isPrimary() ? $table2->getPrimaryKey() : $table2->getIndex($indexName);
assert($table2Index instanceof Index);
// See if index has changed in "to" table.
$toTableIndex = $index->isPrimary() ? $toTable->getPrimaryKey() : $toTable->getIndex($indexName);
assert($toTableIndex instanceof Index);

if (! $this->diffIndex($index, $table2Index)) {
if (! $this->diffIndex($index, $toTableIndex)) {
continue;
}

$tableDifferences->changedIndexes[$indexName] = $table2Index;
$tableDifferences->changedIndexes[$indexName] = $toTableIndex;
$changes++;
}

$this->detectIndexRenamings($tableDifferences);

$fromFkeys = $table1->getForeignKeys();
$toFkeys = $table2->getForeignKeys();
$fromForeignKeys = $fromTable->getForeignKeys();
$toForeignKeys = $toTable->getForeignKeys();

foreach ($fromFkeys as $key1 => $constraint1) {
foreach ($toFkeys as $key2 => $constraint2) {
if ($this->diffForeignKey($constraint1, $constraint2) === false) {
unset($fromFkeys[$key1], $toFkeys[$key2]);
foreach ($fromForeignKeys as $fromKey => $fromConstraint) {
foreach ($toForeignKeys as $toKey => $toConstraint) {
if ($this->diffForeignKey($fromConstraint, $toConstraint) === false) {
unset($fromForeignKeys[$fromKey], $toForeignKeys[$toKey]);
} else {
if (strtolower($constraint1->getName()) === strtolower($constraint2->getName())) {
$tableDifferences->changedForeignKeys[] = $constraint2;
if (strtolower($fromConstraint->getName()) === strtolower($toConstraint->getName())) {
$tableDifferences->changedForeignKeys[] = $toConstraint;
$changes++;
unset($fromFkeys[$key1], $toFkeys[$key2]);
unset($fromForeignKeys[$fromKey], $toForeignKeys[$toKey]);
}
}
}
}

foreach ($fromFkeys as $constraint1) {
$tableDifferences->removedForeignKeys[] = $constraint1;
foreach ($fromForeignKeys as $fromConstraint) {
$tableDifferences->removedForeignKeys[] = $fromConstraint;
$changes++;
}

foreach ($toFkeys as $constraint2) {
$tableDifferences->addedForeignKeys[] = $constraint2;
foreach ($toForeignKeys as $toConstraint) {
$tableDifferences->addedForeignKeys[] = $toConstraint;
$changes++;
}

Expand Down

0 comments on commit 29bf315

Please sign in to comment.