Skip to content

Commit

Permalink
schemadiff: FullTextKeyStrategy, handling multiple 'ADD FULLTEXT key'…
Browse files Browse the repository at this point in the history
… alter options (vitessio#11012) (vitessio#961)

* schemadiff: FullTextKeyStrategy, handling multiple 'ADD FULLTEXT key' options

Signed-off-by: Shlomi Noach <[email protected]>

* removed debug info

Signed-off-by: Shlomi Noach <[email protected]>

Signed-off-by: Shlomi Noach <[email protected]>

Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach authored Aug 16, 2022
1 parent 125f67e commit 2817223
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
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
}

0 comments on commit 2817223

Please sign in to comment.