diff --git a/go/vt/mysqlctl/schema.go b/go/vt/mysqlctl/schema.go index 7d57c5488cb..e84c3812961 100644 --- a/go/vt/mysqlctl/schema.go +++ b/go/vt/mysqlctl/schema.go @@ -19,6 +19,7 @@ package mysqlctl import ( "fmt" "regexp" + "sort" "strings" "vitess.io/vitess/go/vt/vtgate/evalengine" @@ -75,7 +76,7 @@ func (mysqld *Mysqld) GetSchema(ctx context.Context, dbName string, tables, excl return sd, nil } - sd.TableDefinitions = make([]*tabletmanagerdatapb.TableDefinition, 0, len(qr.Rows)) + tds := make(tableDefinitions, 0, len(qr.Rows)) for _, row := range qr.Rows { tableName := row[0].ToString() tableType := row[1].ToString() @@ -133,9 +134,13 @@ func (mysqld *Mysqld) GetSchema(ctx context.Context, dbName string, tables, excl td.Type = tableType td.DataLength = dataLength td.RowCount = rowCount - sd.TableDefinitions = append(sd.TableDefinitions, td) + tds = append(tds, td) } + sort.Sort(tds) + + sd.TableDefinitions = tds + sd, err = tmutils.FilterTables(sd, tables, excludeTables, includeViews) if err != nil { return nil, err @@ -374,3 +379,20 @@ func (mysqld *Mysqld) ApplySchemaChange(ctx context.Context, dbName string, chan return &tabletmanagerdatapb.SchemaChangeResult{BeforeSchema: beforeSchema, AfterSchema: afterSchema}, nil } + +//tableDefinitions is a sortable collection of table definitions +type tableDefinitions []*tabletmanagerdatapb.TableDefinition + +func (t tableDefinitions) Len() int { + return len(t) +} + +func (t tableDefinitions) Less(i, j int) bool { + return t[i].Name < t[j].Name +} + +func (t tableDefinitions) Swap(i, j int) { + t[i], t[j] = t[j], t[i] +} + +var _ sort.Interface = (tableDefinitions)(nil) diff --git a/go/vt/mysqlctl/tabledefinitions_test.go b/go/vt/mysqlctl/tabledefinitions_test.go new file mode 100644 index 00000000000..bbb448bf4b1 --- /dev/null +++ b/go/vt/mysqlctl/tabledefinitions_test.go @@ -0,0 +1,46 @@ +/* +Copyright 2020 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package mysqlctl + +import ( + "sort" + "testing" + + "vitess.io/vitess/go/test/utils" +) + +func TestTableDefinitionsAreSorted(t *testing.T) { + // assert that two collections of table definitions are comparable even if + // the inputs are ordered differently + tds1 := tableDefinitions{{ + Name: "table1", + Schema: "schema1", + }, { + Name: "table2", + Schema: "schema2", + }} + tds2 := tableDefinitions{{ + Name: "table2", + Schema: "schema2", + }, { + Name: "table1", + Schema: "schema1", + }} + sort.Sort(tds1) + sort.Sort(tds2) + utils.MustMatch(t, tds1, tds2, "") +}