Skip to content

Commit 157d1c9

Browse files
committed
Fix issue when underlying index of ForeignKey tries to be removed without FK itself
in MySQL it causes issues
1 parent cb7b28f commit 157d1c9

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

Orm/Xtensive.Orm/Orm/Upgrade/Internals/SqlActionTranslator.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal sealed class SqlActionTranslator
4545
private readonly List<string> enforceChangedColumns = new List<string>();
4646
private readonly ISqlExecutor sqlExecutor;
4747
private readonly bool allowCreateConstraints;
48+
private readonly bool removeFkBeforeIndex;
4849

4950
private readonly string collationName;
5051
private readonly ActionSequence actions;
@@ -60,6 +61,7 @@ internal sealed class SqlActionTranslator
6061
private readonly List<DataAction> clearDataActions = new List<DataAction>();
6162
private readonly HashSet<TableColumn> recreatedColumns = new HashSet<TableColumn>();
6263
private readonly Dictionary<string, SequenceDescriptor> removedGeneratorDescriptors = new Dictionary<string, SequenceDescriptor>();
64+
private readonly List<(Table, string)> removedForeignKeysDueToIndexes = new List<(Table, string)>();
6365

6466

6567
private UpgradeActionSequenceBuilder currentOutput;
@@ -563,8 +565,16 @@ private void VisitRemoveSecondaryIndexAction(RemoveNodeAction action)
563565
return;
564566

565567
var index = table.Indexes[secondaryIndexInfo.Name];
568+
if (removeFkBeforeIndex && secondaryIndexInfo.Parent.ForeignKeys.TryGetValue(secondaryIndexInfo.Name, out var dependantFk)) {
569+
var foreignKey = table.TableConstraints[dependantFk.Name];
570+
if (foreignKey != null) {
571+
removedForeignKeysDueToIndexes.Add((table, foreignKey.Name));
572+
preCleanupDataOutput.RegisterCommand(SqlDdl.Alter(table, SqlDdl.DropConstraint(foreignKey)));
573+
_ = table.TableConstraints.Remove(foreignKey);
574+
}
575+
}
566576
preCleanupDataOutput.RegisterCommand(SqlDdl.Drop(index));
567-
table.Indexes.Remove(index);
577+
_ = table.Indexes.Remove(index);
568578
}
569579

570580
private void VisitAlterSecondaryIndexAction(NodeAction action)
@@ -595,9 +605,14 @@ private void VisitRemoveForeignKeyAction(RemoveNodeAction action)
595605
if (table==null)
596606
return;
597607

608+
if (removeFkBeforeIndex && removedForeignKeysDueToIndexes.Contains((table, foreignKeyInfo.Name))) {
609+
// index removal already causes FK removal
610+
return;
611+
}
612+
598613
var foreignKey = table.TableConstraints[foreignKeyInfo.Name];
599614
preCleanupDataOutput.RegisterCommand(SqlDdl.Alter(table, SqlDdl.DropConstraint(foreignKey)));
600-
table.TableConstraints.Remove(foreignKey);
615+
_ = table.TableConstraints.Remove(foreignKey);
601616
}
602617

603618
private void VisitAlterForeignKeyAction(NodeAction action)
@@ -1270,7 +1285,6 @@ public SqlActionTranslator(
12701285
driver = handlers.StorageDriver;
12711286
providerInfo = handlers.ProviderInfo;
12721287
sequenceQueryBuilder = handlers.SequenceQueryBuilder;
1273-
providerInfo = handlers.ProviderInfo;
12741288
typeIdColumnName = handlers.NameBuilder.TypeIdColumnName;
12751289

12761290
this.resolver = resolver;
@@ -1287,6 +1301,9 @@ public SqlActionTranslator(
12871301
if (!string.IsNullOrEmpty(collation))
12881302
collationName = collation;
12891303
}
1304+
if (providerInfo.ProviderName == WellKnown.Provider.MySql) {
1305+
removeFkBeforeIndex = true;
1306+
}
12901307
}
12911308
}
12921309
}

0 commit comments

Comments
 (0)