Commit 4dc09995 authored by Steve Müller's avatar Steve Müller Committed by Benjamin Eberlei

fix adding primary key during table alteration in MySQL

parent 76af9c69
......@@ -115,6 +115,22 @@ class MySqlPlatform extends AbstractPlatform
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
}
/**
* {@inheritDoc}
*/
public function getDateAddHourExpression($date, $hours)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
}
/**
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
}
/**
* {@inheritDoc}
*/
......@@ -605,13 +621,16 @@ class MySqlPlatform extends AbstractPlatform
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;
......
......@@ -2,9 +2,9 @@
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Comparator;
class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
......@@ -20,7 +20,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$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));
}
......@@ -41,7 +41,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$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.");
......@@ -89,4 +89,30 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertFalse($table->hasPrimaryKey());
$this->assertFalse($table->getColumn('id')->getAutoincrement());
}
/**
* @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,12 +3,12 @@
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;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Comparator;
class MySqlPlatformTest extends AbstractPlatformTestCase
{
......
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