MCPcopy Create free account
hub / github.com/bytebase/bytebase / ValidateBinlogAccess

Function ValidateBinlogAccess

backend/component/ghost/validator.go:42–158  ·  view source on GitHub ↗

ValidateBinlogAccess performs comprehensive validation of gh-ost prerequisites Returns a structured result that can be used for both plan checks and execution

(ctx context.Context, driver db.Driver, adminDataSource *storepb.DataSource)

Source from the content-addressed store, hash-verified

40// ValidateBinlogAccess performs comprehensive validation of gh-ost prerequisites
41// Returns a structured result that can be used for both plan checks and execution
42func ValidateBinlogAccess(ctx context.Context, driver db.Driver, adminDataSource *storepb.DataSource) *BinlogValidationResult {
43 result := &BinlogValidationResult{
44 Valid: true,
45 BinlogEnabled: false,
46 HasPrivilege: false,
47 MissingPrivileges: []string{},
48 CurrentGrants: []string{},
49 }
50
51 // Test 1: Check if we can access binary log status
52 // Try both old and new MySQL commands for compatibility
53 canAccessBinlog := false
54 if _, err := driver.GetDB().ExecContext(ctx, "SHOW MASTER STATUS"); err == nil {
55 canAccessBinlog = true
56 } else if _, err := driver.GetDB().ExecContext(ctx, "SHOW BINARY LOG STATUS"); err == nil {
57 canAccessBinlog = true
58 }
59
60 if !canAccessBinlog {
61 result.Valid = false
62 result.FailureReason = binlogStatusInaccessible
63 result.Error = errors.New("cannot access binary logs - ensure user has REPLICATION CLIENT privilege")
64 slog.Error("binlog access validation failed: cannot access binary logs",
65 slog.String("host", adminDataSource.GetHost()),
66 slog.String("user", adminDataSource.GetUsername()))
67 return result
68 }
69
70 // Test 2: Check if binary logging is enabled
71 var logBin string
72 row := driver.GetDB().QueryRowContext(ctx, "SELECT @@log_bin")
73 if err := row.Scan(&logBin); err != nil {
74 result.Valid = false
75 result.FailureReason = validationQueryFailed
76 result.Error = errors.Wrap(err, "failed to check if binary logging is enabled")
77 return result
78 }
79
80 result.BinlogEnabled = (logBin == "1" || strings.ToUpper(logBin) == "ON")
81 if !result.BinlogEnabled {
82 result.Valid = false
83 result.FailureReason = binlogDisabled
84 result.Error = errors.New("binary logging is not enabled on this MySQL instance")
85 return result
86 }
87
88 // Test 3: Check user privileges
89 rows, err := driver.GetDB().QueryContext(ctx, "SHOW GRANTS")
90 if err != nil {
91 result.Valid = false
92 result.FailureReason = validationQueryFailed
93 result.Error = errors.Wrap(err, "failed to check user grants")
94 return result
95 }
96 defer rows.Close()
97
98 for rows.Next() {
99 var grant string

Callers 1

RunForTargetMethod · 0.92

Calls 11

ScanMethod · 0.80
QueryContextMethod · 0.80
ErrorfMethod · 0.80
InfoMethod · 0.80
GetDBMethod · 0.65
StringMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45
GetHostMethod · 0.45
GetUsernameMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected