Commit 9a467f1e authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #212 from sdepablos/master

Fix dropping foreign key multiple times with test
parents 804bd86e 283ff9b9
......@@ -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$
......@@ -95,6 +95,18 @@ 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) {
// strtolower the table name to make if compatible with getShortestName
$localTableName = strtolower($foreignKey->getLocalTableName());
if (isset($diff->changedTables[$localTableName])) {
foreach ($diff->changedTables[$localTableName]->removedForeignKeys as $key => $removedForeignKey) {
unset($diff->changedTables[$localTableName]->removedForeignKeys[$key]);
}
}
}
}
}
......
......@@ -781,6 +781,36 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertCount(0, $diff->removedSequences);
}
/**
* You can get multiple drops for a FK when a table referenced by a foreign
* key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys
* array because of the dropped table, and once on changedTables array. We
* now check that the key is present once.
*/
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);
$this->assertCount(1, $diff->orphanedForeignKeys);
}
/**
* @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