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: TableCharsetCollateStrategy hint #12137

Merged
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
24 changes: 24 additions & 0 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,12 @@ func (c *CreateTableEntity) diffOptions(alterTable *sqlparser.AlterTable,
// skip
case "AVG_ROW_LENGTH":
// skip. MyISAM only, not interesting
case "CHARSET":
switch hints.TableCharsetCollateStrategy {
case TableCharsetCollateStrict:
tableOption = &sqlparser.TableOption{String: ""}
// in all other strategies we ignore the charset
}
case "CHECKSUM":
tableOption = &sqlparser.TableOption{Value: sqlparser.NewIntLiteral("0")}
case "COLLATE":
Expand Down Expand Up @@ -930,6 +936,18 @@ func (c *CreateTableEntity) diffOptions(alterTable *sqlparser.AlterTable,
// options are different.
// However, we don't automatically apply these changes. It depends on the option!
switch strings.ToUpper(t1Option.Name) {
case "CHARSET", "COLLATE":
switch hints.TableCharsetCollateStrategy {
case TableCharsetCollateStrict:
alterTableOptions = append(alterTableOptions, t2Option)
case TableCharsetCollateIgnoreEmpty:
if t1Option.String != "" && t2Option.String != "" {
alterTableOptions = append(alterTableOptions, t2Option)
}
// if one is empty, we ignore
case TableCharsetCollateIgnoreAlways:
// ignore always
}
case "AUTO_INCREMENT":
switch hints.AutoIncrementStrategy {
case AutoIncrementApplyAlways:
Expand Down Expand Up @@ -961,6 +979,12 @@ func (c *CreateTableEntity) diffOptions(alterTable *sqlparser.AlterTable,
for _, t2Option := range t2Options {
if _, ok := t1OptionsMap[t2Option.Name]; !ok {
switch strings.ToUpper(t2Option.Name) {
case "CHARSET", "COLLATE":
switch hints.TableCharsetCollateStrategy {
case TableCharsetCollateStrict:
alterTableOptions = append(alterTableOptions, t2Option)
// in all other strategies we ignore the charset
}
case "AUTO_INCREMENT":
switch hints.AutoIncrementStrategy {
case AutoIncrementApplyAlways, AutoIncrementApplyHigher:
Expand Down
54 changes: 53 additions & 1 deletion go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestCreateTableDiff(t *testing.T) {
fulltext int
colrename int
constraint int
charset int
}{
{
name: "identical",
Expand Down Expand Up @@ -972,7 +973,57 @@ func TestCreateTableDiff(t *testing.T) {
cdiff: "ALTER TABLE `t1` AUTO_INCREMENT 100",
},
{
name: `change table charset`,
name: "apply table charset",
from: "create table t (id int, primary key(id))",
to: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8mb4",
diff: "alter table t charset utf8mb4",
cdiff: "ALTER TABLE `t` CHARSET utf8mb4",
},
{
name: "ignore empty table charset",
from: "create table t (id int, primary key(id))",
to: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8mb4",
charset: TableCharsetCollateIgnoreEmpty,
},
{
name: "ignore empty table charset and collate",
from: "create table t (id int, primary key(id))",
to: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_0900_ai_ci",
charset: TableCharsetCollateIgnoreEmpty,
},
{
name: "ignore empty table collate",
from: "create table t (id int, primary key(id))",
to: "create table t (id int, primary key(id)) COLLATE utf8mb4_0900_ai_ci",
charset: TableCharsetCollateIgnoreEmpty,
},
{
name: "ignore empty table charset and collate in target",
from: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_0900_ai_ci",
to: "create table t (id int, primary key(id))",
charset: TableCharsetCollateIgnoreEmpty,
},
{
name: "ignore dropped collate",
from: "create table t (id int, primary key(id)) COLLATE utf8mb4_0900_ai_ci",
to: "create table t (id int, primary key(id))",
charset: TableCharsetCollateIgnoreEmpty,
},
{
name: "ignore table charset",
from: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8",
to: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8mb4",
charset: TableCharsetCollateIgnoreAlways,
},
{
name: "change table charset",
from: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8",
to: "create table t (id int, primary key(id)) DEFAULT CHARSET = utf8mb4",
diff: "alter table t charset utf8mb4",
cdiff: "ALTER TABLE `t` CHARSET utf8mb4",
},
{
name: `change table charset and columns`,
from: "create table t (id int primary key, t1 varchar(128) default null, t2 varchar(128) not null, t3 tinytext charset latin1, t4 tinytext charset latin1) default charset=utf8",
to: "create table t (id int primary key, t1 varchar(128) not null, t2 varchar(128) not null, t3 tinytext, t4 tinytext charset latin1) default charset=utf8mb4",
diff: "alter table t modify column t1 varchar(128) not null, modify column t2 varchar(128) not null, modify column t3 tinytext, charset utf8mb4",
Expand Down Expand Up @@ -1059,6 +1110,7 @@ func TestCreateTableDiff(t *testing.T) {
hints.ConstraintNamesStrategy = ts.constraint
hints.ColumnRenameStrategy = ts.colrename
hints.FullTextKeyStrategy = ts.fulltext
hints.TableCharsetCollateStrategy = ts.charset
alter, err := c.Diff(other, &hints)

require.Equal(t, len(ts.diffs), len(ts.cdiffs))
Expand Down
21 changes: 14 additions & 7 deletions go/vt/schemadiff/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,20 @@ const (
FullTextKeyUnifyStatements
)

const (
TableCharsetCollateStrict int = iota
TableCharsetCollateIgnoreEmpty
TableCharsetCollateIgnoreAlways
)

// DiffHints is an assortment of rules for diffing entities
type DiffHints struct {
StrictIndexOrdering bool
AutoIncrementStrategy int
RangeRotationStrategy int
ConstraintNamesStrategy int
ColumnRenameStrategy int
TableRenameStrategy int
FullTextKeyStrategy int
StrictIndexOrdering bool
AutoIncrementStrategy int
RangeRotationStrategy int
ConstraintNamesStrategy int
ColumnRenameStrategy int
TableRenameStrategy int
FullTextKeyStrategy int
TableCharsetCollateStrategy int
}