Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(sbm): alter table option if options not match (#2947)

h3n4l0999e9356a87
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index d5ef5dde32..85b8856826 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -45,9 +45,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
if err != nil {
return "", errors.Wrapf(err, "failed to parse new statement %q", newStmt)
}
-
var diff []ast.Node
-
oldTableMap := buildTableMap(oldNodes)
for _, node := range newNodes {
@@ -61,6 +59,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
diff = append(diff, &stmt)
continue
}
+ if alterTableOptionStmt := diffTableOptions(newStmt.Table, oldStmt.Options, newStmt.Options); alterTableOptionStmt != nil {
+ diff = append(diff, alterTableOptionStmt)
+ }
indexMap := buildIndexMap(oldStmt)
var alterTableAddColumnSpecs []*ast.AlterTableSpec
var alterTableModifyColumnSpecs []*ast.AlterTableSpec
@@ -149,20 +150,20 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
}
}
- deparse := func(nodes []ast.Node) (string, error) {
- restoreFlags := format.DefaultRestoreFlags | format.RestoreStringWithoutCharset
- var buf bytes.Buffer
- for _, node := range nodes {
- if err := node.Restore(format.NewRestoreCtx(restoreFlags, &buf)); err != nil {
- return "", err
- }
- if _, err := buf.Write([]byte(";\n")); err != nil {
- return "", err
- }
+ return deparse(diff, format.DefaultRestoreFlags|format.RestoreStringWithoutCharset)
+}
+
+func deparse(nodes []ast.Node, flag format.RestoreFlags) (string, error) {
+ var buf bytes.Buffer
+ for _, node := range nodes {
+ if err := node.Restore(format.NewRestoreCtx(flag, &buf)); err != nil {
+ return "", err
+ }
+ if _, err := buf.Write([]byte(";\n")); err != nil {
+ return "", err
}
- return buf.String(), nil
}
- return deparse(diff)
+ return buf.String(), nil
}
// buildTableMap returns a map of table name to create table statements.
@@ -383,3 +384,297 @@ func getIndexName(index *ast.Constraint) string {
// TODO(zp): handle the duplicated index name.
return index.Keys[0].Column.Name.O
}
+
+// diffTableOptions returns the diff of two table options, returns nil if they are the same.
+func diffTableOptions(tableName *ast.TableName, old, new []*ast.TableOption) *ast.AlterTableStmt {
+ // https://dev.mysql.com/doc/refman/8.0/en/create-table.html
+ // table_option: {
+ // AUTOEXTEND_SIZE [=] value
+ // | AUTO_INCREMENT [=] value
+ // | AVG_ROW_LENGTH [=] value
+ // | [DEFAULT] CHARACTER SET [=] charset_name
+ // | CHECKSUM [=] {0 | 1}
+ // | [DEFAULT] COLLATE [=] collation_name
+ // | COMMENT [=] 'string'
+ // | COMPRESSION [=] {'ZLIB' | 'LZ4' | 'NONE'}
+ // | CONNECTION [=] 'connect_string'
+ // | {DATA | INDEX} DIRECTORY [=] 'absolute path to directory'
+ // | DELAY_KEY_WRITE [=] {0 | 1}
+ // | ENCRYPTION [=] {'Y' | 'N'}
+ // | ENGINE [=] engine_name
+ // | ENGINE_ATTRIBUTE [=] 'string'
+ // | INSERT_METHOD [=] { NO | FIRST | LAST }
+ // | KEY_BLOCK_SIZE [=] value
+ // | MAX_ROWS [=] value
+ // | MIN_ROWS [=] value
+ // | PACK_KEYS [=] {0 | 1 | DEFAULT}
+ // | PASSWORD [=] 'string'
+ // | ROW_FORMAT [=] {DEFAULT | DYNAMIC | FIXED | COMPRESSED | REDUNDANT | COMPACT}
+ // | START TRANSACTION // This is an internal-use table option.
+ // // It was introduced in MySQL 8.0.21 to permit CREATE TABLE ... SELECT to be logged as a single,
+ // // atomic transaction in the binary log when using row-based replication with a storage engine that supports atomic DDL.
+ // | SECONDARY_ENGINE_ATTRIBUTE [=] 'string'
+ // | STATS_AUTO_RECALC [=] {DEFAULT | 0 | 1}
+ // | STATS_PERSISTENT [=] {DEFAULT | 0 | 1}
+ // | STATS_SAMPLE_PAGES [=] value
+ // | TABLESPACE tablespace_name [STORAGE {DISK | MEMORY}]
+ // | UNION [=] (tbl_name[,tbl_name]...)
+ // }
+
+ // We use map to record the table options, so we can easily find the difference.
+ oldOptionsMap := buildTableOptionMap(old)
+ newOptionsMap := buildTableOptionMap(new)
+
+ var options []*ast.TableOption
+ for oldTp, oldOption := range oldOptionsMap {
+ newOption, ok := newOptionsMap[oldTp]
+ if !ok {
+ // We should drop the table option if it doesn't exist in the new table options.
+ if astOption := dropTableOption(oldOption); astOption != nil {
+ options = append(options, astOption)
+ }
+ continue
+ }
+ if !isTableOptionValEqual(oldOption, newOption) {
+ options = append(options, newOption)
+ }
+ }
+ // We should add the table option if it doesn't exist in the old table options.
+ for newTp, newOption := range newOptionsMap {
+ if _, ok := oldOptionsMap[newTp]; !ok {
+ // We should add the table option if it doesn't exist in the old table options.
+ options = append(options, newOption)
+ }
+ }
+ if len(options) == 0 {
+ return nil
+ }
+ return &ast.AlterTableStmt{
+ Table: tableName,
+ Specs: []*ast.AlterTableSpec{
+ {
+ Tp: ast.AlterTableOption,
+ Options: options,
+ },
+ },
+ }
+}
+
+// dropTableOption generate the table options node need to oppended to the ALTER TABLE OPTION spec.
+func dropTableOption(option *ast.TableOption) *ast.TableOption {
+ switch option.Tp {
+ case ast.TableOptionAutoIncrement:
+ // You cannot reset the counter to a value less than or equal to the value that is currently in use.
+ // For both InnoDB and MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column,
+ // the value is reset to the current maximum AUTO_INCREMENT column value plus one.
+ // https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
+ // So we always set the auto_increment value to 0, it will be reset to the current maximum AUTO_INCREMENT column value plus one.
+ return &ast.TableOption{
+ Tp: ast.TableOptionAutoIncrement,
+ UintValue: 0,
+ }
+ case ast.TableOptionAvgRowLength:
+ // AVG_ROW_LENGTH only works in MyISAM tables.
+ return &ast.TableOption{
+ Tp: ast.TableOptionAvgRowLength,
+ UintValue: 0,
+ }
+ case ast.TableOptionCharset:
+ // TODO(zp): we use utf8mb4 as the default charset, but it's not always true. We should consider the database default charset.
+ return &ast.TableOption{
+ Tp: ast.TableOptionCharset,
+ StrValue: "utf8mb4",
+ }
+ case ast.TableOptionCollate:
+ // TODO(zp): default collate is related with the charset.
+ return &ast.TableOption{
+ Tp: ast.TableOptionCollate,
+ StrValue: "utf8mb4_general_ci",
+ }
+ case ast.TableOptionCheckSum:
+ return &ast.TableOption{
+ Tp: ast.TableOptionCheckSum,
+ UintValue: 0,
+ }
+ case ast.TableOptionComment:
+ // Set to "" to remove the comment.
+ return &ast.TableOption{
+ Tp: ast.TableOptionComment,
+ }
+ case ast.TableOptionCompression:
+ // TODO(zp): handle the compression
+ case ast.TableOptionConnection:
+ // Set to "" to remove the connection.
+ return &ast.TableOption{
+ Tp: ast.TableOptionConnection,
+ }
+ case ast.TableOptionDataDirectory:
+ case ast.TableOptionIndexDirectory:
+ // TODO(zp): handle the default data directory and index directory, there are a lot of situations we need to consider.
+ // 1. Data Directory and Index Directory will be ignored on Windows.
+ // 2. For a normal table, data directory and index directory cannot changed after the table is created, but for a partition table, it can be changed by USING `alter add PARTITIONS data DIRECTORY ...`
+ // 3. We should know the default data directory like /var/mysql/data, and the default index directory like /var/mysql/index.
+ case ast.TableOptionDelayKeyWrite:
+ return &ast.TableOption{
+ Tp: ast.TableOptionDelayKeyWrite,
+ UintValue: 0,
+ }
+ case ast.TableOptionEncryption:
+ return &ast.TableOption{
+ Tp: ast.TableOptionEncryption,
+ StrValue: "N",
+ }
+ case ast.TableOptionEngine:
+ // TODO(zp): handle the default engine
+ case ast.TableOptionInsertMethod:
+ // INSERT METHOD only support in MERGE storage engine.
+ // https://dev.mysql.com/doc/refman/8.0/en/create-table.html
+ return &ast.TableOption{
+ Tp: ast.TableOptionInsertMethod,
+ StrValue: "NO",
+ }
+ case ast.TableOptionKeyBlockSize:
+ // TODO(zp): InnoDB doesn't support this. And the default value will be ensured when compiling.
+ case ast.TableOptionMaxRows:
+ return &ast.TableOption{
+ Tp: ast.TableOptionMaxRows,
+ UintValue: 0,
+ }
+ case ast.TableOptionMinRows:
+ return &ast.TableOption{
+ Tp: ast.TableOptionMinRows,
+ UintValue: 0,
+ }
+ case ast.TableOptionPackKeys:
+ // TiDB doesn't support this, and the restore will always write "DEFAULT".
+ return &ast.TableOption{
+ Tp: ast.TableOptionPackKeys,
+ }
+ case ast.TableOptionPassword:
+ // mysqldump will not dump the password, but we handle it to pass the test.
+ return &ast.TableOption{
+ Tp: ast.TableOptionPassword,
+ }
+ case ast.TableOptionRowFormat:
+ return &ast.TableOption{
+ Tp: ast.TableOptionRowFormat,
+ UintValue: ast.RowFormatDefault,
+ }
+ case ast.TableOptionStatsAutoRecalc:
+ return &ast.TableOption{
+ Tp: ast.TableOptionStatsAutoRecalc,
+ Default: true,
+ }
+ case ast.TableOptionStatsPersistent:
+ // TiDB doesn't support this, and the restore will always write "DEFAULT".
+ return &ast.TableOption{
+ Tp: ast.TableOptionStatsPersistent,
+ }
+ case ast.TableOptionStatsSamplePages:
+ return &ast.TableOption{
+ Tp: ast.TableOptionStatsSamplePages,
+ Default: true,
+ }
+ case ast.TableOptionTablespace:
+ // TODO(zp): handle the table space
+ case ast.TableOptionUnion:
+ // TODO(zp): handle the union
+ }
+ return nil
+}
+
+// isTableOptionValEqual compare the two table options value, if they are equal, returns true.
+// Caller need to ensure the two table options are not nil and the type is the same.
+func isTableOptionValEqual(old, new *ast.TableOption) bool {
+ switch old.Tp {
+ case ast.TableOptionAutoIncrement:
+ // You cannot reset the counter to a value less than or equal to the value that is currently in use.
+ // For both InnoDB and MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column,
+ // the value is reset to the current maximum AUTO_INCREMENT column value plus one.
+ // https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
+ return old.UintValue == new.UintValue
+ case ast.TableOptionAvgRowLength:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionCharset:
+ if old.Default != new.Default {
+ return false
+ }
+ if old.Default && new.Default {
+ return true
+ }
+ return old.StrValue == new.StrValue
+ case ast.TableOptionCollate:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionCheckSum:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionComment:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionCompression:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionConnection:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionDataDirectory:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionIndexDirectory:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionDelayKeyWrite:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionEncryption:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionEngine:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionInsertMethod:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionKeyBlockSize:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionMaxRows:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionMinRows:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionPackKeys:
+ // TiDB doesn't support this, and the restore will always write "DEFAULT".
+ // The parser will ignore it. So we can only ignore it here.
+ // https://github.com/pingcap/tidb/blob/master/parser/parser.y#L11661
+ return old.Default == new.Default
+ case ast.TableOptionPassword:
+ // mysqldump will not dump the password, so we just return false here.
+ return false
+ case ast.TableOptionRowFormat:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionStatsAutoRecalc:
+ // TiDB parser will ignore the DEFAULT value. So we just can compare the UINT value here.
+ // https://github.com/pingcap/tidb/blob/master/parser/parser.y#L11599
+ return old.UintValue == new.UintValue
+ case ast.TableOptionStatsPersistent:
+ // TiDB parser doesn't support this, it only assign the type without any value.
+ // https://github.com/pingcap/tidb/blob/master/parser/parser.y#L11595
+ return true
+ case ast.TableOptionStatsSamplePages:
+ return old.UintValue == new.UintValue
+ case ast.TableOptionTablespace:
+ return old.StrValue == new.StrValue
+ case ast.TableOptionUnion:
+ oldTableNames := old.TableNames
+ newTableNames := new.TableNames
+ if len(oldTableNames) != len(newTableNames) {
+ return false
+ }
+ for i, oldTableName := range oldTableNames {
+ newTableName := newTableNames[i]
+ if oldTableName.Name.O != newTableName.Name.O {
+ return false
+ }
+ }
+ return true
+ }
+ return true
+}
+
+// buildTableOptionMap builds a map of table options.
+func buildTableOptionMap(options []*ast.TableOption) map[ast.TableOptionType]*ast.TableOption {
+ m := make(map[ast.TableOptionType]*ast.TableOption)
+ for _, option := range options {
+ m[option.Tp] = option
+ }
+ return m
+}
diff --git a/plugin/parser/differ/mysql/table_test.go b/plugin/parser/differ/mysql/table_test.go
index 8f03f41d34..0c68cae23e 100644
--- a/plugin/parser/differ/mysql/table_test.go
+++ b/plugin/parser/differ/mysql/table_test.go
@@ -44,3 +44,229 @@ func TestTable(t *testing.T) {
a.Equal(test.want, out)
}
}
+
+func TestTableOption(t *testing.T) {
+ tests := []struct {
+ old string
+ new string
+ want string
+ }{
+ // AUTO_INCREMENT
+ {
+ old: `CREATE TABLE book(id INT PRIMARY KEY AUTO_INCREMENT) AUTO_INCREMENT = 4;`,
+ new: `CREATE TABLE book(id INT PRIMARY KEY AUTO_INCREMENT) AUTO_INCREMENT = 10;`,
+ want: "ALTER TABLE `book` AUTO_INCREMENT = 10;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT PRIMARY KEY AUTO_INCREMENT) AUTO_INCREMENT = 4;`,
+ new: `CREATE TABLE book(id INT PRIMARY KEY AUTO_INCREMENT);`,
+ want: "ALTER TABLE `book` AUTO_INCREMENT = 0;\n",
+ },
+ // AVG_ROW_LENGTH
+ {
+ old: `CREATE TABLE book(id INT) AVG_ROW_LENGTH = 1;`,
+ new: `CREATE TABLE book(id INT) AVG_ROW_LENGTH = 2;`,
+ want: "ALTER TABLE `book` AVG_ROW_LENGTH = 2;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) AVG_ROW_LENGTH = 1;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` AVG_ROW_LENGTH = 0;\n",
+ },
+ // DEFAULT CHARSET
+ {
+ old: `CREATE TABLE book(id INT) DEFAULT CHARACTER SET = utf8;`,
+ new: `CREATE TABLE book(id INT) DEFAULT CHARACTER SET = utf8mb4;`,
+ want: "ALTER TABLE `book` DEFAULT CHARACTER SET = UTF8MB4;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) DEFAULT CHARACTER SET = utf8;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` DEFAULT CHARACTER SET = UTF8MB4;\n",
+ },
+ // DEFAULT COLLATE
+ {
+ old: `CREATE TABLE book(id INT) DEFAULT COLLATE = latin1_swedish_ci;`,
+ new: `CREATE TABLE book(id INT) DEFAULT COLLATE = utf8mb4_general_ci;`,
+ want: "ALTER TABLE `book` DEFAULT COLLATE = UTF8MB4_GENERAL_CI;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) DEFAULT COLLATE = latin1_swedish_ci;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` DEFAULT COLLATE = UTF8MB4_GENERAL_CI;\n",
+ },
+ // CHECKSUM
+ {
+ old: `CREATE TABLE book(id INT) CHECKSUM = 1;`,
+ new: `CREATE TABLE book(id INT) CHECKSUM = 0;`,
+ want: "ALTER TABLE `book` CHECKSUM = 0;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) CHECKSUM = 1;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` CHECKSUM = 0;\n",
+ },
+ // COMMENT
+ {
+ old: `CREATE TABLE book(id INT) COMMENT = 'old';`,
+ new: `CREATE TABLE book(id INT) COMMENT = 'new';`,
+ want: "ALTER TABLE `book` COMMENT = 'new';\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) COMMENT = 'old';`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` COMMENT = '';\n",
+ },
+ // TODO(zp): handle drop COMPRESSION
+ {
+ old: `CREATE TABLE book(id INT) COMPRESSION = 'ZLIB';`,
+ new: `CREATE TABLE book(id INT) COMPRESSION = 'LZ4';`,
+ want: "ALTER TABLE `book` COMPRESSION = 'LZ4';\n",
+ },
+ // CONNECTION
+ {
+ old: `CREATE TABLE book(id INT) CONNECTION = 'old';`,
+ new: `CREATE TABLE book(id INT) CONNECTION = 'new';`,
+ want: "ALTER TABLE `book` CONNECTION = 'new';\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) CONNECTION = 'old';`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` CONNECTION = '';\n",
+ },
+ // TODO(zp): handle drop DATA DIRECTORY
+ {
+ old: `CREATE TABLE book(id INT) DATA DIRECTORY = 'old';`,
+ new: `CREATE TABLE book(id INT) DATA DIRECTORY = 'new';`,
+ want: "ALTER TABLE `book` DATA DIRECTORY = 'new';\n",
+ },
+ // TODO(zp): handle drop INDEX DIRECTORY
+ {
+ old: `CREATE TABLE book(id INT) INDEX DIRECTORY = 'old';`,
+ new: `CREATE TABLE book(id INT) INDEX DIRECTORY = 'new';`,
+ want: "ALTER TABLE `book` INDEX DIRECTORY = 'new';\n",
+ },
+ // DELAY_KEY_WRITE
+ {
+ old: `CREATE TABLE book(id INT) DELAY_KEY_WRITE = 1;`,
+ new: `CREATE TABLE book(id INT) DELAY_KEY_WRITE = 0;`,
+ want: "ALTER TABLE `book` DELAY_KEY_WRITE = 0;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) DELAY_KEY_WRITE = 1;`,
+ new: `CREATE TABLE book(id INT)`,
+ want: "ALTER TABLE `book` DELAY_KEY_WRITE = 0;\n",
+ },
+ // ENCRYPTION
+ {
+ old: `CREATE TABLE book(id INT) ENCRYPTION = 'Y';`,
+ new: `CREATE TABLE book(id INT) ENCRYPTION = 'N';`,
+ want: "ALTER TABLE `book` ENCRYPTION = 'N';\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) ENCRYPTION = 'Y';`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` ENCRYPTION = 'N';\n",
+ },
+ // INSERT_METHOD
+ // TODO(zp): enable this test if the upstream repo fix it.
+ // https://github.com/pingcap/tidb/pull/38355
+ // {
+ // old: `CREATE TABLE book(id INT) INSERT_METHOD = LAST;`,
+ // new: `CREATE TABLE book(id INT) INSERT_METHOD = FIRST;`,
+ // want: "ALTER TABLE `book` INSERT_METHOD = FIRST;\n",
+ // },
+ // {
+ // old: `CREATE TABLE book(id INT) INSERT_METHOD = LAST;`,
+ // new: `CREATE TABLE book(id INT);`,
+ // want: "ALTER TABLE `book` INSERT_METHOD = NO;\n",
+ // },
+ // TODO(zp): KEY_BLOCK_SIZE
+
+ // MAX_ROWS
+ {
+ old: `CREATE TABLE book(id INT) MAX_ROWS = 100;`,
+ new: `CREATE TABLE book(id INT) MAX_ROWS = 200;`,
+ want: "ALTER TABLE `book` MAX_ROWS = 200;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) MAX_ROWS = 100;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` MAX_ROWS = 0;\n",
+ },
+ // MIN_ROWS
+ {
+ old: `CREATE TABLE book(id INT) MIN_ROWS = 100;`,
+ new: `CREATE TABLE book(id INT) MIN_ROWS = 200;`,
+ want: "ALTER TABLE `book` MIN_ROWS = 200;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) MIN_ROWS = 100;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` MIN_ROWS = 0;\n",
+ },
+ // PACK_KEYS
+ // TODO(zp): alter table pack_keys
+ {
+ old: `CREATE TABLE book(id INT) PACK_KEYS = 1;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` PACK_KEYS = DEFAULT /* TableOptionPackKeys is not supported */ ;\n",
+ },
+ // PACK_KEYS
+ // TODO(zp): alter table pack_keys
+ {
+ old: `CREATE TABLE book(id INT) PACK_KEYS = 1;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` PACK_KEYS = DEFAULT /* TableOptionPackKeys is not supported */ ;\n",
+ },
+ // PASSWORD
+ {
+ old: `CREATE TABLE book(id INT) PASSWORD = 'old';`,
+ new: `CREATE TABLE book(id INT) PASSWORD = 'new';`,
+ want: "ALTER TABLE `book` PASSWORD = 'new';\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) PASSWORD = 'old';`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` PASSWORD = '';\n",
+ },
+ // ROW_FORMAT
+ {
+ old: `CREATE TABLE book(id INT) ROW_FORMAT = DYNAMIC;`,
+ new: `CREATE TABLE book(id INT) ROW_FORMAT = COMPRESSED;`,
+ want: "ALTER TABLE `book` ROW_FORMAT = COMPRESSED;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) ROW_FORMAT = DYNAMIC;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` ROW_FORMAT = DEFAULT;\n",
+ },
+ // STATS_AUTO_RECALC
+ {
+ old: `CREATE TABLE book(id INT) STATS_AUTO_RECALC = 1;`,
+ new: `CREATE TABLE book(id INT) STATS_AUTO_RECALC = 0;`,
+ want: "ALTER TABLE `book` STATS_AUTO_RECALC = 0;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT) STATS_AUTO_RECALC = 1;`,
+ new: `CREATE TABLE book(id INT);`,
+ want: "ALTER TABLE `book` STATS_AUTO_RECALC = DEFAULT;\n",
+ },
+ // TODO(zp): STATS_PERSISTENT
+
+ // UNION
+ {
+ old: `CREATE TABLE book(id INT) UNION = (book2);`,
+ new: `CREATE TABLE book(id INT) UNION = (book2, book3);`,
+ want: "ALTER TABLE `book` UNION = (`book2`,`book3`);\n",
+ },
+ }
+ t.Parallel()
+ a := require.New(t)
+ mysqlDiffer := &SchemaDiffer{}
+ for _, test := range tests {
+ out, err := mysqlDiffer.SchemaDiff(test.old, test.new)
+ a.NoError(err)
+ a.Equalf(test.want, out, "old: %s\nnew: %s\n", test.old, test.new)
+ }
+}