Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schemadiff: FullTextKeyStrategy, handling multiple 'ADD FULLTEXT key' alter options #11012

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,9 @@ func (c *CreateTableEntity) TableDiff(other *CreateTableEntity, hints *DiffHints
Table: otherStmt.Table,
}
diffedTableCharset := ""
var parentAlterTableEntityDiff *AlterTableEntityDiff
var partitionSpecs []*sqlparser.PartitionSpec
var superfluousFulltextKeys []*sqlparser.AddIndexDefinition
{
t1Options := c.CreateTable.TableSpec.Options
t2Options := other.CreateTable.TableSpec.Options
Expand All @@ -639,7 +641,7 @@ func (c *CreateTableEntity) TableDiff(other *CreateTableEntity, hints *DiffHints
// ordered keys for both tables:
t1Keys := c.CreateTable.TableSpec.Indexes
t2Keys := other.CreateTable.TableSpec.Indexes
c.diffKeys(alterTable, t1Keys, t2Keys, hints)
superfluousFulltextKeys = c.diffKeys(alterTable, t1Keys, t2Keys, hints)
}
{
// diff constraints
Expand Down Expand Up @@ -668,11 +670,20 @@ func (c *CreateTableEntity) TableDiff(other *CreateTableEntity, hints *DiffHints
return nil, err
}
}
var parentAlterTableEntityDiff *AlterTableEntityDiff
tableSpecHasChanged := len(alterTable.AlterOptions) > 0 || alterTable.PartitionOption != nil || alterTable.PartitionSpec != nil
if tableSpecHasChanged {
parentAlterTableEntityDiff = &AlterTableEntityDiff{alterTable: alterTable, from: c, to: other}
}
for _, superfluousFulltextKey := range superfluousFulltextKeys {
alterTable := &sqlparser.AlterTable{
Table: otherStmt.Table,
AlterOptions: []sqlparser.AlterOption{superfluousFulltextKey},
}
diff := &AlterTableEntityDiff{alterTable: alterTable, from: c, to: other}
// if we got superfluous fulltext keys, that means the table spec has changed, ie
// parentAlterTableEntityDiff is not nil
parentAlterTableEntityDiff.addSubsequentDiff(diff)
}
for _, partitionSpec := range partitionSpecs {
alterTable := &sqlparser.AlterTable{
Table: otherStmt.Table,
Expand Down Expand Up @@ -1103,7 +1114,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
t1Keys []*sqlparser.IndexDefinition,
t2Keys []*sqlparser.IndexDefinition,
hints *DiffHints,
) {
) (superfluousFulltextKeys []*sqlparser.AddIndexDefinition) {
t1KeysMap := map[string]*sqlparser.IndexDefinition{}
t2KeysMap := map[string]*sqlparser.IndexDefinition{}
for _, key := range t1Keys {
Expand Down Expand Up @@ -1134,6 +1145,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
}
}

addedFulltextKeys := 0
for _, t2Key := range t2Keys {
t2KeyName := t2Key.Info.Name.String()
// evaluate modified & added keys:
Expand Down Expand Up @@ -1164,9 +1176,21 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
addKey := &sqlparser.AddIndexDefinition{
IndexDefinition: t2Key,
}
alterTable.AlterOptions = append(alterTable.AlterOptions, addKey)
addedAsSuperfluousStatement := false
if t2Key.Info.Fulltext {
if addedFulltextKeys > 0 && hints.FullTextKeyStrategy == FullTextKeyDistinctStatements {
// Special case: MySQL does not support multiple ADD FULLTEXT KEY statements in a single ALTER
superfluousFulltextKeys = append(superfluousFulltextKeys, addKey)
addedAsSuperfluousStatement = true
}
addedFulltextKeys++
}
if !addedAsSuperfluousStatement {
alterTable.AlterOptions = append(alterTable.AlterOptions, addKey)
}
}
}
return superfluousFulltextKeys
}

// indexOnlyVisibilityChange checks whether the change on an index is only
Expand Down
25 changes: 25 additions & 0 deletions go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestCreateTableDiff(t *testing.T) {
errorMsg string
autoinc int
rotation int
fulltext int
colrename int
constraint int
}{
Expand Down Expand Up @@ -422,6 +423,29 @@ func TestCreateTableDiff(t *testing.T) {
diff: "alter table t1 alter index i_idx invisible",
cdiff: "ALTER TABLE `t1` ALTER INDEX `i_idx` INVISIBLE",
},
// FULLTEXT keys
{
name: "add one fulltext key",
from: "create table t1 (id int primary key, name tinytext not null)",
to: "create table t1 (id int primary key, name tinytext not null, fulltext key name_ft(name))",
diff: "alter table t1 add fulltext key name_ft (`name`)",
cdiff: "ALTER TABLE `t1` ADD FULLTEXT KEY `name_ft` (`name`)",
},
{
name: "add two fulltext keys, distinct statements",
from: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null)",
to: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null, fulltext key name1_ft(name1), fulltext key name2_ft(name2))",
diffs: []string{"alter table t1 add fulltext key name1_ft (name1)", "alter table t1 add fulltext key name2_ft (name2)"},
cdiffs: []string{"ALTER TABLE `t1` ADD FULLTEXT KEY `name1_ft` (`name1`)", "ALTER TABLE `t1` ADD FULLTEXT KEY `name2_ft` (`name2`)"},
},
{
name: "add two fulltext keys, unify statements",
from: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null)",
to: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null, fulltext key name1_ft(name1), fulltext key name2_ft(name2))",
fulltext: FullTextKeyUnifyStatements,
diff: "alter table t1 add fulltext key name1_ft (name1), add fulltext key name2_ft (name2)",
cdiff: "ALTER TABLE `t1` ADD FULLTEXT KEY `name1_ft` (`name1`), ADD FULLTEXT KEY `name2_ft` (`name2`)",
},
// CHECK constraints
{
name: "identical check constraints",
Expand Down Expand Up @@ -981,6 +1005,7 @@ func TestCreateTableDiff(t *testing.T) {
hints.RangeRotationStrategy = ts.rotation
hints.ConstraintNamesStrategy = ts.constraint
hints.ColumnRenameStrategy = ts.colrename
hints.FullTextKeyStrategy = ts.fulltext
alter, err := c.Diff(other, &hints)

require.Equal(t, len(ts.diffs), len(ts.cdiffs))
Expand Down
6 changes: 6 additions & 0 deletions go/vt/schemadiff/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ const (
TableRenameHeuristicStatement
)

const (
FullTextKeyDistinctStatements = iota
FullTextKeyUnifyStatements
)

// DiffHints is an assortment of rules for diffing entities
type DiffHints struct {
StrictIndexOrdering bool
Expand All @@ -89,4 +94,5 @@ type DiffHints struct {
ConstraintNamesStrategy int
ColumnRenameStrategy int
TableRenameStrategy int
FullTextKeyStrategy int
}