diff --git a/pkg/parser/common.go b/pkg/parser/common.go index 94a268bfec..9128fa7d0c 100644 --- a/pkg/parser/common.go +++ b/pkg/parser/common.go @@ -271,6 +271,27 @@ func SplitDDL(stmt ast.StmtNode, schema string) (sqls []string, err error) { v.Specs = []*ast.AlterTableSpec{spec} + // handle `alter table t1 add column (c1 int, c2 int)` + if spec.Tp == ast.AlterTableAddColumns && len(spec.NewColumns) > 1 { + columns := spec.NewColumns + spec.Position = &ast.ColumnPosition{ + Tp: ast.ColumnPositionNone, // otherwise restore will become "alter table t1 add column (c1 int)" + } + for _, c := range columns { + spec.NewColumns = []*ast.ColumnDef{c} + bf.Reset() + err = stmt.Restore(ctx) + if err != nil { + v.Specs = specs + v.Table = table + return nil, terror.ErrRestoreASTNode.Delegate(err) + } + sqls = append(sqls, bf.String()) + } + // we have restore SQL for every columns, skip below general restoring and continue on next spec + continue + } + bf.Reset() err = stmt.Restore(ctx) if err != nil { diff --git a/pkg/parser/common_test.go b/pkg/parser/common_test.go index d80f6dcf91..de85c2a753 100644 --- a/pkg/parser/common_test.go +++ b/pkg/parser/common_test.go @@ -67,6 +67,7 @@ var sqls = []string{ "alter table `t1` partition by list (a) (partition x default)", "alter table `t1` partition by system_time (partition x history, partition y current)", "alter database `test` charset utf8mb4", + "alter table `t1` add column (c1 int, c2 int)", } var nonDDLs = []string{ @@ -171,6 +172,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) { {"ALTER TABLE `test`.`t1` PARTITION BY LIST (`a`) (PARTITION `x` DEFAULT)"}, {"ALTER TABLE `test`.`t1` PARTITION BY SYSTEM_TIME (PARTITION `x` HISTORY,PARTITION `y` CURRENT)"}, {"ALTER DATABASE `test` CHARACTER SET = utf8mb4"}, + {"ALTER TABLE `test`.`t1` ADD COLUMN `c1` INT", "ALTER TABLE `test`.`t1` ADD COLUMN `c2` INT"}, } expectedTableName := [][][]*filter.Table{ @@ -215,6 +217,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) { {{genTableName("test", "t1")}}, {{genTableName("test", "t1")}}, {{genTableName("test", "")}}, + {{genTableName("test", "t1")}, {genTableName("test", "t1")}}, } targetTableNames := [][][]*filter.Table{ @@ -259,6 +262,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) { {{genTableName("xtest", "xt1")}}, {{genTableName("xtest", "xt1")}}, {{genTableName("xtest", "")}}, + {{genTableName("xtest", "t1")}, {genTableName("xtest", "t1")}}, } targetSQLs := [][]string{ @@ -303,6 +307,7 @@ func (t *testParserSuite) TestResolveDDL(c *C) { {"ALTER TABLE `xtest`.`xt1` PARTITION BY LIST (`a`) (PARTITION `x` DEFAULT)"}, {"ALTER TABLE `xtest`.`xt1` PARTITION BY SYSTEM_TIME (PARTITION `x` HISTORY,PARTITION `y` CURRENT)"}, {"ALTER DATABASE `xtest` CHARACTER SET = utf8mb4"}, + {"ALTER TABLE `xtest`.`t1` ADD COLUMN `c1` INT", "ALTER TABLE `xtest`.`t1` ADD COLUMN `c2` INT"}, } for i, sql := range sqls { diff --git a/tests/sequence_safe_mode/data/db1.increment.sql b/tests/sequence_safe_mode/data/db1.increment.sql index 1eec206bac..7b523569a1 100644 --- a/tests/sequence_safe_mode/data/db1.increment.sql +++ b/tests/sequence_safe_mode/data/db1.increment.sql @@ -1,15 +1,13 @@ use sequence_safe_mode_test; insert into t1 (uid, name) values (10003, 'Buenos Aires'); -alter table t1 add column age int; +alter table t1 add column (age int, level int); alter table t1 add index age(age); -alter table t1 add column level int; alter table t1 add index level(level); insert into t1 (uid, name, age) values (10005, 'Buenos Aires', 200); insert into t2 (uid, name) values (20005, 'Aureliano José'); insert into t1 (uid, name, age) values (10006, 'Buenos Aires', 200); -alter table t2 add column age int; +alter table t2 add column (age int, level int); alter table t2 add index age(age); -alter table t2 add column level int; alter table t2 add index level(level); insert into t1 (uid, name, age) values (10007, 'Buenos Aires', 300); insert into t2 (uid, name, age) values (20006, 'Colonel Aureliano Buendía', 301); diff --git a/tests/sequence_safe_mode/data/db2.increment.sql b/tests/sequence_safe_mode/data/db2.increment.sql index a64470de11..2c8de84fa9 100644 --- a/tests/sequence_safe_mode/data/db2.increment.sql +++ b/tests/sequence_safe_mode/data/db2.increment.sql @@ -1,13 +1,11 @@ use sequence_safe_mode_test; delete from t3 where name = 'Santa Sofía de la Piedad'; -alter table t2 add column age int; +alter table t2 add column (age int, level int); alter table t2 add index age(age); -alter table t2 add column level int; alter table t2 add index level(level); insert into t2 (uid, name, age) values (40002, 'Remedios Moscote', 100), (40003, 'Amaranta', 103); insert into t3 (uid, name) values (30004, 'Aureliano José'), (30005, 'Santa Sofía de la Piedad'), (30006, '17 Aurelianos'); -alter table t3 add column age int; +alter table t3 add column (age int, level int); alter table t3 add index age(age); -alter table t3 add column level int; alter table t3 add index level(level); update t3 set age = 1;