Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DBAL-464] Fix dropping primary key with autoincrement column in MySQL #430

Merged
merged 3 commits into from
Dec 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,19 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
$table = $diff->name;

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) {
if ($remIndex->getColumns() == $addIndex->getColumns()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,30 @@ public function testAlterTableAddPrimaryKey()
$this->assertFalse($table->hasIndex('idx_id'));
$this->assertTrue($table->hasPrimaryKey());
}

/**
* @group DBAL-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));

$table = $this->_sm->listTableDetails("drop_primary_key");

$this->assertFalse($table->hasPrimaryKey());
$this->assertFalse($table->getColumn('id')->getAutoincrement());
}
}
25 changes: 25 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,29 @@ public function testAlterTableAddPrimaryKey()
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
);
}

/**
* @group DBAL-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))
);
}
}