From 16f2083685718d26290754ac8be277f30d9e7759 Mon Sep 17 00:00:00 2001 From: Chunzhu Li Date: Wed, 9 Sep 2020 17:27:28 +0800 Subject: [PATCH] fix scan null string error (#150) --- dumpling/v4/export/metadata.go | 28 +++++++++++++++------------- dumpling/v4/export/metadata_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/dumpling/v4/export/metadata.go b/dumpling/v4/export/metadata.go index c8e27cd8..a0eadc22 100644 --- a/dumpling/v4/export/metadata.go +++ b/dumpling/v4/export/metadata.go @@ -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]) @@ -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 { diff --git a/dumpling/v4/export/metadata_test.go b/dumpling/v4/export/metadata_test.go index 3bee9872..cf11e837 100644 --- a/dumpling/v4/export/metadata_test.go +++ b/dumpling/v4/export/metadata_test.go @@ -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)