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

Commit

Permalink
export: don't write SHOW MASTER STATUS when lacking privilege (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 authored Dec 3, 2020
1 parent a8a54d0 commit 92b87b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 9 additions & 5 deletions v4/export/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ func (m *globalMetadata) recordGlobalMetaData(db *sql.Conn, serverType ServerTyp
}

func recordGlobalMetaData(db *sql.Conn, buffer *bytes.Buffer, serverType ServerType, afterConn bool, snapshot string) error {
// get master status info
buffer.WriteString("SHOW MASTER STATUS:")
if afterConn {
buffer.WriteString(" /* AFTER CONNECTION POOL ESTABLISHED */")
writeMasterStatusHeader := func() {
buffer.WriteString("SHOW MASTER STATUS:")
if afterConn {
buffer.WriteString(" /* AFTER CONNECTION POOL ESTABLISHED */")
}
buffer.WriteString("\n")
}
buffer.WriteString("\n")

switch serverType {
// For MySQL:
// mysql 5.6+
Expand Down Expand Up @@ -106,6 +108,7 @@ func recordGlobalMetaData(db *sql.Conn, buffer *bytes.Buffer, serverType ServerT
gtidSet := getValidStr(str, gtidSetFieldIndex)

if logFile != "" {
writeMasterStatusHeader()
fmt.Fprintf(buffer, "\tLog: %s\n\tPos: %s\n\tGTID:%s\n", logFile, pos, gtidSet)
}
// For MariaDB:
Expand Down Expand Up @@ -136,6 +139,7 @@ func recordGlobalMetaData(db *sql.Conn, buffer *bytes.Buffer, serverType ServerT
}

if logFile != "" {
writeMasterStatusHeader()
fmt.Fprintf(buffer, "\tLog: %s\n\tPos: %s\n\tGTID:%s\n", logFile, pos, gtidSet)
}
default:
Expand Down
16 changes: 16 additions & 0 deletions v4/export/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package export

import (
"context"
"errors"
"fmt"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -269,3 +270,18 @@ func (s *testMetaDataSuite) TestTiDBSnapshotMetaData(c *C) {
"\tGTID:\n\n")
c.Assert(mock.ExpectationsWereMet(), IsNil)
}

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

mock.ExpectQuery("SHOW MASTER STATUS").WillReturnError(errors.New("lack SUPER or REPLICATION CLIENT privilege"))

m := newGlobalMetadata(s.createStorage(c), "")
// some consistencyType will ignore this error, this test make sure no extra message is written
c.Assert(m.recordGlobalMetaData(conn, ServerTypeTiDB, false), NotNil)
c.Assert(m.buffer.String(), Equals, "")
}

0 comments on commit 92b87b9

Please sign in to comment.