From 0b46574a479fcf5a9bcc77be1766876e4080b52d Mon Sep 17 00:00:00 2001 From: hanchuanchuan Date: Wed, 8 Jan 2020 15:34:53 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=BC=98=E5=8C=96=E4=BD=BF=E7=94=A8p?= =?UTF-8?q?t=E5=B7=A5=E5=85=B7=E6=97=B6=E7=9A=84alter=E5=AD=90=E5=8F=A5,?= =?UTF-8?q?=E5=B9=B6=E5=AE=8C=E5=96=84=E5=85=B6=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- session/osc.go | 21 ++++---- session/session.go | 7 +++ session/session_inception_common_test.go | 4 ++ session/session_inception_test.go | 62 ++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index cfaed923..ec5465ea 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,7 @@ require ( github.com/dustin/go-humanize v1.0.0 // indirect github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect - github.com/etcd-io/gofail v0.0.0-20180808172546-51ce9a71510a // indirect + github.com/etcd-io/gofail v0.0.0-20180808172546-51ce9a71510a github.com/ghodss/yaml v1.0.0 // indirect github.com/github/gh-ost v1.0.48 github.com/go-sql-driver/mysql v1.4.1 diff --git a/session/osc.go b/session/osc.go index a0e1a2c0..562f2fcd 100644 --- a/session/osc.go +++ b/session/osc.go @@ -761,16 +761,17 @@ func (s *session) getAlterTablePostPart(sql string, isPtOSC bool) string { } // 解析后的语句长度不能小于解析前! - sqlParts := strings.Fields(sql) - if len(sqlParts) >= 4 { - newSql := strings.Join(sqlParts[3:], " ") - if len(part) < len(newSql) { - log.Errorf("origin sql: %s", sql) - log.Errorf("parsed after: %s", part) - s.AppendErrorMessage("alter子句解析失败,请联系作者或自行调整!") - return "" - } - } + // sqlParts := strings.Fields(sql) + // if len(sqlParts) >= 4 { + // newSql := strings.Join(sqlParts[3:], " ") + // if len(part) < len(newSql) && + // !strings.Contains(part, "UNIQUE") { + // log.Errorf("origin sql: %s", sql) + // log.Errorf("parsed after: %s", part) + // s.AppendErrorMessage("alter子句解析失败,请联系作者或自行调整!") + // return "" + // } + // } // gh-ost不需要处理`,pt-osc需要处理 if isPtOSC { diff --git a/session/session.go b/session/session.go index 73d973f4..70515628 100644 --- a/session/session.go +++ b/session/session.go @@ -98,6 +98,9 @@ type Session interface { // HaveBegin() bool // HaveCommit() bool // RecordSets() *MyRecordSets + + // 用以测试 + GetAlterTablePostPart(sql string, isPtOSC bool) string } var ( @@ -314,6 +317,10 @@ func (s *session) SetCollation(coID int) error { return nil } +func (s *session) GetAlterTablePostPart(sql string, isPtOSC bool) string { + return s.getAlterTablePostPart(sql, isPtOSC) +} + func (s *session) PreparedPlanCache() *kvcache.SimpleLRUCache { return s.preparedPlanCache } diff --git a/session/session_inception_common_test.go b/session/session_inception_common_test.go index 50ba6b03..2d14bcd9 100644 --- a/session/session_inception_common_test.go +++ b/session/session_inception_common_test.go @@ -68,6 +68,8 @@ type testCommon struct { remoteBackupTable string parser *parser.Parser + + session session.Session } func (s *testCommon) initSetUp(c *C) { @@ -105,6 +107,8 @@ func (s *testCommon) initSetUp(c *C) { server.InitOscProcessList() s.tk.Se.SetSessionManager(server) + s.session = s.tk.Se + cfg := config.GetGlobalConfig() _, localFile, _, _ := runtime.Caller(0) localFile = path.Dir(localFile) diff --git a/session/session_inception_test.go b/session/session_inception_test.go index 83572ff0..df9b07ea 100644 --- a/session/session_inception_test.go +++ b/session/session_inception_test.go @@ -2602,3 +2602,65 @@ func (s *testSessionIncSuite) TestNewRewrite(c *C) { c.Assert(newSql, Equals, row.countSql) } } + +func (s *testSessionIncSuite) TestGetAlterTablePostPart(c *C) { + sqls := []struct { + sql string + outPT string + outGhost string + }{ + { + "alter table tb_archery add unique index uniq_test_ghost_3 (test_ghost_3);", + "ADD UNIQUE \\`uniq_test_ghost_3\\`(\\`test_ghost_3\\`)", + "ADD UNIQUE `uniq_test_ghost_3`(`test_ghost_3`)", + }, + { + "alter table tb_archery add COLUMN c1 varchar(100) default null comment '!@#$%^&*(){}:<>?,./' after id123;", + "ADD COLUMN \\`c1\\` VARCHAR(100) DEFAULT NULL COMMENT '!@#\\$%^&*(){}:<>?,./' AFTER \\`id123\\`", + "ADD COLUMN `c1` VARCHAR(100) DEFAULT NULL COMMENT '!@#$%^&*(){}:<>?,./' AFTER `id123`", + }, + { + "alter table tb_archery add primary key(id);", + "ADD PRIMARY KEY(\\`id\\`)", + "ADD PRIMARY KEY(`id`)", + }, + { + "alter table tb_archery add unique key uniq_1(c1);", + "ADD UNIQUE \\`uniq_1\\`(\\`c1\\`)", + "ADD UNIQUE `uniq_1`(`c1`)", + }, + { + "alter table tb_archery alter column c1 drop default;", + "ALTER COLUMN \\`c1\\` DROP DEFAULT", + "ALTER COLUMN `c1` DROP DEFAULT", + }, + { + "alter table tb_archery default character set utf8 collate utf8_bin;", + "CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN", + "CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN", + }, + { + "alter table tb_archery collate = utf8_bin;", + "DEFAULT COLLATE = UTF8_BIN", + "DEFAULT COLLATE = UTF8_BIN", + }, + { + "alter table t1 modify c1 varchar(100) character set utf8 collate utf8_bin;", + "MODIFY COLUMN \\`c1\\` VARCHAR(100) CHARACTER SET UTF8 COLLATE utf8_bin", + "MODIFY COLUMN `c1` VARCHAR(100) CHARACTER SET UTF8 COLLATE utf8_bin", + }, + { + "alter table t1 modify column c1 varchar(100) collate utf8_bin;", + "MODIFY COLUMN \\`c1\\` VARCHAR(100) COLLATE utf8_bin", + "MODIFY COLUMN `c1` VARCHAR(100) COLLATE utf8_bin", + }, + } + + for _, row := range sqls { + out := s.session.GetAlterTablePostPart(row.sql, true) + c.Assert(out, Equals, row.outPT, Commentf("%v", row.sql)) + + out = s.session.GetAlterTablePostPart(row.sql, false) + c.Assert(out, Equals, row.outGhost, Commentf("%v", row.sql)) + } +}