@@ -184,6 +184,7 @@ private function getElementsWithFixedName(array $tableData): array
184184 * @param string $tableName
185185 * @param array $tableData
186186 * @return array
187+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
187188 */
188189 private function getElementsWithAutogeneratedName (Schema $ schema , string $ tableName , array $ tableData ) : array
189190 {
@@ -193,26 +194,40 @@ private function getElementsWithAutogeneratedName(Schema $schema, string $tableN
193194 $ elementType = 'index ' ;
194195 if (!empty ($ tableData [$ elementType ])) {
195196 foreach ($ tableData [$ elementType ] as $ tableElementData ) {
196- $ indexName = $ this ->elementNameResolver ->getFullIndexName (
197- $ table ,
198- $ tableElementData ['column ' ] ?? [],
199- $ tableElementData ['indexType ' ] ?? null
200- );
201- $ declaredStructure [$ elementType ][$ indexName ] = true ;
197+ if ($ table ->getIndexByName ($ tableElementData ['referenceId ' ])) {
198+ $ indexName = $ this ->elementNameResolver ->getFullIndexName (
199+ $ table ,
200+ $ tableElementData ['column ' ],
201+ $ tableElementData ['indexType ' ] ?? null
202+ );
203+ $ declaredStructure [$ elementType ][$ indexName ] = true ;
204+ }
202205 }
203206 }
204207
205208 $ elementType = 'constraint ' ;
206209 if (!empty ($ tableData [$ elementType ])) {
207210 foreach ($ tableData [$ elementType ] as $ tableElementData ) {
208- if ($ tableElementData ['type ' ] === 'foreign ' && isset ($ tableElementData ['referenceTable ' ])) {
209- $ constraintName = $ this ->getConstraintName ($ schema , $ tableName , $ tableElementData );
211+ $ constraintName = null ;
212+ if ($ tableElementData ['type ' ] === 'foreign ' && $ table ->getConstraintByName ($ tableElementData ['referenceId ' ])) {
213+ $ referenceTable = $ schema ->getTableByName ($ tableElementData ['referenceTable ' ]);
214+ $ column = $ table ->getColumnByName ($ tableElementData ['column ' ]);
215+ $ referenceColumn = $ referenceTable ->getColumnByName ($ tableElementData ['referenceColumn ' ]);
216+ $ constraintName = ($ column !== false && $ referenceColumn !== false ) ?
217+ $ this ->elementNameResolver ->getFullFKName (
218+ $ table ,
219+ $ column ,
220+ $ referenceTable ,
221+ $ referenceColumn
222+ ) : null ;
210223 } else {
211- $ constraintName = $ this ->elementNameResolver ->getFullIndexName (
212- $ table ,
213- $ tableElementData ['column ' ] ?? [],
214- $ tableElementData ['type ' ] ?? null
215- );
224+ if ($ table ->getIndexByName ($ tableElementData ['referenceId ' ])) {
225+ $ constraintName = $ this ->elementNameResolver ->getFullIndexName (
226+ $ table ,
227+ $ tableElementData ['column ' ],
228+ $ tableElementData ['type ' ]
229+ );
230+ }
216231 }
217232 if ($ constraintName ) {
218233 $ declaredStructure [$ elementType ][$ constraintName ] = true ;
@@ -223,34 +238,6 @@ private function getElementsWithAutogeneratedName(Schema $schema, string $tableN
223238 return $ declaredStructure ;
224239 }
225240
226- /**
227- * Provide autogenerated names of the table constraint.
228- *
229- * @param Schema $schema
230- * @param string $tableName
231- * @param array $tableElementData
232- * @return string|null
233- */
234- private function getConstraintName (Schema $ schema , string $ tableName , array $ tableElementData ): ?string
235- {
236- $ table = $ schema ->getTableByName ($ tableName );
237-
238- $ referenceTable = isset ($ tableElementData ['referenceTable ' ])
239- ? $ schema ->getTableByName ($ tableElementData ['referenceTable ' ]) : false ;
240- $ column = isset ($ tableElementData ['column ' ])
241- ? $ table ->getColumnByName ($ tableElementData ['column ' ]) : false ;
242- $ referenceColumn = isset ($ tableElementData ['referenceColumn ' ])
243- ? $ referenceTable ->getColumnByName ($ tableElementData ['referenceColumn ' ]) : false ;
244-
245- return ($ column !== false && $ referenceColumn !== false && $ referenceTable !== false ) ?
246- $ this ->elementNameResolver ->getFullFKName (
247- $ table ,
248- $ column ,
249- $ referenceTable ,
250- $ referenceColumn
251- ) : null ;
252- }
253-
254241 /**
255242 * Load db_schema content from the primary scope app/etc/db_schema.xml.
256243 *
0 commit comments