Commit 59656833 authored by andig's avatar andig Committed by Steve Müller

fix removing autoincrement column from primary key in MySQL

parent 42440c33
......@@ -654,6 +654,24 @@ class MySqlPlatform extends AbstractPlatform
}
}
// handle columns that are removed from changed indexes
foreach ($diff->changedIndexes as $chgKey => $chgIndex) {
// Dropping primary keys requires to unset autoincrement attribute on the particular column first
if ($chgIndex->isPrimary() && $diff->fromTable instanceof Table) {
foreach ($diff->fromTable->getIndex($chgIndex->getName())->getColumns() as $columnName) {
$column = $diff->fromTable->getColumn($columnName);
if ($column->getAutoincrement() === true && in_array($columnName, $chgIndex->getColumns()) === false) {
$column->setAutoincrement(false);
$sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
}
}
}
}
$engine = 'INNODB';
if ($diff->fromTable instanceof Table && $diff->fromTable->hasOption('engine')) {
......
......@@ -333,6 +333,32 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
);
}
/**
* @group DBAL-464
*/
public function testAlterPrimaryKeyWithAutoincrementColumn()
{
$table = new Table("drop_primary_key");
$table->addColumn('id', 'integer', array('primary' => true, 'autoincrement' => true));
$table->addColumn('foo', 'integer');
$table->setPrimaryKey(array('id'));
$comparator = new Comparator();
$diffTable = clone $table;
$diffTable->dropPrimaryKey();
$diffTable->setPrimaryKey(array('foo'));
$this->assertEquals(
array(
'ALTER TABLE drop_primary_key MODIFY id INT NOT NULL',
'ALTER TABLE drop_primary_key DROP PRIMARY KEY',
'ALTER TABLE drop_primary_key ADD PRIMARY KEY (foo)'
),
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
);
}
/**
* @group DBAL-464
*/
......
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