Skip to content
Closed
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
35 changes: 12 additions & 23 deletions src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Doctrine\DBAL\Exception\InvalidColumnType\ColumnValuesRequired;
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
use Doctrine\DBAL\Platforms\Keywords\MySQLKeywords;
use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\MySQLSchemaManager;
Expand Down Expand Up @@ -367,8 +366,8 @@ public function getAlterTableSQL(TableDiff $diff): array
. $this->getColumnDeclarationSQL($newColumn->getQuotedName($this), $newColumnProperties);
}

$addedIndexes = $this->indexAssetsByLowerCaseName($diff->getAddedIndexes());
$modifiedIndexes = $this->indexAssetsByLowerCaseName($diff->getModifiedIndexes());
$addedIndexes = $this->indexIndexesByLowerCaseName($diff->getAddedIndexes());
$modifiedIndexes = $this->indexIndexesByLowerCaseName($diff->getModifiedIndexes());
$diffModified = false;

if (isset($addedIndexes['primary'])) {
Expand All @@ -377,19 +376,11 @@ public function getAlterTableSQL(TableDiff $diff): array
unset($addedIndexes['primary']);
$diffModified = true;
} elseif (isset($modifiedIndexes['primary'])) {
$addedColumns = $this->indexAssetsByLowerCaseName($diff->getAddedColumns());

// Necessary in case the new primary key includes a new auto_increment column
foreach ($modifiedIndexes['primary']->getColumns() as $columnName) {
if (isset($addedColumns[$columnName]) && $addedColumns[$columnName]->getAutoincrement()) {
$keyColumns = array_values(array_unique($modifiedIndexes['primary']->getColumns()));
$queryParts[] = 'DROP PRIMARY KEY';
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
unset($modifiedIndexes['primary']);
$diffModified = true;
break;
}
}
$keyColumns = array_values(array_unique($modifiedIndexes['primary']->getColumns()));
$queryParts[] = 'DROP PRIMARY KEY';
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
unset($modifiedIndexes['primary']);
$diffModified = true;
}

if ($diffModified) {
Expand Down Expand Up @@ -833,18 +824,16 @@ public function createSchemaManager(Connection $connection): MySQLSchemaManager
}

/**
* @param array<T> $assets
*
* @return array<string,T>
* @param array<Index> $indexes
*
* @template T of AbstractAsset
* @return array<string,Index>
*/
private function indexAssetsByLowerCaseName(array $assets): array
private function indexIndexesByLowerCaseName(array $indexes): array
{
$result = [];

foreach ($assets as $asset) {
$result[strtolower($asset->getName())] = $asset;
foreach ($indexes as $index) {
$result[strtolower($index->getName())] = $index;
}

return $result;
Expand Down
16 changes: 2 additions & 14 deletions tests/Functional/Schema/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@ public function testDropPrimaryKeyWithAutoincrementColumn(): void

public function testDropNonAutoincrementColumnFromCompositePrimaryKeyWithAutoincrementColumn(): void
{
if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
self::markTestIncomplete(
'DBAL does not restore the auto-increment attribute after dropping and adding the constraint,'
. ' which is a bug.',
);
}

$this->ensureDroppingPrimaryKeyConstraintIsSupported();

$table = new Table('alter_pk');
Expand All @@ -135,13 +128,6 @@ public function testAddNonAutoincrementColumnToPrimaryKeyWithAutoincrementColumn
{
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof AbstractMySQLPlatform) {
self::markTestIncomplete(
'DBAL does not restore the auto-increment attribute after dropping and adding the constraint,'
. ' which is a bug.',
);
}

if ($platform instanceof SQLitePlatform) {
self::markTestSkipped(
'SQLite does not support auto-increment columns as part of composite primary key constraint',
Expand Down Expand Up @@ -244,6 +230,8 @@ private function testMigration(Table $oldTable, callable $migration): void
$diff = $schemaManager->createComparator()
->compareTables($oldTable, $newTable);

self::assertFalse($diff->isEmpty());

$schemaManager->alterTable($diff);

$introspectedTable = $schemaManager->introspectTable($newTable->getName());
Expand Down
Loading