Commit 86974572 authored by Marco Pivetta's avatar Marco Pivetta

Merge branch 'fix/#2303-#2302-primary-key-alteration-with-auto-increment-on-mysql-2.5' into 2.5

Close #2302
Close #2303
parents dcce6f86 488183a8
......@@ -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,60 @@ 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-2302
*/
public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn()
{
$table = new Table("tbl");
$table->addColumn('id', 'integer', array('autoincrement' => true));
$table->addColumn('foo', 'integer');
$table->addColumn('bar', 'integer');
$table->setPrimaryKey(array('id'));
$comparator = new Comparator();
$diffTable = clone $table;
$diffTable->dropPrimaryKey();
$diffTable->setPrimaryKey(array('id', 'foo'));
$this->assertSame(
array(
'ALTER TABLE tbl MODIFY id INT NOT NULL',
'ALTER TABLE tbl DROP PRIMARY KEY',
'ALTER TABLE tbl ADD PRIMARY KEY (id, foo)',
),
$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