diff --git a/go/vt/wrangler/schema.go b/go/vt/wrangler/schema.go index c743a303dd1..46374e7918b 100644 --- a/go/vt/wrangler/schema.go +++ b/go/vt/wrangler/schema.go @@ -323,6 +323,12 @@ func (wr *Wrangler) CopySchemaShard(ctx context.Context, sourceTabletAlias *topo return fmt.Errorf("GetSchema(%v, %v, %v, %v) failed: %v", sourceTabletAlias, tables, excludeTables, includeViews, err) } createSQL := tmutils.SchemaDefinitionToSQLStrings(sourceSd) + + // We're not clever enough to create tables in an order that's fk friendly, + // so we disable fk checks for the remainder of the (non-pooled) db connection + // using a session variable. + createSQL = append([]string{"set foreign_key_checks = 0"}, createSQL...) + destTabletInfo, err := wr.ts.GetTablet(ctx, destShardInfo.MasterAlias) if err != nil { return fmt.Errorf("GetTablet(%v) failed: %v", destShardInfo.MasterAlias, err) @@ -434,6 +440,8 @@ func (wr *Wrangler) applySQLShard(ctx context.Context, tabletInfo *topo.TabletIn ctx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() // Need to make sure that we enable binlog, since we're only applying the statement on masters. + // Do not set "usePool" to true. The SQL statements may change session settings, like + // disabling foreign key checks. _, err = wr.tmc.ExecuteFetchAsDba(ctx, tabletInfo.Tablet, false, []byte(filledChange), 0, false, reloadSchema) return err } diff --git a/go/vt/wrangler/testlib/copy_schema_shard_test.go b/go/vt/wrangler/testlib/copy_schema_shard_test.go index 024a44be75b..65a52531299 100644 --- a/go/vt/wrangler/testlib/copy_schema_shard_test.go +++ b/go/vt/wrangler/testlib/copy_schema_shard_test.go @@ -99,6 +99,7 @@ func copySchema(t *testing.T, useShardAsSource bool) { sourceRdonly.FakeMysqlDaemon.Schema = schema changeToDb := "USE vt_ks" + disableFkChecksSession := "set foreign_key_checks = 0" createDb := "CREATE DATABASE `vt_ks` /*!40100 DEFAULT CHARACTER SET utf8 */" createTable := "CREATE TABLE `vt_ks`.`table1` (\n" + " `id` bigint(20) NOT NULL AUTO_INCREMENT,\n" + @@ -140,6 +141,7 @@ func copySchema(t *testing.T, useShardAsSource bool) { // The destination table is asked to create the new schema. destinationMasterDb.AddQuery(changeToDb, &sqltypes.Result{}) + destinationMasterDb.AddQuery(disableFkChecksSession, &sqltypes.Result{}) destinationMasterDb.AddQuery(createDb, &sqltypes.Result{}) destinationMasterDb.AddQuery(createTable, &sqltypes.Result{}) destinationMasterDb.AddQuery(createTableView, &sqltypes.Result{})