Commit 45664da5 authored by Marco Pivetta's avatar Marco Pivetta

Hotfix for DBAL-669

Updating a schema should introduce also schema creation statements in the generated SQL
parent 83f92c3f
......@@ -63,6 +63,12 @@ class Comparator
$foreignKeysToTable = array();
foreach ($toSchema->getTables() as $table) {
$namespace = $table->getNamespaceName();
if ($namespace && ! $fromSchema->hasNamespace($namespace)) {
$diff->newNamespaces[$namespace] = $namespace;
}
$tableName = $table->getShortestName($toSchema->getName());
if ( ! $fromSchema->hasTable($tableName)) {
$diff->newTables[$tableName] = $toSchema->getTable($tableName);
......
......@@ -177,6 +177,24 @@ class Schema extends AbstractAsset
return strtolower($name);
}
/**
* Does this schema have a namespace with the given name?
*
* @param string $namespaceName
*
* @return boolean
*/
public function hasNamespace($namespaceName)
{
foreach ($this->_tables as $table) {
if ($table->getNamespaceName() === $namespaceName) {
return true;
}
}
return false;
}
/**
* Does this schema have a table with the given name?
*
......
......@@ -37,6 +37,13 @@ class SchemaDiff
*/
public $fromSchema;
/**
* All added namespaces.
*
* @var string[]
*/
public $newNamespaces = array();
/**
* All added tables.
*
......@@ -132,6 +139,12 @@ class SchemaDiff
{
$sql = array();
if ($platform->supportsSchemas()) {
foreach ($this->newNamespaces as $newNamespace) {
$sql[] = $platform->getCreateSchemaSQL($newNamespace);
}
}
if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
$sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
......
......@@ -802,10 +802,38 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$expected = new SchemaDiff();
$expected->fromSchema = $oldSchema;
$expected->newNamespaces['foo'] = 'foo';
$this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
}
/**
* @group DBAL-669
*/
public function testNamespacesComparison()
{
$config = new SchemaConfig();
$config->setName("schemaName");
$oldSchema = new Schema(array(), array(), $config);
$oldSchema->createTable('taz');
$oldSchema->createTable('war.tab');
$newSchema= new Schema(array(), array(), $config);
$newSchema->createTable('bar.tab');
$newSchema->createTable('baz.tab');
$newSchema->createTable('war.tab');
$expected = new SchemaDiff();
$expected->fromSchema = $oldSchema;
$expected->newNamespaces = array('bar' => 'bar', 'baz' => 'baz');
$diff = Comparator::compareSchemas($oldSchema, $newSchema);
$this->assertEquals(array('bar' => 'bar', 'baz' => 'baz'), $diff->newNamespaces);
$this->assertCount(2, $diff->newTables);
}
/**
* @group DBAL-204
*/
......
......@@ -221,4 +221,28 @@ class SchemaTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($schema->hasTable('`foo`'));
}
/**
* @group DBAL-669
*/
public function testHasNamespace()
{
$schema = new Schema();
$this->assertFalse($schema->hasNamespace('foo'));
$schema->createTable('foo');
$this->assertFalse($schema->hasNamespace('foo'));
$schema->createTable('bar.baz');
$this->assertFalse($schema->hasNamespace('baz'));
$this->assertTrue($schema->hasNamespace('bar'));
$this->assertFalse($schema->hasNamespace('tab'));
$schema->createTable('tab.taz');
$this->assertTrue($schema->hasNamespace('tab'));
}
}
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