Skip to content

Commit

Permalink
schemadiff: Fix handling of primary key (#11059)
Browse files Browse the repository at this point in the history
This fixes the handling of primary key options. When dropping a primary
key, we should use `drop primary key` and not use the name.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink authored Aug 22, 2022
1 parent 70cf2f9 commit 3e9881d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,13 +1137,13 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
t2KeysMap[key.Info.Name.String()] = key
}

dropKeyStatement := func(name sqlparser.IdentifierCI) *sqlparser.DropKey {
dropKeyStatement := func(info *sqlparser.IndexInfo) *sqlparser.DropKey {
dropKey := &sqlparser.DropKey{}
if strings.EqualFold(dropKey.Name.String(), "PRIMARY") {
if strings.EqualFold(info.Type, sqlparser.PrimaryKeyTypeStr) {
dropKey.Type = sqlparser.PrimaryKeyType
} else {
dropKey.Type = sqlparser.NormalKeyType
dropKey.Name = name
dropKey.Name = info.Name
}
return dropKey
}
Expand All @@ -1153,7 +1153,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
for _, t1Key := range t1Keys {
if _, ok := t2KeysMap[t1Key.Info.Name.String()]; !ok {
// column exists in t1 but not in t2, hence it is dropped
dropKey := dropKeyStatement(t1Key.Info.Name)
dropKey := dropKeyStatement(t1Key.Info)
alterTable.AlterOptions = append(alterTable.AlterOptions, dropKey)
}
}
Expand All @@ -1177,7 +1177,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
}

// For other changes, we're going to drop and create.
dropKey := dropKeyStatement(t1Key.Info.Name)
dropKey := dropKeyStatement(t1Key.Info)
addKey := &sqlparser.AddIndexDefinition{
IndexDefinition: t2Key,
}
Expand Down Expand Up @@ -1628,7 +1628,15 @@ func (c *CreateTableEntity) apply(diff *AlterTableEntityDiff) error {
// we expect the named key to be found
found := false
switch opt.Type {
case sqlparser.NormalKeyType, sqlparser.PrimaryKeyType:
case sqlparser.PrimaryKeyType:
for i, idx := range c.TableSpec.Indexes {
if strings.EqualFold(idx.Info.Type, sqlparser.PrimaryKeyTypeStr) {
found = true
c.TableSpec.Indexes = append(c.TableSpec.Indexes[0:i], c.TableSpec.Indexes[i+1:]...)
break
}
}
case sqlparser.NormalKeyType:
for i, index := range c.TableSpec.Indexes {
if strings.EqualFold(index.Info.Name.String(), opt.Name.String()) {
found = true
Expand Down
8 changes: 4 additions & 4 deletions go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ func TestCreateTableDiff(t *testing.T) {
name: "dropped primary key",
from: "create table t1 (id int, primary key(id))",
to: "create table t2 (id int)",
diff: "alter table t1 drop key `PRIMARY`",
cdiff: "ALTER TABLE `t1` DROP KEY `PRIMARY`",
diff: "alter table t1 drop primary key",
cdiff: "ALTER TABLE `t1` DROP PRIMARY KEY",
},
{
name: "dropped key",
Expand All @@ -379,8 +379,8 @@ func TestCreateTableDiff(t *testing.T) {
name: "modified primary key",
from: "create table t1 (`id` int, i int, primary key(id), key i_idx(i))",
to: "create table t2 (`id` int, i int, primary key(id, i),key i_idx(`i`))",
diff: "alter table t1 drop key `PRIMARY`, add primary key (id, i)",
cdiff: "ALTER TABLE `t1` DROP KEY `PRIMARY`, ADD PRIMARY KEY (`id`, `i`)",
diff: "alter table t1 drop primary key, add primary key (id, i)",
cdiff: "ALTER TABLE `t1` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `i`)",
},
{
name: "reordered key, no diff",
Expand Down

0 comments on commit 3e9881d

Please sign in to comment.