2727use Doctrine \DBAL \Types ;
2828
2929use function array_merge ;
30+ use function assert ;
3031use function count ;
3132use function explode ;
3233use function implode ;
@@ -861,23 +862,8 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
861862 {
862863 $ oldTable = $ diff ->getOldTable ();
863864 $ foreignKeys = $ oldTable ->getForeignKeys ();
865+ $ nameMap = $ this ->getDiffColumnNameMap ($ diff );
864866 $ keysByName = [];
865- foreach ($ foreignKeys as $ key => $ foreignKey ) {
866- $ constraintName = $ foreignKey ->getObjectName ();
867-
868- if ($ constraintName === null ) {
869- continue ;
870- }
871-
872- $ constraintKey = strtolower (
873- $ constraintName ->getIdentifier ()
874- ->getValue (),
875- );
876-
877- $ keysByName [$ constraintKey ] = $ key ;
878- }
879-
880- $ nameMap = $ this ->getDiffColumnNameMap ($ diff );
881867
882868 foreach ($ foreignKeys as $ key => $ constraint ) {
883869 $ changed = false ;
@@ -900,6 +886,17 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
900886 $ changed = true ;
901887 }
902888
889+ $ constraintName = $ constraint ->getObjectName ();
890+
891+ if ($ constraintName !== null ) {
892+ $ constraintKey = strtolower (
893+ $ constraintName ->getIdentifier ()
894+ ->getValue (),
895+ );
896+
897+ $ keysByName [$ constraintKey ] = $ key ;
898+ }
899+
903900 if (! $ changed ) {
904901 continue ;
905902 }
@@ -921,7 +918,8 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
921918 ->getValue (),
922919 );
923920
924- unset($ foreignKeys [$ keysByName [$ constraintKey ]]);
921+ assert (isset ($ keysByName [$ constraintKey ]));
922+ unset($ foreignKeys [$ keysByName [$ constraintKey ]], $ keysByName [$ constraintKey ]);
925923 }
926924
927925 foreach ($ diff ->getAddedForeignKeys () as $ constraint ) {
@@ -933,11 +931,10 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
933931 ->getValue (),
934932 );
935933
936- if (isset ($ keysByName [$ constraintKey ])) {
937- $ foreignKeys [$ keysByName [$ constraintKey ]] = $ constraint ;
938- } else {
939- $ foreignKeys [] = $ constraint ;
940- }
934+ assert (! isset ($ keysByName [$ constraintKey ]));
935+ $ foreignKeys [] = $ constraint ;
936+
937+ $ keysByName [$ constraintKey ] = count ($ foreignKeys ) - 1 ;
941938 } else {
942939 $ foreignKeys [] = $ constraint ;
943940 }
0 commit comments