Commit 04208c6a authored by Sergi de Pablos's avatar Sergi de Pablos

Fix dropping foreign key multiple times with test

In some cases the Comparator class returns multiple drops for the same foreign key.
Specifically, in case you have two tables, A & B, with A having a foreign key FK
referencing B, if you drop table B, the resulting diff shows this FK twice,
once on the diff->orphanedForeignKeys array as we're deleting B, and another on
the diff->changedTables array as table A is also being modified. As a result of this you
get the DROP FOREIGN KEY instruction twice in the final SQL.
parent 804bd86e
......@@ -24,7 +24,7 @@ namespace Doctrine\DBAL\Schema;
*
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
*
*
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
......@@ -46,7 +46,7 @@ class Comparator
/**
* Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema.
*
* The returned differences are returned in such a way that they contain the
* The returned diferences are returned in such a way that they contain the
* operations to change the schema stored in $fromSchema to the schema that is
* stored in $toSchema.
*
......@@ -95,6 +95,16 @@ class Comparator
foreach ($diff->removedTables as $tableName => $table) {
if (isset($foreignKeysToTable[$tableName])) {
$diff->orphanedForeignKeys = array_merge($diff->orphanedForeignKeys, $foreignKeysToTable[$tableName]);
// deleting duplicated foreign keys present on both on the orphanedForeignKey
// and the removedForeignKeys from changedTables
foreach ($foreignKeysToTable[$tableName] as $foreignKey) {
if (isset($diff->changedTables[$foreignKey->getLocalTableName()])) {
foreach ($diff->changedTables[$foreignKey->getLocalTableName()]->removedForeignKeys as $key => $removedForeignKey) {
unset($diff->changedTables[$foreignKey->getLocalTableName()]->removedForeignKeys[$key]);
}
}
}
}
}
......@@ -262,7 +272,7 @@ class Comparator
/**
* Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
* however ambiguities between different possibilities should not lead to renaming at all.
* however ambiguouties between different possibilites should not lead to renaming at all.
*
* @param TableDiff $tableDifferences
*/
......
......@@ -781,6 +781,32 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertCount(0, $diff->removedSequences);
}
/**
* You can get multiple drops for a FK when
*/
public function testAvoidMultipleDropForeignKey()
{
$oldSchema = new Schema();
$tableForeign = $oldSchema->createTable('foreign');
$tableForeign->addColumn('id', 'integer');
$table = $oldSchema->createTable('foo');
$table->addColumn('fk', 'integer');
$table->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
$newSchema = new Schema();
$table = $newSchema->createTable('foo');
$c = new Comparator();
$diff = $c->compare($oldSchema, $newSchema);
$this->assertCount(0, $diff->changedTables['foo']->removedForeignKeys);
}
/**
* @param SchemaDiff $diff
* @param int $newTableCount
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment