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

fix scan null string error #150

Merged
merged 2 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe c.MkDir or something in /tmp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't write file to disk since we don't use writeGlobalMetaData. So I think it's okay.

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