15
15
use Doctrine \DBAL \Schema \TableDiff ;
16
16
use Doctrine \DBAL \TransactionIsolationLevel ;
17
17
use Doctrine \DBAL \Types \Types ;
18
- use Doctrine \Deprecations \Deprecation ;
19
18
20
- use function array_diff ;
21
19
use function array_map ;
22
20
use function array_merge ;
23
21
use function count ;
@@ -357,48 +355,21 @@ public function getAlterTableSQL(TableDiff $diff): array
357
355
$ droppedIndexes = $ this ->indexIndexesByLowerCaseName ($ diff ->getDroppedIndexes ());
358
356
$ addedIndexes = $ this ->indexIndexesByLowerCaseName ($ diff ->getAddedIndexes ());
359
357
360
- $ noLongerPrimaryKeyColumns = [];
361
-
362
358
if (isset ($ droppedIndexes ['primary ' ])) {
363
359
$ queryParts [] = 'DROP PRIMARY KEY ' ;
364
360
365
- $ noLongerPrimaryKeyColumns = $ droppedIndexes ['primary ' ]-> getColumns ( );
361
+ $ diff -> unsetDroppedIndex ( $ droppedIndexes ['primary ' ]);
366
362
}
367
363
368
364
if (isset ($ addedIndexes ['primary ' ])) {
369
365
$ keyColumns = $ addedIndexes ['primary ' ]->getQuotedColumns ($ this );
370
366
$ queryParts [] = 'ADD PRIMARY KEY ( ' . implode (', ' , $ keyColumns ) . ') ' ;
371
367
372
- $ noLongerPrimaryKeyColumns = array_diff (
373
- $ noLongerPrimaryKeyColumns ,
374
- $ addedIndexes ['primary ' ]->getColumns (),
375
- );
376
-
377
368
$ diff ->unsetAddedIndex ($ addedIndexes ['primary ' ]);
378
369
}
379
370
380
371
$ tableSql = [];
381
372
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
-
402
373
if (count ($ queryParts ) > 0 ) {
403
374
$ tableSql [] = 'ALTER TABLE ' . $ diff ->getOldTable ()->getObjectName ()->toSQL ($ this ) . ' '
404
375
. implode (', ' , $ queryParts );
@@ -421,8 +392,6 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
421
392
$ tableNameSQL = $ diff ->getOldTable ()->getObjectName ()->toSQL ($ this );
422
393
423
394
foreach ($ diff ->getDroppedIndexes () as $ droppedIndex ) {
424
- $ sql = array_merge ($ sql , $ this ->getPreAlterTableAlterPrimaryKeySQL ($ diff , $ droppedIndex ));
425
-
426
395
foreach ($ diff ->getAddedIndexes () as $ addedIndex ) {
427
396
if ($ droppedIndex ->getColumns () !== $ addedIndex ->getColumns ()) {
428
397
continue ;
@@ -449,66 +418,7 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
449
418
}
450
419
}
451
420
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 ));
512
422
}
513
423
514
424
protected function getCreateIndexSQLFlags (Index $ index ): string
0 commit comments