diff --git a/go/vt/vtctl/vtctl.go b/go/vt/vtctl/vtctl.go index 571a5896900..997f1eae885 100644 --- a/go/vt/vtctl/vtctl.go +++ b/go/vt/vtctl/vtctl.go @@ -2263,6 +2263,7 @@ func commandMoveTables(ctx context.Context, wr *wrangler.Wrangler, subFlags *fla autoStart := subFlags.Bool("auto_start", true, "If false, streams will start in the Stopped state and will need to be explicitly started") stopAfterCopy := subFlags.Bool("stop_after_copy", false, "Streams will be stopped once the copy phase is completed") + dropConstraints := subFlags.Bool("drop_constraints", false, "If true, tables in the target keyspace will be created without constraints.") _ = subFlags.Bool("v1", true, "") if err := subFlags.Parse(args); err != nil { @@ -2288,7 +2289,7 @@ func commandMoveTables(ctx context.Context, wr *wrangler.Wrangler, subFlags *fla target := subFlags.Arg(1) tableSpecs := subFlags.Arg(2) return wr.MoveTables(ctx, *workflow, source, target, tableSpecs, *cells, *tabletTypes, *allTables, - *excludes, *autoStart, *stopAfterCopy, "") + *excludes, *autoStart, *stopAfterCopy, "", *dropConstraints) } // VReplicationWorkflowAction defines subcommands passed to vtctl for movetables or reshard diff --git a/go/vt/wrangler/materializer.go b/go/vt/wrangler/materializer.go index d20065addb1..394bcb8d92a 100644 --- a/go/vt/wrangler/materializer.go +++ b/go/vt/wrangler/materializer.go @@ -117,7 +117,7 @@ func shouldInclude(table string, excludes []string) bool { // MoveTables initiates moving table(s) over to another keyspace func (wr *Wrangler) MoveTables(ctx context.Context, workflow, sourceKeyspace, targetKeyspace, tableSpecs, cell, tabletTypes string, allTables bool, excludeTables string, autoStart, stopAfterCopy bool, - externalCluster string) error { + externalCluster string, dropConstraints bool) error { //FIXME validate tableSpecs, allTables, excludeTables var tables []string var externalTopo *topo.Server @@ -242,13 +242,19 @@ func (wr *Wrangler) MoveTables(ctx context.Context, workflow, sourceKeyspace, ta StopAfterCopy: stopAfterCopy, ExternalCluster: externalCluster, } + + createDDLMode := createDDLAsCopy + if dropConstraints { + createDDLMode = createDDLAsCopyDropConstraint + } + for _, table := range tables { buf := sqlparser.NewTrackedBuffer(nil) buf.Myprintf("select * from %v", sqlparser.NewTableIdent(table)) ms.TableSettings = append(ms.TableSettings, &vtctldatapb.TableMaterializeSettings{ TargetTable: table, SourceExpression: buf.String(), - CreateDdl: createDDLAsCopy, + CreateDdl: createDDLMode, }) } mz, err := wr.prepareMaterializerStreams(ctx, ms) diff --git a/go/vt/wrangler/materializer_test.go b/go/vt/wrangler/materializer_test.go index c58d5adf588..d132e28e07e 100644 --- a/go/vt/wrangler/materializer_test.go +++ b/go/vt/wrangler/materializer_test.go @@ -62,7 +62,7 @@ func TestMigrateTables(t *testing.T) { env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{}) ctx := context.Background() - err := env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1", "", "", false, "", true, false, "") + err := env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1", "", "", false, "", true, false, "", false) require.NoError(t, err) vschema, err := env.wr.ts.GetSrvVSchema(ctx, env.cell) require.NoError(t, err) @@ -103,11 +103,11 @@ func TestMissingTables(t *testing.T) { env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{}) ctx := context.Background() - err := env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1,tyt", "", "", false, "", true, false, "") + err := env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1,tyt", "", "", false, "", true, false, "", false) require.EqualError(t, err, "table(s) not found in source keyspace sourceks: tyt") - err = env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1,tyt,t2,txt", "", "", false, "", true, false, "") + err = env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1,tyt,t2,txt", "", "", false, "", true, false, "", false) require.EqualError(t, err, "table(s) not found in source keyspace sourceks: tyt,txt") - err = env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1", "", "", false, "", true, false, "") + err = env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1", "", "", false, "", true, false, "", false) require.NoError(t, err) } @@ -163,7 +163,7 @@ func TestMoveTablesAllAndExclude(t *testing.T) { env.tmc.expectVRQuery(200, insertPrefix, &sqltypes.Result{}) env.tmc.expectVRQuery(200, mzSelectIDQuery, &sqltypes.Result{}) env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{}) - err = env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "", "", "", tcase.allTables, tcase.excludeTables, true, false, "") + err = env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "", "", "", tcase.allTables, tcase.excludeTables, true, false, "", false) require.NoError(t, err) require.EqualValues(t, tcase.want, targetTables(env)) }) @@ -197,7 +197,7 @@ func TestMoveTablesStopFlags(t *testing.T) { env.tmc.expectVRQuery(200, mzSelectIDQuery, &sqltypes.Result{}) // -auto_start=false is tested by NOT expecting the update query which sets state to RUNNING err = env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", "t1", "", - "", false, "", false, true, "") + "", false, "", false, true, "", false) require.NoError(t, err) env.tmc.verifyQueries(t) }) @@ -223,7 +223,7 @@ func TestMigrateVSchema(t *testing.T) { env.tmc.expectVRQuery(200, mzUpdateQuery, &sqltypes.Result{}) ctx := context.Background() - err := env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", `{"t1":{}}`, "", "", false, "", true, false, "") + err := env.wr.MoveTables(ctx, "workflow", "sourceks", "targetks", `{"t1":{}}`, "", "", false, "", true, false, "", false) require.NoError(t, err) vschema, err := env.wr.ts.GetSrvVSchema(ctx, env.cell) require.NoError(t, err) diff --git a/go/vt/wrangler/workflow.go b/go/vt/wrangler/workflow.go index 84476d646d1..f1db6e42275 100644 --- a/go/vt/wrangler/workflow.go +++ b/go/vt/wrangler/workflow.go @@ -54,9 +54,9 @@ type VReplicationWorkflowParams struct { AllTables, RenameTables bool // Reshard specific - SourceShards, TargetShards []string - SkipSchemaCopy bool - AutoStart, StopAfterCopy bool + SourceShards, TargetShards []string + SkipSchemaCopy bool + AutoStart, StopAfterCopy, DropConstraints bool // Migrate specific ExternalCluster string @@ -410,7 +410,7 @@ func (vrw *VReplicationWorkflow) initMoveTables() error { log.Infof("In VReplicationWorkflow.initMoveTables() for %+v", vrw) return vrw.wr.MoveTables(vrw.ctx, vrw.params.Workflow, vrw.params.SourceKeyspace, vrw.params.TargetKeyspace, vrw.params.Tables, vrw.params.Cells, vrw.params.TabletTypes, vrw.params.AllTables, vrw.params.ExcludeTables, - vrw.params.AutoStart, vrw.params.StopAfterCopy, vrw.params.ExternalCluster) + vrw.params.AutoStart, vrw.params.StopAfterCopy, vrw.params.ExternalCluster, vrw.params.DropConstraints) } func (vrw *VReplicationWorkflow) initReshard() error {