diff --git a/database/sampling.go b/database/sampling.go index 5ccbf55e92d43df2e9dc6f03036b54328c9b0bc2..5d97142515a9c5953e5600ba5d20375f9c5e4008 100644 --- a/database/sampling.go +++ b/database/sampling.go @@ -17,12 +17,10 @@ package database import ( + "database/sql" "fmt" - "time" - "strings" - - "database/sql" + "time" "github.com/XiaoMi/soar/common" "github.com/ziutek/mymysql/mysql" @@ -92,7 +90,6 @@ func (db *Connector) SamplingData(onlineConn *Connector, database string, tables } else { where = common.Config.SamplingCondition } - err = db.startSampling(onlineConn.Conn, database, table, where) } return err @@ -143,17 +140,17 @@ func (db *Connector) startSampling(onlineConn *sql.DB, database, table string, w values = append(values, fmt.Sprintf(`unhex("%s")`, fmt.Sprintf("%x", val))) } } - } - valuesStr = append(valuesStr, "("+strings.Join(values, `,`)+")") - valuesCount++ - if maxValuesCount <= valuesCount { - err = db.doSampling(table, columnsStr, strings.Join(valuesStr, `,`)) - if err != nil { - break + valuesStr = append(valuesStr, "("+strings.Join(values, `,`)+")") + valuesCount++ + if maxValuesCount <= valuesCount { + err = db.doSampling(table, columnsStr, strings.Join(valuesStr, `,`)) + if err != nil { + break + } + values = make([]string, 0) + valuesStr = make([]string, 0) + valuesCount = 0 } - values = make([]string, 0) - valuesStr = make([]string, 0) - valuesCount = 0 } } res.Close() diff --git a/env/env_test.go b/env/env_test.go index fa3805a252ef4714ad5ffc4121fa4b4e4c8b9d67..a1da45f408ee5a423f3754f63e54ea7ed7476268 100644 --- a/env/env_test.go +++ b/env/env_test.go @@ -282,3 +282,60 @@ func TestCreateDatabase(t *testing.T) { } common.Log.Debug("Exiting function: %s", common.GetFunctionName()) } + +func TestCreateTable(t *testing.T) { + orgSamplingCondition := common.Config.SamplingCondition + common.Config.SamplingCondition = "LIMIT 1" + + vEnv, rEnv := BuildEnv() + defer vEnv.CleanUp() + // TODO: support VIEW, + tables := []string{ + "actor", + // "actor_info", // VIEW + "address", + "category", + "city", + "country", + "customer", + "customer_list", + "film", + "film_actor", + "film_category", + "film_list", + "film_text", + "inventory", + "language", + "nicer_but_slower_film_list", + "payment", + "rental", + // "sales_by_film_category", // VIEW + // "sales_by_store", // VIEW + "staff", + "staff_list", + "store", + } + for _, table := range tables { + err := vEnv.createTable(rEnv, "sakila", table) + if err != nil { + t.Error(err) + } + } + common.Config.SamplingCondition = orgSamplingCondition +} + +func TestCreateDatabase(t *testing.T) { + vEnv, rEnv := BuildEnv() + defer vEnv.CleanUp() + err := vEnv.createDatabase(rEnv, "sakila") + if err != nil { + t.Error(err) + } + if vEnv.DBHash("sakila") == "sakila" { + t.Errorf("database: sakila rehashed failed!") + } + + if vEnv.DBHash("not_exist_db") != "not_exist_db" { + t.Errorf("database: not_exist_db rehashed!") + } +}