Skip to content

Commit

Permalink
update: 优化使用pt工具时的alter子句,并完善其测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchuanchuan committed Jan 8, 2020
1 parent ee02a03 commit 0b46574
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions session/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type Session interface {
// HaveBegin() bool
// HaveCommit() bool
// RecordSets() *MyRecordSets

// 用以测试
GetAlterTablePostPart(sql string, isPtOSC bool) string
}

var (
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions session/session_inception_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type testCommon struct {

remoteBackupTable string
parser *parser.Parser

session session.Session
}

func (s *testCommon) initSetUp(c *C) {
Expand Down Expand Up @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions session/session_inception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit 0b46574

Please sign in to comment.