Skip to content

Commit

Permalink
fix: output origin col charset and collate; fix bug; add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
newborn22 committed Dec 3, 2024
1 parent 0885934 commit cfdb2fb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
5 changes: 4 additions & 1 deletion go/vt/schemadiff/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *ColumnDefinitionEntity) ColumnDiff(other *ColumnDefinitionEntity, hints
return nil, nil
}

return NewModifyColumnDiffByDefinition(otherClone.ColumnDefinition), nil
return NewModifyColumnDiffByDefinition(other.ColumnDefinition), nil
}

// IsTextual returns true when this column is of textual type, and is capable of having a character set property
Expand Down Expand Up @@ -149,6 +149,9 @@ func (c *ColumnDefinitionEntity) SetExplicitCharsetCollate() error {
if c.ColumnDefinition.Type.Charset.Name == "" && c.ColumnDefinition.Type.Options.Collate != "" {
// Column has explicit collation but no charset. We can infer the charset from the collation.
collationID := collationEnv.LookupByName(c.ColumnDefinition.Type.Options.Collate)
if collationID == nil {
return fmt.Errorf("unable to determine charset for column %s with collation %s", c.ColumnDefinition.Name, c.ColumnDefinition.Type.Options.Collate)
}
charset := collationEnv.LookupCharsetName(collationID.ID())
if charset == "" {
return fmt.Errorf("unable to determine charset for column %s with collation %s", c.ColumnDefinition.Name, collationID.Name())
Expand Down
97 changes: 73 additions & 24 deletions go/vt/schemadiff/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,51 @@ func TestColumnCharset(t *testing.T) {
}{
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL COLLATE utf8mb4_0900_ai_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL COLLATE utf8mb4_0900_ai_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateStrict},
"",
},
// todo enhancement?: it's a complex case, in this case, name should keep utf8mb4_0900_ai_ci. but we deal with table and column charset collate separately now,
// so it's hard to be resolved.
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL COLLATE utf8mb4_0900_ai_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateStrict},
"ALTER TABLE `b2` COLLATE utf8mb4_general_ci",
},
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateStrict},
"ALTER TABLE `b2` MODIFY COLUMN `name` varchar(255) NOT NULL, COLLATE utf8mb4_general_ci",
},
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
Expand All @@ -39,56 +69,75 @@ func TestColumnCharset(t *testing.T) {

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateIgnoreAlways},
"ALTER TABLE `b2` COLLATE utf8mb4_general_ci",
},
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL COLLATE utf8mb4_general_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB `,

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL COLLATE utf8mb4_0900_ai_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_generic_ai_ci `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateStrict},
"ALTER TABLE `b2` COLLATE utf8mb4_generic_ai_ci",
) ENGINE=InnoDB `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateIgnoreAlways},
"",
},
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
name varchar(255) NOT NULL COLLATE utf8mb4_general_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,
) ENGINE=InnoDB `,

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
name varchar(255) NOT NULL COLLATE utf8mb4_0900_ai_ci,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_generic_ai_ci `,
) ENGINE=InnoDB `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateStrict},
"ALTER TABLE `b2` MODIFY COLUMN `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_generic_ai_ci NOT NULL, COLLATE utf8mb4_generic_ai_ci",
"ALTER TABLE `b2` MODIFY COLUMN `name` varchar(255) COLLATE utf8mb4_0900_ai_ci NOT NULL",
},
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
age1 int NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci `,
) ENGINE=InnoDB `,

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
age2 int NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_generic_ai_ci `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateIgnoreAlways},
"ALTER TABLE `b2` COLLATE utf8mb4_generic_ai_ci",
) ENGINE=InnoDB `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateStrict},
"ALTER TABLE `b2` DROP COLUMN `age1`, ADD COLUMN `age2` int NOT NULL",
},
{
`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL COLLATE utf8mb4_generic_ai_ci,
name varchar(255) NOT NULL,
age1 int NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB `,

`CREATE TABLE b2 (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL COLLATE utf8mb4_0900_ai_ci,
name varchar(255) NOT NULL,
age2 int NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB `,
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateIgnoreAlways},
"",
&DiffHints{TableCharsetCollateStrategy: TableCharsetCollateStrict, ColumnCharsetCollateStrategy: ColumnCharsetCollateStrict, ColumnRenameStrategy: ColumnRenameHeuristicStatement},
"ALTER TABLE `b2` RENAME COLUMN `age1` TO `age2`",
},
}

Expand Down

0 comments on commit cfdb2fb

Please sign in to comment.