From a9bab82ffb5ea4605722f457b8e51ada9a0c1cb5 Mon Sep 17 00:00:00 2001 From: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:47:03 +0200 Subject: [PATCH] `schemadiff`: consistent key ordering in table diff (#17141) Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> --- go/vt/schemadiff/schema_diff.go | 4 ++-- go/vt/schemadiff/schema_diff_test.go | 2 +- go/vt/schemadiff/table.go | 6 ++++-- go/vt/schemadiff/table_test.go | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/go/vt/schemadiff/schema_diff.go b/go/vt/schemadiff/schema_diff.go index 1c161ab56bf..158d134f520 100644 --- a/go/vt/schemadiff/schema_diff.go +++ b/go/vt/schemadiff/schema_diff.go @@ -280,8 +280,8 @@ func (d *SchemaDiff) UnorderedDiffs() []EntityDiff { return d.diffs } -// AllDependenciess returns all known dependencies -func (d *SchemaDiff) AllDependenciess() (deps []*DiffDependency) { +// AllDependencies returns all known dependencies +func (d *SchemaDiff) AllDependencies() (deps []*DiffDependency) { for _, dep := range d.dependencies { deps = append(deps, dep) } diff --git a/go/vt/schemadiff/schema_diff_test.go b/go/vt/schemadiff/schema_diff_test.go index 8088cc896ed..b39166451b2 100644 --- a/go/vt/schemadiff/schema_diff_test.go +++ b/go/vt/schemadiff/schema_diff_test.go @@ -1272,7 +1272,7 @@ func TestSchemaDiff(t *testing.T) { } assert.Equalf(t, tc.expectDiffs, len(allDiffs), "found diffs: %v", allDiffsStatements) - deps := schemaDiff.AllDependenciess() + deps := schemaDiff.AllDependencies() depsKeys := []string{} for _, dep := range deps { depsKeys = append(depsKeys, dep.hashKey()) diff --git a/go/vt/schemadiff/table.go b/go/vt/schemadiff/table.go index c429ab15ba4..e002ef18e15 100644 --- a/go/vt/schemadiff/table.go +++ b/go/vt/schemadiff/table.go @@ -1711,8 +1711,10 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable, } } } - for _, stmt := range dropKeyStatements { - alterTable.AlterOptions = append(alterTable.AlterOptions, stmt) + for _, t1Key := range t1Keys { + if stmt, ok := dropKeyStatements[t1Key.Info.Name.String()]; ok { + alterTable.AlterOptions = append(alterTable.AlterOptions, stmt) + } } return superfluousFulltextKeys } diff --git a/go/vt/schemadiff/table_test.go b/go/vt/schemadiff/table_test.go index 6526c5ae118..84c40d769c2 100644 --- a/go/vt/schemadiff/table_test.go +++ b/go/vt/schemadiff/table_test.go @@ -733,6 +733,24 @@ func TestCreateTableDiff(t *testing.T) { "- KEY `i_idx` (`i`)", }, }, + { + name: "multiple dropped keys and columns", + from: "create table t1 (`id` int, i1 int, i2 int, i3 int, primary key (id), key k1(i1), key k2(i2), key k3(i1), key k4(i2), key k5(i1), key k6(i2))", + to: "create table t1 (`id` int, primary key (id))", + diff: "alter table t1 drop key k1, drop key k2, drop key k3, drop key k4, drop key k5, drop key k6, drop column i1, drop column i2, drop column i3", + cdiff: "ALTER TABLE `t1` DROP KEY `k1`, DROP KEY `k2`, DROP KEY `k3`, DROP KEY `k4`, DROP KEY `k5`, DROP KEY `k6`, DROP COLUMN `i1`, DROP COLUMN `i2`, DROP COLUMN `i3`", + textdiffs: []string{ + "- KEY `k1` (`i1`)", + "- KEY `k2` (`i2`)", + "- KEY `k3` (`i1`)", + "- KEY `k4` (`i2`)", + "- KEY `k5` (`i1`)", + "- KEY `k6` (`i2`)", + "- `i1` int,", + "- `i2` int,", + "- `i3` int,", + }, + }, { name: "modified key", from: "create table t1 (`id` int primary key, i int, key i_idx(i))",