Commit c10d4679 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-204] Remove foreign keys pointing to removed namespace schema tables.

parent 793d249e
...@@ -465,6 +465,16 @@ class Table extends AbstractAsset ...@@ -465,6 +465,16 @@ class Table extends AbstractAsset
return $this->_fkConstraints[$constraintName]; return $this->_fkConstraints[$constraintName];
} }
public function removeForeignKey($constraintName)
{
$constraintName = strtolower($constraintName);
if(!$this->hasForeignKey($constraintName)) {
throw SchemaException::foreignKeyDoesNotExist($constraintName, $this->_name);
}
unset($this->_fkConstraints[$constraintName]);
}
/** /**
* @return Column[] * @return Column[]
*/ */
......
...@@ -89,6 +89,10 @@ class RemoveNamespacedAssets implements Visitor ...@@ -89,6 +89,10 @@ class RemoveNamespacedAssets implements Visitor
*/ */
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
{ {
$foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
if ( ! $foreignTable->isInDefaultNamespace($this->schema->getName()) ) {
$localTable->removeForeignKey($fkConstraint->getName());
}
} }
/** /**
......
...@@ -5,6 +5,7 @@ namespace Doctrine\Tests\DBAL\Schema\Visitor; ...@@ -5,6 +5,7 @@ namespace Doctrine\Tests\DBAL\Schema\Visitor;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaConfig; use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets; use Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets;
use Doctrine\DBAL\Platforms\MySqlPlatform;
class RemoveNamespacedAssetsTest extends \PHPUnit_Framework_TestCase class RemoveNamespacedAssetsTest extends \PHPUnit_Framework_TestCase
{ {
...@@ -26,4 +27,28 @@ class RemoveNamespacedAssetsTest extends \PHPUnit_Framework_TestCase ...@@ -26,4 +27,28 @@ class RemoveNamespacedAssetsTest extends \PHPUnit_Framework_TestCase
$tables = $schema->getTables(); $tables = $schema->getTables();
$this->assertEquals(array("test.test", "test.baz"), array_keys($tables), "Only 2 tables should be present, both in 'test' namespace."); $this->assertEquals(array("test.test", "test.baz"), array_keys($tables), "Only 2 tables should be present, both in 'test' namespace.");
} }
/**
* @group DBAL-204
*/
public function testCleanupForeignKeys()
{
$config = new SchemaConfig;
$config->setName("test");
$schema = new Schema(array(), array(), $config);
$testTable = $schema->createTable("test.test");
$testTable->addColumn('id', 'integer');
$fooTable = $schema->createTable("foo.bar");
$fooTable->addColumn('id', 'integer');
$testTable->addForeignKeyConstraint("foo.bar", array("id"), array("id"));
$schema->visit(new RemoveNamespacedAssets());
$sql = $schema->toSql(new MySqlPlatform());
$this->assertEquals(1, count($sql), "Just one CREATE TABLE statement, no foreign key and table to foo.bar");
}
} }
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