@@ -996,7 +996,6 @@ func (e *Executor) cutOverVReplMigration(ctx context.Context, s *VReplStream, sh
996
996
}
997
997
998
998
renameQuery := sqlparser .BuildParsedQuery (sqlSwapTables , onlineDDL .Table , sentryTableName , vreplTable , onlineDDL .Table , sentryTableName , vreplTable )
999
-
1000
999
waitForRenameProcess := func () error {
1001
1000
// This function waits until it finds the RENAME TABLE... query running in MySQL's PROCESSLIST, or until timeout
1002
1001
// The function assumes that one of the renamed tables is locked, thus causing the RENAME to block. If nothing
@@ -1404,6 +1403,25 @@ func (e *Executor) duplicateCreateTable(ctx context.Context, onlineDDL *schema.O
1404
1403
}
1405
1404
newCreateTable = sqlparser .Clone (originalCreateTable )
1406
1405
newCreateTable .SetTable (newCreateTable .GetTable ().Qualifier .CompliantName (), newTableName )
1406
+
1407
+ // If this table has a self-referencing foreign key constraint, ensure the referenced table gets renamed:
1408
+ renameSelfFK := func (node sqlparser.SQLNode ) (kontinue bool , err error ) {
1409
+ switch node := node .(type ) {
1410
+ case * sqlparser.ConstraintDefinition :
1411
+ fk , ok := node .Details .(* sqlparser.ForeignKeyDefinition )
1412
+ if ! ok {
1413
+ return true , nil
1414
+ }
1415
+ if referencedTableName := fk .ReferenceDefinition .ReferencedTable .Name .String (); referencedTableName == originalCreateTable .Table .Name .String () {
1416
+ // This is a self-referencing foreign key
1417
+ // We need to rename the referenced table as well
1418
+ fk .ReferenceDefinition .ReferencedTable .Name = sqlparser .NewIdentifierCS (newTableName )
1419
+ }
1420
+ }
1421
+ return true , nil
1422
+ }
1423
+ _ = sqlparser .Walk (renameSelfFK , newCreateTable )
1424
+
1407
1425
// manipulate CreateTable statement: take care of constraints names which have to be
1408
1426
// unique across the schema
1409
1427
constraintMap , err = e .validateAndEditCreateTableStatement (onlineDDL , newCreateTable )
0 commit comments