Skip to content

Commit

Permalink
NewConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
martianzhang committed Dec 21, 2018
1 parent 7847713 commit 16df1e3
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 220 deletions.
36 changes: 10 additions & 26 deletions advisor/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,30 @@
package advisor

import (
"flag"
"fmt"
"os"
"strings"
"testing"

"github.com/XiaoMi/soar/common"
"github.com/XiaoMi/soar/database"
"github.com/XiaoMi/soar/env"

"github.com/kr/pretty"
"vitess.io/vitess/go/vt/sqlparser"
)

var update = flag.Bool("update", false, "update .golden files")
var vEnv *env.VirtualEnv
var rEnv *database.Connector

func init() {
common.BaseDir = common.DevPath
err := common.ParseConfig("")
if err != nil {
fmt.Println(err.Error())
}
vEnv, rEnv := env.BuildEnv()
common.LogIfError(err, "init ParseConfig")
common.Log.Debug("advisor_test init")
vEnv, rEnv = env.BuildEnv()
if _, err = vEnv.Version(); err != nil {
fmt.Println(err.Error(), ", By pass all advisor test cases")
os.Exit(0)
Expand All @@ -45,15 +50,14 @@ func init() {
fmt.Println(err.Error(), ", By pass all advisor test cases")
os.Exit(0)
}
defer vEnv.CleanUp()
}

// ARG.003
func TestRuleImplicitConversion(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
dsn := common.Config.OnlineDSN
common.Config.OnlineDSN = common.Config.TestDSN
vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

initSQLs := []string{
`CREATE TABLE t1 (id int, title varchar(255) CHARSET utf8 COLLATE utf8_general_ci);`,
Expand Down Expand Up @@ -104,9 +108,6 @@ func TestRuleImpossibleOuterJoin(t *testing.T) {
`select city_id, city, country from city left outer join country on city.country_id=country.country_id WHERE city.city_id IS NULL`,
}

vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

for _, sql := range sqls {
stmt, syntaxErr := sqlparser.Parse(sql)
if syntaxErr != nil {
Expand Down Expand Up @@ -146,9 +147,6 @@ func TestIndexAdvisorRuleGroupByConst(t *testing.T) {
},
}

vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

for _, sql := range sqls[0] {
stmt, syntaxErr := sqlparser.Parse(sql)
if syntaxErr != nil {
Expand Down Expand Up @@ -211,9 +209,6 @@ func TestIndexAdvisorRuleOrderByConst(t *testing.T) {
},
}

vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

for _, sql := range sqls[0] {
stmt, syntaxErr := sqlparser.Parse(sql)
if syntaxErr != nil {
Expand Down Expand Up @@ -275,9 +270,6 @@ func TestRuleUpdatePrimaryKey(t *testing.T) {
},
}

vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

for _, sql := range sqls[0] {
stmt, syntaxErr := sqlparser.Parse(sql)
if syntaxErr != nil {
Expand Down Expand Up @@ -328,8 +320,6 @@ func TestRuleUpdatePrimaryKey(t *testing.T) {

func TestIndexAdvise(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

for _, sql := range common.TestSQLs {
stmt, syntaxErr := sqlparser.Parse(sql)
Expand Down Expand Up @@ -360,8 +350,6 @@ func TestIndexAdviseNoEnv(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
orgOnlineDSNStatus := common.Config.OnlineDSN.Disable
common.Config.OnlineDSN.Disable = true
vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

for _, sql := range common.TestSQLs {
stmt, syntaxErr := sqlparser.Parse(sql)
Expand Down Expand Up @@ -391,7 +379,6 @@ func TestIndexAdviseNoEnv(t *testing.T) {

func TestDuplicateKeyChecker(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
_, rEnv := env.BuildEnv()
rule := DuplicateKeyChecker(rEnv, "sakila")
if len(rule) != 0 {
t.Errorf("got rules: %s", pretty.Sprint(rule))
Expand Down Expand Up @@ -426,9 +413,6 @@ func TestIdxColsTypeCheck(t *testing.T) {
`select city_id, city, country from city left outer join country using(country_id) WHERE city.city_id=59 and country.country='Algeria'`,
}

vEnv, rEnv := env.BuildEnv()
defer vEnv.CleanUp()

for _, sql := range sqls {
stmt, syntaxErr := sqlparser.Parse(sql)
if syntaxErr != nil {
Expand Down
3 changes: 0 additions & 3 deletions advisor/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
package advisor

import (
"flag"
"testing"

"github.com/XiaoMi/soar/common"
)

var update = flag.Bool("update", false, "update .golden files")

func TestListTestSQLs(t *testing.T) {
err := common.GoldenDiff(func() { ListTestSQLs() }, t.Name(), update)
if nil != err {
Expand Down
2 changes: 1 addition & 1 deletion cmd/soar/soar.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func main() {

// 当程序卡死的时候,或者由于某些原因程序没有退出,可以通过捕获信号量的形式让程序优雅退出并且清理测试环境
common.HandleSignal(func() {
shutdown(vEnv)
shutdown(vEnv, rEnv)
})

// 对指定的库表进行索引重复检查
Expand Down
44 changes: 21 additions & 23 deletions cmd/soar/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,48 @@ func initConfig() {
// if error found return non-zero, no error return zero
func checkConfig() int {
// TestDSN connection check
testConn := &database.Connector{
Addr: common.Config.TestDSN.Addr,
User: common.Config.TestDSN.User,
Pass: common.Config.TestDSN.Password,
Database: common.Config.TestDSN.Schema,
Charset: common.Config.TestDSN.Charset,
}
testVersion, err := testConn.Version()
connTest, err := database.NewConnector(common.Config.TestDSN)
if err != nil {
fmt.Println("test-dsn:", common.Config.TestDSN.Addr, err.Error())
return 1
}
testVersion, err := connTest.Version()
if err != nil && !common.Config.TestDSN.Disable {
fmt.Println("test-dsn:", testConn, err.Error())
fmt.Println("test-dsn:", connTest, err.Error())
return 1
}
if common.Config.Verbose {
if err == nil {
fmt.Println("test-dsn", testConn, "Version:", testVersion)
fmt.Println("test-dsn", connTest, "Version:", testVersion)
} else {
fmt.Println("test-dsn", common.Config.TestDSN)
}
}

if !testConn.HasAllPrivilege() {
if !connTest.HasAllPrivilege() {
fmt.Printf("test-dsn: %s, need all privileges", common.FormatDSN(common.Config.TestDSN))
return 1
}
// OnlineDSN connection check
onlineConn := &database.Connector{
Addr: common.Config.OnlineDSN.Addr,
User: common.Config.OnlineDSN.User,
Pass: common.Config.OnlineDSN.Password,
Database: common.Config.OnlineDSN.Schema,
Charset: common.Config.OnlineDSN.Charset,
}
onlineVersion, err := onlineConn.Version()
connOnline, err := database.NewConnector(common.Config.OnlineDSN)
if err != nil {
fmt.Println("test-dsn:", common.Config.OnlineDSN.Addr, err.Error())
return 1
}
onlineVersion, err := connOnline.Version()
if err != nil && !common.Config.OnlineDSN.Disable {
fmt.Println("online-dsn:", onlineConn, err.Error())
fmt.Println("online-dsn:", connOnline, err.Error())
return 1
}
if common.Config.Verbose {
if err == nil {
fmt.Println("online-dsn", onlineConn, "Version:", onlineVersion)
fmt.Println("online-dsn", connOnline, "Version:", onlineVersion)
} else {
fmt.Println("online-dsn", common.Config.OnlineDSN)
}
}

if !onlineConn.HasSelectPrivilege() {
if !connOnline.HasSelectPrivilege() {
fmt.Printf("online-dsn: %s, need all privileges", common.FormatDSN(common.Config.OnlineDSN))
return 1
}
Expand Down Expand Up @@ -229,9 +225,11 @@ func initQuery(query string) string {
return query
}

func shutdown(vEnv *env.VirtualEnv) {
func shutdown(vEnv *env.VirtualEnv, rEnv *database.Connector) {
if common.Config.DropTestTemporary {
vEnv.CleanUp()
}
vEnv.Conn.Close()
rEnv.Conn.Close()
os.Exit(0)
}
19 changes: 10 additions & 9 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ var (
// Configuration 配置文件定义结构体
type Configuration struct {
// +++++++++++++++测试环境+++++++++++++++++
OnlineDSN *dsn `yaml:"online-dsn"` // 线上环境数据库配置
TestDSN *dsn `yaml:"test-dsn"` // 测试环境数据库配置
OnlineDSN *Dsn `yaml:"online-dsn"` // 线上环境数据库配置
TestDSN *Dsn `yaml:"test-dsn"` // 测试环境数据库配置
AllowOnlineAsTest bool `yaml:"allow-online-as-test"` // 允许 Online 环境也可以当作 Test 环境
DropTestTemporary bool `yaml:"drop-test-temporary"` // 是否清理Test环境产生的临时库表
CleanupTestDatabase bool `yaml:"cleanup-test-database"` // 清理残余的测试数据库(程序异常退出或未开启drop-test-temporary) issue #48
Expand Down Expand Up @@ -132,14 +132,14 @@ type Configuration struct {

// Config 默认设置
var Config = &Configuration{
OnlineDSN: &dsn{
OnlineDSN: &Dsn{
Net: "tcp",
Schema: "information_schema",
Charset: "utf8mb4",
Disable: true,
Version: 99999,
},
TestDSN: &dsn{
TestDSN: &Dsn{
Net: "tcp",
Schema: "information_schema",
Charset: "utf8mb4",
Expand Down Expand Up @@ -224,7 +224,8 @@ var Config = &Configuration{
MaxPrettySQLLength: 1024,
}

type dsn struct {
// Dsn Data source name
type Dsn struct {
Net string `yaml:"net"`
Addr string `yaml:"addr"`
Schema string `yaml:"schema"`
Expand All @@ -243,7 +244,7 @@ type dsn struct {
}

// 解析命令行DSN输入
func parseDSN(odbc string, d *dsn) *dsn {
func parseDSN(odbc string, d *Dsn) *Dsn {
var addr, user, password, schema, charset string
if odbc == FormatDSN(d) {
return d
Expand All @@ -260,7 +261,7 @@ func parseDSN(odbc string, d *dsn) *dsn {
// 设置为空表示禁用环境
odbc = strings.TrimSpace(odbc)
if odbc == "" {
return &dsn{Disable: true}
return &Dsn{Disable: true}
}

// username:password@ip:port/database
Expand Down Expand Up @@ -354,7 +355,7 @@ func parseDSN(odbc string, d *dsn) *dsn {
charset = "utf8mb4"
}

dsn := &dsn{
dsn := &Dsn{
Addr: addr,
User: user,
Password: password,
Expand All @@ -367,7 +368,7 @@ func parseDSN(odbc string, d *dsn) *dsn {
}

// FormatDSN 格式化打印DSN
func FormatDSN(env *dsn) string {
func FormatDSN(env *Dsn) string {
if env == nil || env.Disable {
return ""
}
Expand Down
2 changes: 1 addition & 1 deletion common/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package common
import "fmt"

func ExampleFormatDSN() {
dsxExp := &dsn{
dsxExp := &Dsn{
Addr: "127.0.0.1:3306",
Schema: "mysql",
User: "root",
Expand Down
2 changes: 1 addition & 1 deletion common/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewDB(db string) *DB {
}
}

// TableName 含有表的属性
// Table 含有表的属性
type Table struct {
TableName string
TableAliases []string
Expand Down
Loading

0 comments on commit 16df1e3

Please sign in to comment.