Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go/vt/wrangler/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions go/vt/wrangler/testlib/copy_schema_shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down Expand Up @@ -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{})
Expand Down