Commit 8b28a9a1 authored by Guilherme Blanco's avatar Guilherme Blanco

Merge pull request #435 from deeky666/DBAL-400

[DBAL-400] Fix adding primary key during table alteration in MySQL
parents 4b74a50f 09101dd2
...@@ -602,13 +602,16 @@ class MySqlPlatform extends AbstractPlatform ...@@ -602,13 +602,16 @@ class MySqlPlatform extends AbstractPlatform
foreach ($diff->addedIndexes as $addKey => $addIndex) { foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() == $addIndex->getColumns()) { if ($remIndex->getColumns() == $addIndex->getColumns()) {
$type = ''; $indexClause = 'INDEX ' . $addIndex->getName();
if ($addIndex->isUnique()) {
$type = 'UNIQUE '; if ($addIndex->isPrimary()) {
$indexClause = 'PRIMARY KEY';
} elseif ($addIndex->isUnique()) {
$indexClause = 'UNIQUE INDEX ' . $addIndex->getName();
} }
$query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', '; $query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', ';
$query .= 'ADD ' . $type . 'INDEX ' . $addIndex->getName(); $query .= 'ADD ' . $indexClause;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex->getQuotedColumns($this)) . ')'; $query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex->getQuotedColumns($this)) . ')';
$sql[] = $query; $sql[] = $query;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\Tests\DBAL\Functional\Schema; namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Schema;
...@@ -21,7 +22,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -21,7 +22,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$tableNew = clone $tableFetched; $tableNew = clone $tableFetched;
$tableNew->setPrimaryKey(array('bar_id', 'foo_id')); $tableNew->setPrimaryKey(array('bar_id', 'foo_id'));
$comparator = new \Doctrine\DBAL\Schema\Comparator; $comparator = new Comparator;
$this->_sm->alterTable($comparator->diffTable($tableFetched, $tableNew)); $this->_sm->alterTable($comparator->diffTable($tableFetched, $tableNew));
} }
...@@ -42,7 +43,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -42,7 +43,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->_sm->createTable($table); $this->_sm->createTable($table);
$tableFetched = $this->_sm->listTableDetails("diffbug_routing_translations"); $tableFetched = $this->_sm->listTableDetails("diffbug_routing_translations");
$comparator = new \Doctrine\DBAL\Schema\Comparator; $comparator = new Comparator;
$diff = $comparator->diffTable($tableFetched, $table); $diff = $comparator->diffTable($tableFetched, $table);
$this->assertFalse($diff, "no changes expected."); $this->assertFalse($diff, "no changes expected.");
...@@ -64,4 +65,30 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -64,4 +65,30 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertArrayHasKey('f_index', $indexes); $this->assertArrayHasKey('f_index', $indexes);
$this->assertTrue($indexes['f_index']->hasFlag('fulltext')); $this->assertTrue($indexes['f_index']->hasFlag('fulltext'));
} }
/**
* @group DBAL-400
*/
public function testAlterTableAddPrimaryKey()
{
$table = new Table('alter_table_add_pk');
$table->addColumn('id', 'integer');
$table->addColumn('foo', 'integer');
$table->addIndex(array('id'), 'idx_id');
$this->_sm->createTable($table);
$comparator = new Comparator();
$diffTable = clone $table;
$diffTable->dropIndex('idx_id');
$diffTable->setPrimaryKey(array('id'));
$this->_sm->alterTable($comparator->diffTable($table, $diffTable));
$table = $this->_sm->listTableDetails("alter_table_add_pk");
$this->assertFalse($table->hasIndex('idx_id'));
$this->assertTrue($table->hasPrimaryKey());
}
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL\Platforms; namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
...@@ -295,4 +296,26 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -295,4 +296,26 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216))); $this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216)));
$this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array())); $this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array()));
} }
/**
* @group DBAL-400
*/
public function testAlterTableAddPrimaryKey()
{
$table = new Table('alter_table_add_pk');
$table->addColumn('id', 'integer');
$table->addColumn('foo', 'integer');
$table->addIndex(array('id'), 'idx_id');
$comparator = new Comparator();
$diffTable = clone $table;
$diffTable->dropIndex('idx_id');
$diffTable->setPrimaryKey(array('id'));
$this->assertEquals(
array('ALTER TABLE alter_table_add_pk DROP INDEX idx_id, ADD PRIMARY KEY (id)'),
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
);
}
} }
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