Commit d1929749 authored by Steve Müller's avatar Steve Müller Committed by Marco Pivetta

fix another primary key alteration with autoincrement column case on MySQL

fixes #2302
parent dcce6f86
......@@ -626,23 +626,12 @@ class MySqlPlatform extends AbstractPlatform
$sql = array();
$table = $diff->getName($this)->getQuotedName($this);
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->changedIndexes as $changedIndex) {
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $changedIndex));
}
// original autoincrement information might be needed later on by other parts of the table alteration
$column->setAutoincrement(true);
}
}
}
foreach ($diff->removedIndexes as $remKey => $remIndex) {
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $remIndex));
foreach ($diff->addedIndexes as $addKey => $addIndex) {
if ($remIndex->getColumns() == $addIndex->getColumns()) {
......@@ -692,6 +681,40 @@ class MySqlPlatform extends AbstractPlatform
return $sql;
}
/**
* @param TableDiff $diff
* @param Index $index
*
* @return string[]
*/
private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index)
{
$sql = array();
if (! $index->isPrimary() || ! $diff->fromTable instanceof Table) {
return $sql;
}
$tableName = $diff->getName($this)->getQuotedName($this);
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
foreach ($index->getColumns() as $columnName) {
$column = $diff->fromTable->getColumn($columnName);
if ($column->getAutoincrement() === true) {
$column->setAutoincrement(false);
$sql[] = 'ALTER TABLE ' . $tableName . ' MODIFY ' .
$this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
// original autoincrement information might be needed later on by other parts of the table alteration
$column->setAutoincrement(true);
}
}
return $sql;
}
/**
* @param TableDiff $diff The table diff to gather the SQL for.
*
......
......@@ -384,6 +384,33 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
);
}
/**
* @group DBAL-2302
*/
public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn()
{
$table = new Table("tbl");
$table->addColumn('id', 'integer', array('autoincrement' => true));
$table->addColumn('foo', 'integer');
$table->addColumn('bar', 'integer');
$table->setPrimaryKey(array('id', 'foo'));
$comparator = new Comparator();
$diffTable = clone $table;
$diffTable->dropPrimaryKey();
$diffTable->setPrimaryKey(array('id'));
$this->assertSame(
array(
'ALTER TABLE tbl MODIFY id INT NOT NULL',
'ALTER TABLE tbl DROP PRIMARY KEY',
'ALTER TABLE tbl ADD PRIMARY KEY (id)',
),
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
);
}
/**
* @group DBAL-586
*/
......
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