@@ -522,33 +522,33 @@ public function getAlterTableSQL(TableDiff $diff)
522
522
$ commentsSQL = [];
523
523
$ columnSql = [];
524
524
525
- foreach ($ diff ->addedColumns as $ column ) {
526
- if ($ this ->onSchemaAlterTableAddColumn ($ column , $ diff , $ columnSql )) {
525
+ foreach ($ diff ->addedColumns as $ newColumn ) {
526
+ if ($ this ->onSchemaAlterTableAddColumn ($ newColumn , $ diff , $ columnSql )) {
527
527
continue ;
528
528
}
529
529
530
- $ query = 'ADD ' . $ this ->getColumnDeclarationSQL ($ column ->getQuotedName ($ this ), $ column ->toArray ());
530
+ $ query = 'ADD ' . $ this ->getColumnDeclarationSQL ($ newColumn ->getQuotedName ($ this ), $ newColumn ->toArray ());
531
531
$ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
532
532
533
- $ comment = $ this ->getColumnComment ($ column );
533
+ $ comment = $ this ->getColumnComment ($ newColumn );
534
534
535
535
if ($ comment === null || $ comment === '' ) {
536
536
continue ;
537
537
}
538
538
539
539
$ commentsSQL [] = $ this ->getCommentOnColumnSQL (
540
540
$ diff ->getName ($ this )->getQuotedName ($ this ),
541
- $ column ->getQuotedName ($ this ),
541
+ $ newColumn ->getQuotedName ($ this ),
542
542
$ comment ,
543
543
);
544
544
}
545
545
546
- foreach ($ diff ->removedColumns as $ column ) {
547
- if ($ this ->onSchemaAlterTableRemoveColumn ($ column , $ diff , $ columnSql )) {
546
+ foreach ($ diff ->removedColumns as $ newColumn ) {
547
+ if ($ this ->onSchemaAlterTableRemoveColumn ($ newColumn , $ diff , $ columnSql )) {
548
548
continue ;
549
549
}
550
550
551
- $ query = 'DROP ' . $ column ->getQuotedName ($ this );
551
+ $ query = 'DROP ' . $ newColumn ->getQuotedName ($ this );
552
552
$ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
553
553
}
554
554
@@ -561,26 +561,21 @@ public function getAlterTableSQL(TableDiff $diff)
561
561
continue ;
562
562
}
563
563
564
- if ($ columnDiff ->fromColumn !== null ) {
565
- $ fromColumn = $ columnDiff ->fromColumn ;
566
- } else {
567
- $ fromColumn = $ columnDiff ->getOldColumnName ();
568
- }
569
-
570
- $ oldColumnName = $ fromColumn ->getQuotedName ($ this );
564
+ $ oldColumn = $ columnDiff ->getOldColumn () ?? $ columnDiff ->getOldColumnName ();
565
+ $ newColumn = $ columnDiff ->getNewColumn ();
571
566
572
- $ column = $ columnDiff -> column ;
567
+ $ oldColumnName = $ oldColumn -> getQuotedName ( $ this ) ;
573
568
574
569
if (
575
570
$ columnDiff ->hasTypeChanged ()
576
571
|| $ columnDiff ->hasPrecisionChanged ()
577
572
|| $ columnDiff ->hasScaleChanged ()
578
573
|| $ columnDiff ->hasFixedChanged ()
579
574
) {
580
- $ type = $ column ->getType ();
575
+ $ type = $ newColumn ->getType ();
581
576
582
577
// SERIAL/BIGSERIAL are not "real" types and we can't alter a column to that type
583
- $ columnDefinition = $ column ->toArray ();
578
+ $ columnDefinition = $ newColumn ->toArray ();
584
579
$ columnDefinition ['autoincrement ' ] = false ;
585
580
586
581
// here was a server version check before, but DBAL API does not support this anymore.
@@ -589,45 +584,46 @@ public function getAlterTableSQL(TableDiff $diff)
589
584
}
590
585
591
586
if ($ columnDiff ->hasDefaultChanged ()) {
592
- $ defaultClause = $ column ->getDefault () === null
587
+ $ defaultClause = $ newColumn ->getDefault () === null
593
588
? ' DROP DEFAULT '
594
- : ' SET ' . $ this ->getDefaultValueDeclarationSQL ($ column ->toArray ());
595
- $ query = 'ALTER ' . $ oldColumnName . $ defaultClause ;
596
- $ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
589
+ : ' SET ' . $ this ->getDefaultValueDeclarationSQL ($ newColumn ->toArray ());
590
+
591
+ $ query = 'ALTER ' . $ oldColumnName . $ defaultClause ;
592
+ $ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
597
593
}
598
594
599
595
if ($ columnDiff ->hasNotNullChanged ()) {
600
- $ query = 'ALTER ' . $ oldColumnName . ' ' . ($ column ->getNotnull () ? 'SET ' : 'DROP ' ) . ' NOT NULL ' ;
596
+ $ query = 'ALTER ' . $ oldColumnName . ' ' . ($ newColumn ->getNotnull () ? 'SET ' : 'DROP ' ) . ' NOT NULL ' ;
601
597
$ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
602
598
}
603
599
604
600
if ($ columnDiff ->hasAutoIncrementChanged ()) {
605
- if ($ column ->getAutoincrement ()) {
601
+ if ($ newColumn ->getAutoincrement ()) {
606
602
// add autoincrement
607
603
$ seqName = $ this ->getIdentitySequenceName ($ diff ->name , $ oldColumnName );
608
604
609
605
$ sql [] = 'CREATE SEQUENCE ' . $ seqName ;
610
606
$ sql [] = "SELECT setval(' " . $ seqName . "', (SELECT MAX( " . $ oldColumnName . ') FROM '
611
607
. $ diff ->getName ($ this )->getQuotedName ($ this ) . ')) ' ;
612
608
$ query = 'ALTER ' . $ oldColumnName . " SET DEFAULT nextval(' " . $ seqName . "') " ;
613
- $ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
614
609
} else {
615
610
// Drop autoincrement, but do NOT drop the sequence. It might be re-used by other tables or have
616
611
$ query = 'ALTER ' . $ oldColumnName . ' DROP DEFAULT ' ;
617
- $ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
618
612
}
613
+
614
+ $ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
619
615
}
620
616
621
- $ newComment = $ this ->getColumnComment ($ column );
622
617
$ oldComment = $ this ->getOldColumnComment ($ columnDiff );
618
+ $ newComment = $ this ->getColumnComment ($ newColumn );
623
619
624
620
if (
625
621
$ columnDiff ->hasCommentChanged ()
626
- || ($ columnDiff ->fromColumn !== null && $ oldComment !== $ newComment )
622
+ || ($ columnDiff ->getOldColumn () !== null && $ oldComment !== $ newComment )
627
623
) {
628
624
$ commentsSQL [] = $ this ->getCommentOnColumnSQL (
629
625
$ diff ->getName ($ this )->getQuotedName ($ this ),
630
- $ column ->getQuotedName ($ this ),
626
+ $ newColumn ->getQuotedName ($ this ),
631
627
$ newComment ,
632
628
);
633
629
}
@@ -637,7 +633,7 @@ public function getAlterTableSQL(TableDiff $diff)
637
633
}
638
634
639
635
$ query = 'ALTER ' . $ oldColumnName . ' TYPE '
640
- . $ column ->getType ()->getSQLDeclaration ($ column ->toArray (), $ this );
636
+ . $ newColumn ->getType ()->getSQLDeclaration ($ newColumn ->toArray (), $ this );
641
637
$ sql [] = 'ALTER TABLE ' . $ diff ->getName ($ this )->getQuotedName ($ this ) . ' ' . $ query ;
642
638
}
643
639
@@ -688,18 +684,18 @@ public function getAlterTableSQL(TableDiff $diff)
688
684
*/
689
685
private function isUnchangedBinaryColumn (ColumnDiff $ columnDiff ): bool
690
686
{
691
- $ columnType = $ columnDiff ->column ->getType ();
687
+ $ newColumnType = $ columnDiff ->getNewColumn () ->getType ();
692
688
693
- if (! $ columnType instanceof BinaryType && ! $ columnType instanceof BlobType) {
689
+ if (! $ newColumnType instanceof BinaryType && ! $ newColumnType instanceof BlobType) {
694
690
return false ;
695
691
}
696
692
697
- $ fromColumn = $ columnDiff ->fromColumn instanceof Column ? $ columnDiff ->fromColumn : null ;
693
+ $ oldColumn = $ columnDiff ->getOldColumn () instanceof Column ? $ columnDiff ->getOldColumn () : null ;
698
694
699
- if ($ fromColumn !== null ) {
700
- $ fromColumnType = $ fromColumn ->getType ();
695
+ if ($ oldColumn !== null ) {
696
+ $ oldColumnType = $ oldColumn ->getType ();
701
697
702
- if (! $ fromColumnType instanceof BinaryType && ! $ fromColumnType instanceof BlobType) {
698
+ if (! $ oldColumnType instanceof BinaryType && ! $ oldColumnType instanceof BlobType) {
703
699
return false ;
704
700
}
705
701
@@ -1326,7 +1322,13 @@ public function getJsonTypeDeclarationSQL(array $column)
1326
1322
1327
1323
private function getOldColumnComment (ColumnDiff $ columnDiff ): ?string
1328
1324
{
1329
- return $ columnDiff ->fromColumn !== null ? $ this ->getColumnComment ($ columnDiff ->fromColumn ) : null ;
1325
+ $ oldColumn = $ columnDiff ->getOldColumn ();
1326
+
1327
+ if ($ oldColumn !== null ) {
1328
+ return $ this ->getColumnComment ($ oldColumn );
1329
+ }
1330
+
1331
+ return null ;
1330
1332
}
1331
1333
1332
1334
/** @deprecated The SQL used for schema introspection is an implementation detail and should not be relied upon. */
0 commit comments