Skip to content

Commit

Permalink
Materializer: normalize schema via schemadiff on --atomic-copy
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
  • Loading branch information
shlomi-noach committed Nov 29, 2023
1 parent 5d4ffd0 commit 36a981a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go/test/endtoend/vreplication/fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestFKWorkflow(t *testing.T) {
}
targetKeyspace := "fktarget"
targetTabletId := 200
vc.AddKeyspace(t, []*Cell{cell}, targetKeyspace, shardName, initialFKTargetVSchema, initialFKSchema, 0, 0, targetTabletId, sourceKsOpts)
vc.AddKeyspace(t, []*Cell{cell}, targetKeyspace, shardName, initialFKTargetVSchema, "", 0, 0, targetTabletId, sourceKsOpts)
vtgate.WaitForStatusOfTabletInShard(fmt.Sprintf("%s.%s.primary", targetKeyspace, shardName), 1, 30*time.Second)

workflowName := "fk"
Expand Down
17 changes: 17 additions & 0 deletions go/vt/vtctl/workflow/materializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/mysqlctl/tmutils"
"vitess.io/vitess/go/vt/schemadiff"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vtctl/schematools"
Expand Down Expand Up @@ -444,7 +445,23 @@ func (mz *materializer) deploySchema() error {
}

if len(applyDDLs) > 0 {
if mz.ms.AtomicCopy {
// AtomicCopy suggests we may be interested in Foreign Key support. As such, we want to
// normalize the source schema: ensure the order of table definitions is compatible with
// the constraints graph. We want to first create the parents, then the children.
// We use schemadiff to normalize the schema.
// For now, and because this is could have wider implications, we ignore any errors in
// reading the source schema.
schema, err := schemadiff.NewSchemaFromQueries(applyDDLs)
if err != nil {
log.Error(vterrors.Wrapf(err, "AtomicCopy: failed to normalize schema via schemadiff"))
} else {
applyDDLs = schema.ToQueries()
log.Infof("AtomicCopy used, and schema was normalized via schemadiff. New queries: %v", applyDDLs)
}
}
sql := strings.Join(applyDDLs, ";\n")
log.Infof("materializer.deploySchema: sql=%v", sql)

_, err = mz.tmc.ApplySchema(mz.ctx, targetTablet.Tablet, &tmutils.SchemaChange{
SQL: sql,
Expand Down
17 changes: 17 additions & 0 deletions go/vt/wrangler/materializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/mysqlctl/tmutils"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/schemadiff"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
Expand Down Expand Up @@ -1260,7 +1261,23 @@ func (mz *materializer) deploySchema(ctx context.Context) error {
}

if len(applyDDLs) > 0 {
if mz.ms.AtomicCopy {
// AtomicCopy suggests we may be interested in Foreign Key support. As such, we want to
// normalize the source schema: ensure the order of table definitions is compatible with
// the constraints graph. We want to first create the parents, then the children.
// We use schemadiff to normalize the schema.
// For now, and because this is could have wider implications, we ignore any errors in
// reading the source schema.
schema, err := schemadiff.NewSchemaFromQueries(applyDDLs)
if err != nil {
log.Error(vterrors.Wrapf(err, "AtomicCopy: failed to normalize schema via schemadiff"))
} else {
applyDDLs = schema.ToQueries()
log.Infof("AtomicCopy used, and schema was normalized via schemadiff. New queries: %v", applyDDLs)
}
}
sql := strings.Join(applyDDLs, ";\n")
log.Infof("materializer.deploySchema: sql=%v", sql)

_, err = mz.wr.tmc.ApplySchema(ctx, targetTablet.Tablet, &tmutils.SchemaChange{
SQL: sql,
Expand Down

0 comments on commit 36a981a

Please sign in to comment.