Skip to content

Commit

Permalink
Merge pull request #435 from deeky666/DBAL-400
Browse files Browse the repository at this point in the history
[DBAL-400] Fix adding primary key during table alteration in MySQL
  • Loading branch information
guilhermeblanco committed Nov 28, 2013
2 parents 4b74a50 + 09101dd commit 8b28a9a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
13 changes: 8 additions & 5 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Expand Up @@ -128,7 +128,7 @@ public function getDateAddHourExpression($date, $hours)
*/
public function getDateSubHourExpression($date, $hours)
{
return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
}

/**
Expand Down Expand Up @@ -602,13 +602,16 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() == $addIndex->getColumns()) {

$type = '';
if ($addIndex->isUnique()) {
$type = 'UNIQUE ';
$indexClause = 'INDEX ' . $addIndex->getName();

if ($addIndex->isPrimary()) {
$indexClause = 'PRIMARY KEY';
} elseif ($addIndex->isUnique()) {
$indexClause = 'UNIQUE INDEX ' . $addIndex->getName();
}

$query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', ';
$query .= 'ADD ' . $type . 'INDEX ' . $addIndex->getName();
$query .= 'ADD ' . $indexClause;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex->getQuotedColumns($this)) . ')';

$sql[] = $query;
Expand Down
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\DBAL\Functional\Schema;

use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;

Expand All @@ -21,7 +22,7 @@ public function testSwitchPrimaryKeyColumns()
$tableNew = clone $tableFetched;
$tableNew->setPrimaryKey(array('bar_id', 'foo_id'));

$comparator = new \Doctrine\DBAL\Schema\Comparator;
$comparator = new Comparator;
$this->_sm->alterTable($comparator->diffTable($tableFetched, $tableNew));
}

Expand All @@ -42,7 +43,7 @@ public function testDiffTableBug()
$this->_sm->createTable($table);
$tableFetched = $this->_sm->listTableDetails("diffbug_routing_translations");

$comparator = new \Doctrine\DBAL\Schema\Comparator;
$comparator = new Comparator;
$diff = $comparator->diffTable($tableFetched, $table);

$this->assertFalse($diff, "no changes expected.");
Expand All @@ -64,4 +65,30 @@ public function testFulltextIndex()
$this->assertArrayHasKey('f_index', $indexes);
$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());
}
}
23 changes: 23 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL\Platforms;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
Expand Down Expand Up @@ -295,4 +296,26 @@ public function testBlobTypeDeclarationSQL()
$this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216)));
$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))
);
}
}

0 comments on commit 8b28a9a

Please sign in to comment.