Skip to content

Commit

Permalink
Adding support for returning replica status with deprecated column na…
Browse files Browse the repository at this point in the history
…mes, when show slave status is executed
  • Loading branch information
fulghum committed Nov 25, 2024
1 parent 6625ccc commit 3ebfbf4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
31 changes: 29 additions & 2 deletions sql/plan/show_replica_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package plan

import (
"strings"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/binlogreplication"
"github.com/dolthub/go-mysql-server/sql/types"
Expand All @@ -26,16 +28,28 @@ import (
// https://dev.mysql.com/doc/refman/8.0/en/show-replica-status.html
type ShowReplicaStatus struct {
ReplicaController binlogreplication.BinlogReplicaController
// useDeprecatedColumnNames is used to determine if the column names returned should be the old
// and deprecated master/slave column names, or the new source/replica column names.
useDeprecatedColumnNames bool
}

var _ sql.Node = (*ShowReplicaStatus)(nil)
var _ sql.CollationCoercible = (*ShowReplicaStatus)(nil)
var _ BinlogReplicaControllerCommand = (*ShowReplicaStatus)(nil)

// NewShowReplicaStatus creates a new ShowReplicaStatus node.
func NewShowReplicaStatus() *ShowReplicaStatus {
return &ShowReplicaStatus{}
}

// NewShowSlaveStatus creates a new ShowReplicaStatus node configured to use the old, deprecated
// column names (i.e. master/slave) instead of the new, renamed columns (i.e. source/replica).
func NewShowSlaveStatus() *ShowReplicaStatus {
return &ShowReplicaStatus{
useDeprecatedColumnNames: true,
}
}

// WithBinlogReplicaController implements the BinlogReplicaControllerCommand interface.
func (s *ShowReplicaStatus) WithBinlogReplicaController(controller binlogreplication.BinlogReplicaController) sql.Node {
nc := *s
Expand All @@ -48,11 +62,15 @@ func (s *ShowReplicaStatus) Resolved() bool {
}

func (s *ShowReplicaStatus) String() string {
return "SHOW REPLICA STATUS"
if s.useDeprecatedColumnNames {
return "SHOW SLAVE STATUS"
} else {
return "SHOW REPLICA STATUS"
}
}

func (s *ShowReplicaStatus) Schema() sql.Schema {
return sql.Schema{
sch := sql.Schema{
{Name: "Replica_IO_State", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false},
{Name: "Source_Host", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 255), Default: nil, Nullable: false},
{Name: "Source_User", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false},
Expand Down Expand Up @@ -109,6 +127,15 @@ func (s *ShowReplicaStatus) Schema() sql.Schema {
{Name: "Auto_Position", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false},
{Name: "Replicate_Rewrite_DB", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 64), Default: nil, Nullable: false},
}

if s.useDeprecatedColumnNames {
for i := range sch {
sch[i].Name = strings.ReplaceAll(sch[i].Name, "Source", "Master")
sch[i].Name = strings.ReplaceAll(sch[i].Name, "Replica", "Slave")
}
}

return sch
}

func (s *ShowReplicaStatus) Children() []sql.Node {
Expand Down
9 changes: 9 additions & 0 deletions sql/planbuilder/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func (b *Builder) buildShow(inScope *scope, s *ast.Show) (outScope *scope) {
showRep.ReplicaController = binCat.GetBinlogReplicaController()
}
outScope.node = showRep
case "slave status":
// The deprecated "show slave status" command returns the same information as "show replica status",
// but uses a schema with different column names so we create the node differently here.
outScope = inScope.push()
showRep := plan.NewShowSlaveStatus()
if binCat, ok := b.cat.(binlogreplication.BinlogReplicaCatalog); ok && binCat.HasBinlogReplicaController() {
showRep.ReplicaController = binCat.GetBinlogReplicaController()
}
outScope.node = showRep
default:
unsupportedShow := fmt.Sprintf("SHOW %s", s.Type)
b.handleErr(sql.ErrUnsupportedFeature.New(unsupportedShow))
Expand Down

0 comments on commit 3ebfbf4

Please sign in to comment.