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

fix dropping primary key with autoincrement column in MySQL

parent ad8608ba
......@@ -582,6 +582,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()) {
......
......@@ -64,4 +64,25 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertArrayHasKey('f_index', $indexes);
$this->assertTrue($indexes['f_index']->hasFlag('fulltext'));
}
/**
* @group 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));
}
}
......@@ -295,4 +295,29 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216)));
$this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array()));
}
/**
* @group 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