Commit a0c61ded authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #430 from deeky666/DBAL-464

[DBAL-464] Fix dropping primary key with autoincrement column in MySQL
parents 99aadcb8 bc93de18
......@@ -598,6 +598,19 @@ class MySqlPlatform extends AbstractPlatform
$table = $diff->name;
foreach ($diff->removedIndexes as $remKey => $remIndex) {
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
if ($remIndex->isPrimary() && $diff->fromTable instanceof Table) {
foreach ($remIndex->getColumns() as $columnName) {
$column = $diff->fromTable->getColumn($columnName);
if ($column->getAutoincrement() === true) {
$column->setAutoincrement(false);
$sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
}
}
}
foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() == $addIndex->getColumns()) {
......
......@@ -91,4 +91,30 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertFalse($table->hasIndex('idx_id'));
$this->assertTrue($table->hasPrimaryKey());
}
/**
* @group DBAL-464
*/
public function testDropPrimaryKeyWithAutoincrementColumn()
{
$table = new Table("drop_primary_key");
$table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
$table->addColumn('foo', 'integer', array('primary' => true));
$table->setPrimaryKey(array('id', 'foo'));
$this->_sm->dropAndCreateTable($table);
$diffTable = clone $table;
$diffTable->dropPrimaryKey();
$comparator = new Comparator();
$this->_sm->alterTable($comparator->diffTable($table, $diffTable));
$table = $this->_sm->listTableDetails("drop_primary_key");
$this->assertFalse($table->hasPrimaryKey());
$this->assertFalse($table->getColumn('id')->getAutoincrement());
}
}
......@@ -318,4 +318,29 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
);
}
/**
* @group DBAL-464
*/
public function testDropPrimaryKeyWithAutoincrementColumn()
{
$table = new Table("drop_primary_key");
$table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
$table->addColumn('foo', 'integer', array('primary' => true));
$table->addColumn('bar', 'integer');
$table->setPrimaryKey(array('id', 'foo'));
$comparator = new Comparator();
$diffTable = clone $table;
$diffTable->dropPrimaryKey();
$this->assertEquals(
array(
'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
'ALTER TABLE drop_primary_key DROP PRIMARY KEY'
),
$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