Commit af9d724b authored by Steve Müller's avatar Steve Müller

fix dropping primary key with autoincrement column in MySQL

parent 8b28a9a1
...@@ -598,6 +598,19 @@ class MySqlPlatform extends AbstractPlatform ...@@ -598,6 +598,19 @@ class MySqlPlatform extends AbstractPlatform
$table = $diff->name; $table = $diff->name;
foreach ($diff->removedIndexes as $remKey => $remIndex) { 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) { foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() == $addIndex->getColumns()) { if ($remIndex->getColumns() == $addIndex->getColumns()) {
......
...@@ -91,4 +91,25 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -91,4 +91,25 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertFalse($table->hasIndex('idx_id')); $this->assertFalse($table->hasIndex('idx_id'));
$this->assertTrue($table->hasPrimaryKey()); $this->assertTrue($table->hasPrimaryKey());
} }
/**
* @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));
}
} }
...@@ -318,4 +318,29 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -318,4 +318,29 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable)) $this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
); );
} }
/**
* @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