Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
fix scan null string error (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunzhu authored Sep 9, 2020
1 parent bc33935 commit a45daad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
28 changes: 15 additions & 13 deletions v4/export/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (m *globalMetadata) recordGlobalMetaData(db *sql.Conn, serverType ServerTyp
if err != nil {
return err
}
data := make([]string, len(cols))
data := make([]sql.NullString, len(cols))
args := make([]interface{}, 0, len(cols))
for i := range data {
args = append(args, &data[i])
Expand All @@ -162,18 +162,20 @@ func (m *globalMetadata) recordGlobalMetaData(db *sql.Conn, serverType ServerTyp
}
var connName, pos, logFile, host, gtidSet string
for i, col := range cols {
col = strings.ToLower(col)
switch col {
case "connection_name":
connName = data[i]
case "exec_master_log_pos":
pos = data[i]
case "relay_master_log_file":
logFile = data[i]
case "master_host":
host = data[i]
case "executed_gtid_set":
gtidSet = data[i]
if data[i].Valid {
col = strings.ToLower(col)
switch col {
case "connection_name":
connName = data[i].String
case "exec_master_log_pos":
pos = data[i].String
case "relay_master_log_file":
logFile = data[i].String
case "master_host":
host = data[i].String
case "executed_gtid_set":
gtidSet = data[i].String
}
}
}
if len(host) > 0 {
Expand Down
28 changes: 28 additions & 0 deletions v4/export/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,34 @@ func (s *testMetaDataSuite) TestMysqlWithFollowersMetaData(c *C) {
c.Assert(mock.ExpectationsWereMet(), IsNil)
}

func (s *testMetaDataSuite) TestMysqlWithNullFollowersMetaData(c *C) {
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)
defer db.Close()
conn, err := db.Conn(context.Background())
c.Assert(err, IsNil)

logFile := "ON.000001"
pos := "7502"
gtidSet := "6ce40be3-e359-11e9-87e0-36933cb0ca5a:1-29"
rows := sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB", "Executed_Gtid_Set"}).
AddRow(logFile, pos, "", "", gtidSet)
mock.ExpectQuery("SHOW MASTER STATUS").WillReturnRows(rows)
mock.ExpectQuery("SELECT @@default_master_connection").WillReturnError(fmt.Errorf("mock error"))
mock.ExpectQuery("SHOW SLAVE STATUS").WillReturnRows(sqlmock.NewRows([]string{"SQL_Remaining_Delay"}).AddRow(nil))

testFilePath := "/test"
m := newGlobalMetadata(testFilePath)
c.Assert(m.recordGlobalMetaData(conn, ServerTypeMySQL, false), IsNil)
c.Assert(m.filePath, Equals, path.Join(testFilePath, metadataPath))

c.Assert(m.buffer.String(), Equals, "SHOW MASTER STATUS:\n"+
"\tLog: ON.000001\n"+
"\tPos: 7502\n"+
"\tGTID:6ce40be3-e359-11e9-87e0-36933cb0ca5a:1-29\n\n")
c.Assert(mock.ExpectationsWereMet(), IsNil)
}

func (s *testMetaDataSuite) TestMariaDBMetaData(c *C) {
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)
Expand Down

0 comments on commit a45daad

Please sign in to comment.