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

Fix: Ensuring correct ALTER TABLE statement for creation of an AUTO INCREMENT column as new PRIMARY KEY #3311

Merged
merged 1 commit into from
Dec 7, 2018
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
11 changes: 11 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,17 @@ public function getAlterTableSQL(TableDiff $diff)
$keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns()));
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
unset($diff->addedIndexes['primary']);
} elseif (isset($diff->changedIndexes['primary'])) {
// Necessary in case the new primary key includes a new auto_increment column
foreach ($diff->changedIndexes['primary']->getColumns() as $columnName) {
if (isset($diff->addedColumns[$columnName]) && $diff->addedColumns[$columnName]->getAutoincrement()) {
$keyColumns = array_unique(array_values($diff->changedIndexes['primary']->getColumns()));
$queryParts[] = 'DROP PRIMARY KEY';
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
unset($diff->changedIndexes['primary']);
break;
}
}
}

$sql = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Doctrine\Tests\DBAL\Functional\Platform;

use Doctrine\DBAL\Schema\Comparator;
use Doctrine\Tests\DbalFunctionalTestCase;
use function in_array;

final class NewPrimaryKeyWithNewAutoIncrementColumnTest extends DbalFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp()
{
parent::setUp();

if (in_array($this->getPlatform()->getName(), ['mysql'])) {
return;
}

$this->markTestSkipped('Restricted to MySQL.');
}

/**
* Ensures that the primary key is created within the same "alter table" statement that an auto-increment column
* is added to the table as part of the new primary key.
*
* Before the fix for this problem this resulted in a database error: (at least on mysql)
* SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto
* column and it must be defined as a key
*/
public function testAlterPrimaryKeyToAutoIncrementColumn()
{
$schemaManager = $this->connection->getSchemaManager();
$schema = $schemaManager->createSchema();

$table = $schema->createTable('dbal2807');
$table->addColumn('initial_id', 'integer');
$table->setPrimaryKey(['initial_id']);

$schemaManager->dropAndCreateTable($table);

$newSchema = clone $schema;
$newTable = $newSchema->getTable($table->getName());
$newTable->addColumn('new_id', 'integer', ['autoincrement' => true]);
$newTable->dropPrimaryKey();
$newTable->setPrimaryKey(['new_id']);

$diff = (new Comparator())->compare($schema, $newSchema);

foreach ($diff->toSql($this->getPlatform()) as $sql) {
$this->connection->exec($sql);
}

$validationSchema = $schemaManager->createSchema();
$validationTable = $validationSchema->getTable($table->getName());

$this->assertTrue($validationTable->hasColumn('new_id'));
$this->assertTrue($validationTable->getColumn('new_id')->getAutoincrement());
$this->assertTrue($validationTable->hasPrimaryKey());
$this->assertSame(['new_id'], $validationTable->getPrimaryKeyColumns());
}

private function getPlatform()
{
return $this->connection->getDatabasePlatform();
}
}