@@ -184,6 +184,7 @@ private function getElementsWithFixedName(array $tableData): array
184
184
* @param string $tableName
185
185
* @param array $tableData
186
186
* @return array
187
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
187
188
*/
188
189
private function getElementsWithAutogeneratedName (Schema $ schema , string $ tableName , array $ tableData ) : array
189
190
{
@@ -193,26 +194,40 @@ private function getElementsWithAutogeneratedName(Schema $schema, string $tableN
193
194
$ elementType = 'index ' ;
194
195
if (!empty ($ tableData [$ elementType ])) {
195
196
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
+ }
202
205
}
203
206
}
204
207
205
208
$ elementType = 'constraint ' ;
206
209
if (!empty ($ tableData [$ elementType ])) {
207
210
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 ;
210
223
} 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
+ }
216
231
}
217
232
if ($ constraintName ) {
218
233
$ declaredStructure [$ elementType ][$ constraintName ] = true ;
@@ -223,34 +238,6 @@ private function getElementsWithAutogeneratedName(Schema $schema, string $tableN
223
238
return $ declaredStructure ;
224
239
}
225
240
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
-
254
241
/**
255
242
* Load db_schema content from the primary scope app/etc/db_schema.xml.
256
243
*
0 commit comments