提交 6c47c870 编写于 作者: L liipx

fix -cleanup-test-database with out query

上级 963eb4b5
......@@ -83,6 +83,7 @@ func main() {
// 使用 -cleanup-test-database 命令手动清理残余的 optimizer_xxx 数据库
if common.Config.CleanupTestDatabase {
vEnv.CleanupTestDatabase()
return
}
// 如果使用到测试环境,在这里环境清理
......
......@@ -174,10 +174,10 @@ func startSampling(conn, localConn mysql.Conn, database, table string, factor fl
// str = ""
case []byte:
// 先尝试转成数字,如果报错则转换成string
v, err := row.Int64Err(i)
values[i] = strconv.FormatInt(v, 10)
if err != nil {
if v, err := row.Int64Err(i); err != nil {
values[i] = string(data)
} else {
values[i] = strconv.FormatInt(v, 10)
}
case time.Time:
values[i] = mysql.TimeString(data)
......
......@@ -153,37 +153,42 @@ func (ve VirtualEnv) CleanUp() bool {
// CleanupTestDatabase 清除一小时前的环境
func (ve *VirtualEnv) CleanupTestDatabase() {
common.Log.Debug("CleanupTestDatabase ...")
dbs, err := ve.Query("show databases like 'optimizer%'")
if err == nil {
for _, row := range dbs.Rows {
testDatabase := row.Str(0)
// test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)`
if len(testDatabase) != 39 {
common.Log.Debug("CleanupTestDatabase by pass %s", testDatabase)
continue
}
s := strings.Split(testDatabase, "_")
pastTime, err := time.Parse("060102150405", s[1])
if err != nil {
common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error())
dbs, err := ve.Query("show databases like 'optimizer%%'")
if err != nil {
common.Log.Error("CleanupTestDatabase failed Error:%s", err.Error())
return
}
// TODO: 1 hour should be config-able
minHour := 1
for _, row := range dbs.Rows {
testDatabase := row.Str(0)
// test temporary database format `optimizer_YYMMDDHHmmss_randomString(16)`
if len(testDatabase) != 39 {
common.Log.Debug("CleanupTestDatabase by pass %s", testDatabase)
continue
}
s := strings.Split(testDatabase, "_")
pastTime, err := time.Parse("060102150405", s[1])
if err != nil {
common.Log.Error("CleanupTestDatabase compute pastTime Error: %s", err.Error())
continue
}
subHour := time.Now().Sub(pastTime).Hours()
if subHour > float64(minHour) {
if _, err := ve.Query("drop database %s", testDatabase); err != nil {
common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error())
continue
}
// TODO: 1 hour should be config-able
subHour := time.Now().Sub(pastTime).Hours()
if subHour > 1 {
_, err := ve.Query("drop database %s", testDatabase)
if err != nil {
common.Log.Error("CleanupTestDatabase failed Error: %s", err.Error())
continue
}
common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase)
} else {
common.Log.Debug("CleanupTestDatabase by pass database %s, less than %d hours", testDatabase, subHour)
}
common.Log.Debug("CleanupTestDatabase drop database %s success", testDatabase)
continue
}
} else {
common.Log.Error("CleanupTestDatabase failed Error:%s", err.Error())
common.Log.Debug("CleanupTestDatabase by pass database %s, %.2f less than %d hours", testDatabase, subHour, minHour)
}
common.Log.Debug("CleanupTestDatabase done")
return
}
// BuildVirtualEnv rEnv为SQL源环境,DB使用的信息从接口获取
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册