@@ -646,6 +646,21 @@ func (e *Executor) executeDirectly(ctx context.Context, onlineDDL *schema.Online
646
646
}
647
647
648
648
_ = e .onSchemaMigrationStatus (ctx , onlineDDL .UUID , schema .OnlineDDLStatusRunning , false , progressPctStarted , etaSecondsUnknown , rowsCopiedUnknown , emptyHint )
649
+ if onlineDDL .StrategySetting ().IsAllowForeignKeysFlag () {
650
+ // Foreign key support is curently "unsafe". We further put the burden on the user
651
+ // by disabling foreign key checks. With this, the user is able to create cyclic
652
+ // foreign key references (e.g. t1<->t2) without going through the trouble of
653
+ // CREATE TABLE t1->CREATE TABLE t2->ALTER TABLE t1 ADD FOREIGN KEY ... REFERENCES ts
654
+ // Grab current sql_mode value
655
+ if _ , err := conn .ExecuteFetch (`set @vt_onlineddl_foreign_key_checks=@@foreign_key_checks` , 0 , false ); err != nil {
656
+ return false , vterrors .Errorf (vtrpcpb .Code_UNKNOWN , "could not read foreign_key_checks: %v" , err )
657
+ }
658
+ _ , err = conn .ExecuteFetch ("SET foreign_key_checks=0" , 0 , false )
659
+ if err != nil {
660
+ return false , err
661
+ }
662
+ defer conn .ExecuteFetch ("SET foreign_key_checks=@vt_onlineddl_foreign_key_checks" , 0 , false )
663
+ }
649
664
_ , err = conn .ExecuteFetch (onlineDDL .SQL , 0 , false )
650
665
651
666
if err != nil {
@@ -1288,7 +1303,7 @@ func (e *Executor) newConstraintName(onlineDDL *schema.OnlineDDL, constraintType
1288
1303
// validateAndEditCreateTableStatement inspects the CreateTable AST and does the following:
1289
1304
// - extra validation (no FKs for now...)
1290
1305
// - generate new and unique names for all constraints (CHECK and FK; yes, why not handle FK names; even as we don't support FKs today, we may in the future)
1291
- func (e * Executor ) validateAndEditCreateTableStatement (ctx context. Context , onlineDDL * schema.OnlineDDL , createTable * sqlparser.CreateTable ) (constraintMap map [string ]string , err error ) {
1306
+ func (e * Executor ) validateAndEditCreateTableStatement (onlineDDL * schema.OnlineDDL , createTable * sqlparser.CreateTable ) (constraintMap map [string ]string , err error ) {
1292
1307
constraintMap = map [string ]string {}
1293
1308
hashExists := map [string ]bool {}
1294
1309
@@ -1315,7 +1330,7 @@ func (e *Executor) validateAndEditCreateTableStatement(ctx context.Context, onli
1315
1330
// validateAndEditAlterTableStatement inspects the AlterTable statement and:
1316
1331
// - modifies any CONSTRAINT name according to given name mapping
1317
1332
// - explode ADD FULLTEXT KEY into multiple statements
1318
- func (e * Executor ) validateAndEditAlterTableStatement (ctx context. Context , capableOf capabilities.CapableOf , onlineDDL * schema.OnlineDDL , alterTable * sqlparser.AlterTable , constraintMap map [string ]string ) (alters []* sqlparser.AlterTable , err error ) {
1333
+ func (e * Executor ) validateAndEditAlterTableStatement (capableOf capabilities.CapableOf , onlineDDL * schema.OnlineDDL , alterTable * sqlparser.AlterTable , constraintMap map [string ]string ) (alters []* sqlparser.AlterTable , err error ) {
1319
1334
capableOfInstantDDLXtrabackup , err := capableOf (capabilities .InstantDDLXtrabackupCapability )
1320
1335
if err != nil {
1321
1336
return nil , err
@@ -1405,7 +1420,7 @@ func (e *Executor) duplicateCreateTable(ctx context.Context, onlineDDL *schema.O
1405
1420
newCreateTable .SetTable (newCreateTable .GetTable ().Qualifier .CompliantName (), newTableName )
1406
1421
// manipulate CreateTable statement: take care of constraints names which have to be
1407
1422
// unique across the schema
1408
- constraintMap , err = e .validateAndEditCreateTableStatement (ctx , onlineDDL , newCreateTable )
1423
+ constraintMap , err = e .validateAndEditCreateTableStatement (onlineDDL , newCreateTable )
1409
1424
if err != nil {
1410
1425
return nil , nil , nil , err
1411
1426
}
@@ -1475,7 +1490,7 @@ func (e *Executor) initVreplicationOriginalMigration(ctx context.Context, online
1475
1490
// Also, change any constraint names:
1476
1491
1477
1492
capableOf := mysql .ServerVersionCapableOf (conn .ServerVersion )
1478
- alters , err := e .validateAndEditAlterTableStatement (ctx , capableOf , onlineDDL , alterTable , constraintMap )
1493
+ alters , err := e .validateAndEditAlterTableStatement (capableOf , onlineDDL , alterTable , constraintMap )
1479
1494
if err != nil {
1480
1495
return v , err
1481
1496
}
@@ -2995,7 +3010,7 @@ func (e *Executor) executeCreateDDLActionMigration(ctx context.Context, onlineDD
2995
3010
newCreateTable := sqlparser .CloneRefOfCreateTable (originalCreateTable )
2996
3011
// Rewrite this CREATE TABLE statement such that CONSTRAINT names are edited,
2997
3012
// specifically removing any <tablename> prefix.
2998
- if _ , err := e .validateAndEditCreateTableStatement (ctx , onlineDDL , newCreateTable ); err != nil {
3013
+ if _ , err := e .validateAndEditCreateTableStatement (onlineDDL , newCreateTable ); err != nil {
2999
3014
return failMigration (err )
3000
3015
}
3001
3016
ddlStmt = newCreateTable
0 commit comments