Commit bd07dced authored by andig's avatar andig

fix removing autoincrement column from primary key in MySQL

parent 89d4f06f
...@@ -654,6 +654,24 @@ class MySqlPlatform extends AbstractPlatform ...@@ -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'; $engine = 'INNODB';
if ($diff->fromTable instanceof Table && $diff->fromTable->hasOption('engine')) { if ($diff->fromTable instanceof Table && $diff->fromTable->hasOption('engine')) {
......
...@@ -326,6 +326,32 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase ...@@ -326,6 +326,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 * @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