Skip to content

Remove support for auto-dropping auto-increment with PK #6843

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

Merged
merged 1 commit into from
Mar 13, 2025
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
94 changes: 2 additions & 92 deletions src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Types;
use Doctrine\Deprecations\Deprecation;

use function array_diff;
use function array_map;
use function array_merge;
use function count;
Expand Down Expand Up @@ -357,48 +355,21 @@
$droppedIndexes = $this->indexIndexesByLowerCaseName($diff->getDroppedIndexes());
$addedIndexes = $this->indexIndexesByLowerCaseName($diff->getAddedIndexes());

$noLongerPrimaryKeyColumns = [];

if (isset($droppedIndexes['primary'])) {
$queryParts[] = 'DROP PRIMARY KEY';

$noLongerPrimaryKeyColumns = $droppedIndexes['primary']->getColumns();
$diff->unsetDroppedIndex($droppedIndexes['primary']);

Check warning on line 361 in src/Platforms/AbstractMySQLPlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/AbstractMySQLPlatform.php#L361

Added line #L361 was not covered by tests
}

if (isset($addedIndexes['primary'])) {
$keyColumns = $addedIndexes['primary']->getQuotedColumns($this);
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';

$noLongerPrimaryKeyColumns = array_diff(
$noLongerPrimaryKeyColumns,
$addedIndexes['primary']->getColumns(),
);

$diff->unsetAddedIndex($addedIndexes['primary']);
}

$tableSql = [];

if (isset($droppedIndexes['primary'])) {
$oldTable = $diff->getOldTable();
foreach ($noLongerPrimaryKeyColumns as $columnName) {
if (! $oldTable->hasColumn($columnName)) {
continue;
}

$column = $oldTable->getColumn($columnName);
if ($column->getAutoincrement()) {
$tableSql = array_merge(
$tableSql,
$this->getPreAlterTableAlterPrimaryKeySQL($diff, $droppedIndexes['primary']),
);
break;
}
}

$diff->unsetDroppedIndex($droppedIndexes['primary']);
}

if (count($queryParts) > 0) {
$tableSql[] = 'ALTER TABLE ' . $diff->getOldTable()->getObjectName()->toSQL($this) . ' '
. implode(', ', $queryParts);
Expand All @@ -421,8 +392,6 @@
$tableNameSQL = $diff->getOldTable()->getObjectName()->toSQL($this);

foreach ($diff->getDroppedIndexes() as $droppedIndex) {
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $droppedIndex));

foreach ($diff->getAddedIndexes() as $addedIndex) {
if ($droppedIndex->getColumns() !== $addedIndex->getColumns()) {
continue;
Expand All @@ -449,66 +418,7 @@
}
}

return array_merge(
$sql,
parent::getPreAlterTableIndexForeignKeySQL($diff),
$this->getPreAlterTableRenameIndexForeignKeySQL($diff),
);
}

/** @return list<string> */
private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index): array
{
if (! $index->isPrimary()) {
return [];
}

$table = $diff->getOldTable();

$sql = [];

$tableNameSQL = $table->getObjectName()->toSQL($this);

// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
foreach ($index->getColumns() as $columnName) {
if (! $table->hasColumn($columnName)) {
continue;
}

$column = $table->getColumn($columnName);

if (! $column->getAutoincrement()) {
continue;
}

Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6841',
'Relying on the auto-increment attribute of a column being automatically dropped once a column'
. ' is no longer part of the primary key constraint is deprecated. Instead, drop the auto-increment'
. ' attribute explicitly.',
);

$column->setAutoincrement(false);

$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' MODIFY ' .
$this->getColumnDeclarationSQL($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.
*
* @return list<string>
*/
protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff): array
{
return [];
return array_merge($sql, parent::getPreAlterTableIndexForeignKeySQL($diff));
}

protected function getCreateIndexSQLFlags(Index $index): string
Expand Down
10 changes: 4 additions & 6 deletions tests/Functional/Schema/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ public function testAlterPrimaryKeyFromAutoincrementToNonAutoincrementColumn():
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof AbstractMySQLPlatform) {
self::markTestIncomplete(
'DBAL should not allow this migration on MySQL because an auto-increment column must be part of the'
. ' primary key constraint.',
self::markTestSkipped(
'MySQL does not support auto-increment columns that are not part of the primary key constraint',
);
}

Expand Down Expand Up @@ -85,9 +84,8 @@ public function testDropPrimaryKeyWithAutoincrementColumn(): void
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof AbstractMySQLPlatform) {
self::markTestIncomplete(
'DBAL should not allow this migration on MySQL because an auto-increment column must be part of the'
. ' primary key constraint.',
self::markTestSkipped(
'MySQL does not support auto-increment columns that are not part of the primary key constraint',
);
}

Expand Down
26 changes: 0 additions & 26 deletions tests/Functional/Schema/MySQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,6 @@ public function testAlterTableAddPrimaryKey(): void
self::assertNotNull($table->getPrimaryKey());
}

public function testDropPrimaryKeyWithAutoincrementColumn(): void
{
$table = new Table('drop_primary_key');
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
$table->addColumn('foo', Types::INTEGER);
$table->setPrimaryKey(['id', 'foo']);

$this->dropAndCreateTable($table);

$diffTable = clone $table;

$diffTable->dropPrimaryKey();

$diff = $this->schemaManager->createComparator()
->compareTables($table, $diffTable);

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6841');

$this->schemaManager->alterTable($diff);

$table = $this->schemaManager->introspectTable('drop_primary_key');

self::assertNull($table->getPrimaryKey());
self::assertFalse($table->getColumn('id')->getAutoincrement());
}

public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes(): void
{
if ($this->connection->getDatabasePlatform() instanceof MariaDBPlatform) {
Expand Down