提交 ad1b1e0b 编写于 作者: martianzhang's avatar martianzhang

fix go test -race ./... 😁

上级 caf32e9e
...@@ -61,7 +61,7 @@ fmt: go_version_check ...@@ -61,7 +61,7 @@ fmt: go_version_check
.PHONY: test .PHONY: test
test: test:
@echo "\033[92mRun all test cases ...\033[0m" @echo "\033[92mRun all test cases ...\033[0m"
go test ./... go test -race ./...
@echo "test Success!" @echo "test Success!"
# Code Coverage # Code Coverage
......
...@@ -48,13 +48,13 @@ func init() { ...@@ -48,13 +48,13 @@ func init() {
} }
func TestNewConnection(t *testing.T) { func TestNewConnection(t *testing.T) {
_, err := connTest.NewConnection() conn, err := connTest.NewConnection()
if err != nil { if err != nil {
t.Errorf("TestNewConnection, Error: %s", err.Error()) t.Errorf("TestNewConnection, Error: %s", err.Error())
} }
defer conn.Close()
} }
// TODO: go test -race不通过待解决
func TestQuery(t *testing.T) { func TestQuery(t *testing.T) {
res, err := connTest.Query("select 0") res, err := connTest.Query("select 0")
if err != nil { if err != nil {
......
...@@ -372,19 +372,38 @@ func (td TableDesc) Columns() []string { ...@@ -372,19 +372,38 @@ func (td TableDesc) Columns() []string {
// showCreate show create // showCreate show create
func (db *Connector) showCreate(createType, name string) (string, error) { func (db *Connector) showCreate(createType, name string) (string, error) {
// 执行 show create table|database // 执行 show create table|database
// createType = [table|database] // createType = [table|database|view]
res, err := db.Query(fmt.Sprintf("show create %s `%s`", createType, name)) res, err := db.Query(fmt.Sprintf("show create %s `%s`", createType, name))
if err != nil { if err != nil {
return "", err return "", err
} }
// 获取 CREATE TABLE 语句 // columns info
var tableName, createTable string var colByPass []byte
var create string
createFields := make([]interface{}, 0)
fields := map[string]interface{}{
"Create View": &create,
"Create Table": &create,
"Create Database": &create,
"Table": &colByPass,
"Database": &colByPass,
"View": &colByPass,
"character_set_client": &colByPass,
"collation_connection": &colByPass,
}
cols, err := res.Rows.Columns()
common.LogIfError(err, "")
for _, col := range cols {
createFields = append(createFields, fields[col])
}
// 获取 CREATE TABLE|VIEW|DATABASE 语句
for res.Rows.Next() { for res.Rows.Next() {
res.Rows.Scan(&tableName, &createTable) res.Rows.Scan(createFields...)
} }
return createTable, err return create, err
} }
// ShowCreateDatabase show create database // ShowCreateDatabase show create database
......
...@@ -23,7 +23,6 @@ import ( ...@@ -23,7 +23,6 @@ import (
"github.com/XiaoMi/soar/common" "github.com/XiaoMi/soar/common"
"github.com/kr/pretty" "github.com/kr/pretty"
"vitess.io/vitess/go/vt/sqlparser"
) )
func TestShowTableStatus(t *testing.T) { func TestShowTableStatus(t *testing.T) {
...@@ -72,18 +71,18 @@ func TestShowTables(t *testing.T) { ...@@ -72,18 +71,18 @@ func TestShowTables(t *testing.T) {
func TestShowCreateTable(t *testing.T) { func TestShowCreateTable(t *testing.T) {
orgDatabase := connTest.Database orgDatabase := connTest.Database
connTest.Database = "sakila" connTest.Database = "sakila"
ts, err := connTest.ShowCreateTable("film") tables := []string{
if err != nil { "film",
t.Error("ShowCreateTable Error: ", err) "customer_list",
} }
err := common.GoldenDiff(func() {
err = common.GoldenDiff(func() { for _, table := range tables {
fmt.Println(ts) ts, err := connTest.ShowCreateTable(table)
stmt, err := sqlparser.Parse(ts) if err != nil {
if err != nil { t.Error("ShowCreateTable Error: ", err)
t.Error(err.Error()) }
fmt.Println(ts)
} }
pretty.Println(stmt, err)
}, t.Name(), update) }, t.Name(), update)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
......
...@@ -17,18 +17,4 @@ CREATE TABLE `film` ( ...@@ -17,18 +17,4 @@ CREATE TABLE `film` (
KEY `idx_fk_language_id` (`language_id`), KEY `idx_fk_language_id` (`language_id`),
KEY `idx_fk_original_language_id` (`original_language_id`) KEY `idx_fk_original_language_id` (`original_language_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8 ) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8
&sqlparser.DDL{ CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `customer_list` AS select `cu`.`customer_id` AS `ID`,concat(`cu`.`first_name`,_utf8mb3' ',`cu`.`last_name`) AS `name`,`a`.`address` AS `address`,`a`.`postal_code` AS `zip code`,`a`.`phone` AS `phone`,`city`.`city` AS `city`,`country`.`country` AS `country`,if(`cu`.`active`,_utf8mb3'active',_utf8mb3'') AS `notes`,`cu`.`store_id` AS `SID` from (((`customer` `cu` join `address` `a` on((`cu`.`address_id` = `a`.`address_id`))) join `city` on((`a`.`city_id` = `city`.`city_id`))) join `country` on((`city`.`country_id` = `country`.`country_id`)))
Action: "create",
FromTables: nil,
ToTables: nil,
Table: sqlparser.TableName{
Name: sqlparser.TableIdent{v:"film"},
Qualifier: sqlparser.TableIdent{},
},
IfExists: false,
TableSpec: (*sqlparser.TableSpec)(nil),
OptLike: (*sqlparser.OptLike)(nil),
PartitionSpec: (*sqlparser.PartitionSpec)(nil),
VindexSpec: (*sqlparser.VindexSpec)(nil),
VindexCols: nil,
} nil
...@@ -331,6 +331,10 @@ func (ve *VirtualEnv) BuildVirtualEnv(rEnv *database.Connector, SQLs ...string) ...@@ -331,6 +331,10 @@ func (ve *VirtualEnv) BuildVirtualEnv(rEnv *database.Connector, SQLs ...string)
} }
startIdx := strings.Index(viewDDL, "AS") startIdx := strings.Index(viewDDL, "AS")
if startIdx < 0 || viewDDL == "" {
common.Log.Error("BuildVirtualEnv '%s' got '%s', Index: %d", tb.TableName, viewDDL, startIdx)
return false
}
viewDDL = viewDDL[startIdx+2:] viewDDL = viewDDL[startIdx+2:]
if !ve.BuildVirtualEnv(&tmpEnv, viewDDL) { if !ve.BuildVirtualEnv(&tmpEnv, viewDDL) {
return false return false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册