@@ -73,7 +73,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7373 $ this ->dispatcher ->dispatchTyped ($ event );
7474
7575 $ missingIndices = $ event ->getMissingIndices ();
76- if ($ missingIndices !== []) {
76+ $ toReplaceIndices = $ event ->toReplaceIndices ();
77+
78+ if ($ missingIndices !== [] || $ toReplaceIndices !== []) {
7779 $ schema = new SchemaWrapper ($ this ->connection );
7880
7981 foreach ($ missingIndices as $ missingIndex ) {
@@ -97,15 +99,59 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9799 $ table ->addIndex ($ missingIndex ['columns ' ], $ missingIndex ['indexName ' ], [], $ missingIndex ['options ' ]);
98100 }
99101
100-
101- $ sqlQueries = $ this ->connection ->migrateToSchema ($ schema ->getWrappedSchema (), $ dryRun );
102- if ($ dryRun && $ sqlQueries !== null ) {
103- $ output ->writeln ($ sqlQueries );
102+ if (!$ dryRun ) {
103+ $ this ->connection ->migrateToSchema ($ schema ->getWrappedSchema ());
104104 }
105105 $ output ->writeln ('<info> ' . $ table ->getName () . ' table updated successfully.</info> ' );
106106 }
107107 }
108108 }
109+
110+ foreach ($ toReplaceIndices as $ toReplaceIndex ) {
111+ if ($ schema ->hasTable ($ toReplaceIndex ['tableName ' ])) {
112+ $ table = $ schema ->getTable ($ toReplaceIndex ['tableName ' ]);
113+
114+ $ allOldIndicesExists = true ;
115+ foreach ($ toReplaceIndex ['oldIndexNames ' ] as $ oldIndexName ) {
116+ if (!$ table ->hasIndex ($ oldIndexName )) {
117+ $ allOldIndicesExists = false ;
118+ }
119+ }
120+
121+ if (!$ allOldIndicesExists ) {
122+ continue ;
123+ }
124+
125+ $ output ->writeln ('<info>Adding additional ' . $ toReplaceIndex ['newIndexName ' ] . ' index to the ' . $ table ->getName () . ' table, this can take some time...</info> ' );
126+
127+ if ($ toReplaceIndex ['uniqueIndex ' ]) {
128+ $ table ->addUniqueIndex ($ toReplaceIndex ['columns ' ], $ toReplaceIndex ['newIndexName ' ], $ toReplaceIndex ['options ' ]);
129+ } else {
130+ $ table ->addIndex ($ toReplaceIndex ['columns ' ], $ toReplaceIndex ['newIndexName ' ], [], $ toReplaceIndex ['options ' ]);
131+ }
132+
133+ if (!$ dryRun ) {
134+ $ this ->connection ->migrateToSchema ($ schema ->getWrappedSchema ());
135+ }
136+
137+ foreach ($ toReplaceIndex ['oldIndexNames ' ] as $ oldIndexName ) {
138+ $ output ->writeln ('<info>Removing ' . $ oldIndexName . ' index from the ' . $ table ->getName () . ' table</info> ' );
139+ $ table ->dropIndex ($ oldIndexName );
140+ }
141+
142+ if (!$ dryRun ) {
143+ $ this ->connection ->migrateToSchema ($ schema ->getWrappedSchema ());
144+ }
145+ $ output ->writeln ('<info> ' . $ table ->getName () . ' table updated successfully.</info> ' );
146+ }
147+ }
148+
149+ if ($ dryRun ) {
150+ $ sqlQueries = $ this ->connection ->migrateToSchema ($ schema ->getWrappedSchema (), $ dryRun );
151+ if ($ sqlQueries !== null ) {
152+ $ output ->writeln ($ sqlQueries );
153+ }
154+ }
109155 }
110156
111157 return 0 ;
0 commit comments