Skip to content

Commit c3e6234

Browse files
authored
Merge pull request #6843 from morozov/remove-auto-drop-auto-increment
Remove support for auto-dropping auto-increment with PK
2 parents ddbb641 + 4bd7f5e commit c3e6234

File tree

3 files changed

+6
-124
lines changed

3 files changed

+6
-124
lines changed

src/Platforms/AbstractMySQLPlatform.php

Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
use Doctrine\DBAL\Schema\TableDiff;
1616
use Doctrine\DBAL\TransactionIsolationLevel;
1717
use Doctrine\DBAL\Types\Types;
18-
use Doctrine\Deprecations\Deprecation;
1918

20-
use function array_diff;
2119
use function array_map;
2220
use function array_merge;
2321
use function count;
@@ -357,48 +355,21 @@ public function getAlterTableSQL(TableDiff $diff): array
357355
$droppedIndexes = $this->indexIndexesByLowerCaseName($diff->getDroppedIndexes());
358356
$addedIndexes = $this->indexIndexesByLowerCaseName($diff->getAddedIndexes());
359357

360-
$noLongerPrimaryKeyColumns = [];
361-
362358
if (isset($droppedIndexes['primary'])) {
363359
$queryParts[] = 'DROP PRIMARY KEY';
364360

365-
$noLongerPrimaryKeyColumns = $droppedIndexes['primary']->getColumns();
361+
$diff->unsetDroppedIndex($droppedIndexes['primary']);
366362
}
367363

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

372-
$noLongerPrimaryKeyColumns = array_diff(
373-
$noLongerPrimaryKeyColumns,
374-
$addedIndexes['primary']->getColumns(),
375-
);
376-
377368
$diff->unsetAddedIndex($addedIndexes['primary']);
378369
}
379370

380371
$tableSql = [];
381372

382-
if (isset($droppedIndexes['primary'])) {
383-
$oldTable = $diff->getOldTable();
384-
foreach ($noLongerPrimaryKeyColumns as $columnName) {
385-
if (! $oldTable->hasColumn($columnName)) {
386-
continue;
387-
}
388-
389-
$column = $oldTable->getColumn($columnName);
390-
if ($column->getAutoincrement()) {
391-
$tableSql = array_merge(
392-
$tableSql,
393-
$this->getPreAlterTableAlterPrimaryKeySQL($diff, $droppedIndexes['primary']),
394-
);
395-
break;
396-
}
397-
}
398-
399-
$diff->unsetDroppedIndex($droppedIndexes['primary']);
400-
}
401-
402373
if (count($queryParts) > 0) {
403374
$tableSql[] = 'ALTER TABLE ' . $diff->getOldTable()->getObjectName()->toSQL($this) . ' '
404375
. implode(', ', $queryParts);
@@ -421,8 +392,6 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
421392
$tableNameSQL = $diff->getOldTable()->getObjectName()->toSQL($this);
422393

423394
foreach ($diff->getDroppedIndexes() as $droppedIndex) {
424-
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $droppedIndex));
425-
426395
foreach ($diff->getAddedIndexes() as $addedIndex) {
427396
if ($droppedIndex->getColumns() !== $addedIndex->getColumns()) {
428397
continue;
@@ -449,66 +418,7 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
449418
}
450419
}
451420

452-
return array_merge(
453-
$sql,
454-
parent::getPreAlterTableIndexForeignKeySQL($diff),
455-
$this->getPreAlterTableRenameIndexForeignKeySQL($diff),
456-
);
457-
}
458-
459-
/** @return list<string> */
460-
private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index): array
461-
{
462-
if (! $index->isPrimary()) {
463-
return [];
464-
}
465-
466-
$table = $diff->getOldTable();
467-
468-
$sql = [];
469-
470-
$tableNameSQL = $table->getObjectName()->toSQL($this);
471-
472-
// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
473-
foreach ($index->getColumns() as $columnName) {
474-
if (! $table->hasColumn($columnName)) {
475-
continue;
476-
}
477-
478-
$column = $table->getColumn($columnName);
479-
480-
if (! $column->getAutoincrement()) {
481-
continue;
482-
}
483-
484-
Deprecation::trigger(
485-
'doctrine/dbal',
486-
'https://github.com/doctrine/dbal/pull/6841',
487-
'Relying on the auto-increment attribute of a column being automatically dropped once a column'
488-
. ' is no longer part of the primary key constraint is deprecated. Instead, drop the auto-increment'
489-
. ' attribute explicitly.',
490-
);
491-
492-
$column->setAutoincrement(false);
493-
494-
$sql[] = 'ALTER TABLE ' . $tableNameSQL . ' MODIFY ' .
495-
$this->getColumnDeclarationSQL($column->toArray());
496-
497-
// original autoincrement information might be needed later on by other parts of the table alteration
498-
$column->setAutoincrement(true);
499-
}
500-
501-
return $sql;
502-
}
503-
504-
/**
505-
* @param TableDiff $diff The table diff to gather the SQL for.
506-
*
507-
* @return list<string>
508-
*/
509-
protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff): array
510-
{
511-
return [];
421+
return array_merge($sql, parent::getPreAlterTableIndexForeignKeySQL($diff));
512422
}
513423

514424
protected function getCreateIndexSQLFlags(Index $index): string

tests/Functional/Schema/AlterTableTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ public function testAlterPrimaryKeyFromAutoincrementToNonAutoincrementColumn():
5555
$platform = $this->connection->getDatabasePlatform();
5656

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

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

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

tests/Functional/Schema/MySQLSchemaManagerTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,6 @@ public function testAlterTableAddPrimaryKey(): void
137137
self::assertNotNull($table->getPrimaryKey());
138138
}
139139

140-
public function testDropPrimaryKeyWithAutoincrementColumn(): void
141-
{
142-
$table = new Table('drop_primary_key');
143-
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
144-
$table->addColumn('foo', Types::INTEGER);
145-
$table->setPrimaryKey(['id', 'foo']);
146-
147-
$this->dropAndCreateTable($table);
148-
149-
$diffTable = clone $table;
150-
151-
$diffTable->dropPrimaryKey();
152-
153-
$diff = $this->schemaManager->createComparator()
154-
->compareTables($table, $diffTable);
155-
156-
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6841');
157-
158-
$this->schemaManager->alterTable($diff);
159-
160-
$table = $this->schemaManager->introspectTable('drop_primary_key');
161-
162-
self::assertNull($table->getPrimaryKey());
163-
self::assertFalse($table->getColumn('id')->getAutoincrement());
164-
}
165-
166140
public function testDoesNotPropagateDefaultValuesForUnsupportedColumnTypes(): void
167141
{
168142
if ($this->connection->getDatabasePlatform() instanceof MariaDBPlatform) {

0 commit comments

Comments
 (0)