Skip to content

Commit

Permalink
Merge pull request #533 from ahfa92/main
Browse files Browse the repository at this point in the history
Database version test and various functions to compare with string like Equal Greater Between
  • Loading branch information
svaroqui authored Jan 18, 2024
2 parents 216cf29 + d90ca9e commit dd78b71
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cluster/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (cluster *Cluster) newServerMonitor(url string, user string, pass string, c
server.Domain = domain
server.TLSConfigUsed = ConstTLSCurrentConfig
server.ClusterGroup = cluster
server.DBVersion = dbhelper.NewMySQLVersion("Unknowed-0.0.0", "")
server.DBVersion, _ = dbhelper.NewMySQLVersion("Unknowed-0.0.0", "")
server.Name, server.Port, server.PostgressDB = misc.SplitHostPortDB(url)
server.ServiceName = cluster.Name + "/svc/" + server.Name
server.IsGroupReplicationSlave = false
Expand Down
11 changes: 5 additions & 6 deletions utils/dbhelper/dbhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ func ChangeMaster(db *sqlx.DB, opt ChangeMasterOpt, myver *MySQLVersion) (string
case "SLAVE_POS":
cm += ", " + masterOrSource + "_USE_GTID=SLAVE_POS"
case "CURRENT_POS":
if myver.Greater(*NewMySQLVersion("10.10.0", "")) && myver.IsMariaDB() {
if myver.Greater("10.10.0") && myver.IsMariaDB() {
cm += ", " + masterOrSource + "_USE_GTID=SLAVE_POS, MASTER_DEMOTE_TO_SLAVE=1"
} else {
cm += ", " + masterOrSource + "_USE_GTID=CURRENT_POS"
Expand Down Expand Up @@ -734,12 +734,13 @@ func GetDBVersion(db *sqlx.DB) (*MySQLVersion, string, error) {
if err != nil {
return &MySQLVersion{}, stmt, err
}
v := NewMySQLVersion(version, "")
v, _ := NewMySQLVersion(version, "")
if !v.IsPPostgreSQL() {
stmt = "SELECT @@version_comment"
db.QueryRowx(stmt).Scan(&versionComment)
}
return NewMySQLVersion(version, versionComment), stmt, nil
nv, _ := NewMySQLVersion(version, versionComment)
return nv, stmt, nil
}

// Unused does not look like safe way or documenting it
Expand Down Expand Up @@ -2682,9 +2683,7 @@ func BenchCleanup(db *sqlx.DB) error {

func AnalyzeTable(db *sqlx.DB, myver *MySQLVersion, table string) (string, error) {
query := "ANALYZE TABLE " + table
var v *MySQLVersion
v = NewMySQLVersion("10.4.0", "")
if myver.Greater(*v) && myver.IsMariaDB() {
if myver.Greater("10.4.0") && myver.IsMariaDB() {
query += " PERSISTENT FOR ALL"
}
_, err := db.Exec(query)
Expand Down
83 changes: 60 additions & 23 deletions utils/dbhelper/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"fmt"
"strconv"
"strings"

version "github.com/mcuadros/go-version"
)

type MySQLVersion struct {
Expand All @@ -24,11 +22,15 @@ type MySQLVersion struct {
Release int `json:"release"`
}

func NewMySQLVersion(version string, versionComment string) *MySQLVersion {
/*
Create new MySQLVersion object from string
*/
func NewMySQLVersion(version string, versionComment string) (*MySQLVersion, int) {
var tokens []string
mv := new(MySQLVersion)
if strings.Contains(version, "MariaDB") {
if strings.Contains(version, "MariaDB") || strings.Contains(versionComment, "MariaDB") {
mv.Flavor = "MariaDB"
} else if strings.Contains(version, "PostgreSQL") {
} else if strings.Contains(version, "PostgreSQL") || strings.Contains(versionComment, "PostgreSQL") {
mv.Flavor = "PostgreSQL"
} else if strings.Contains(versionComment, "Percona") {
mv.Flavor = "Percona"
Expand All @@ -38,38 +40,73 @@ func NewMySQLVersion(version string, versionComment string) *MySQLVersion {
if mv.Flavor == "PostgreSQL" {
infos := strings.Split(version, " ")
version = infos[1]
tokens := strings.Split(version, ".")
tokens = strings.Split(version, ".")
mv.Major, _ = strconv.Atoi(tokens[0])
mv.Minor, _ = strconv.Atoi(tokens[1])
if len(tokens) >= 2 {
mv.Minor, _ = strconv.Atoi(tokens[1])
}
if len(tokens) >= 3 {
mv.Release, _ = strconv.Atoi(tokens[2])
}
} else {
infos := strings.Split(version, "-")
version = infos[0]
tokens := strings.Split(version, ".")
tokens = strings.Split(version, ".")
mv.Major, _ = strconv.Atoi(tokens[0])
if len(tokens) >= 2 {
mv.Major, _ = strconv.Atoi(tokens[0])
mv.Minor, _ = strconv.Atoi(tokens[1])
}
if len(tokens) >= 3 {
mv.Release, _ = strconv.Atoi(tokens[2])
}
}
return mv
return mv, len(tokens)
}

func (mv *MySQLVersion) Between(Min MySQLVersion, Max MySQLVersion) bool {
ver := "1" + fmt.Sprintf("%03d", mv.Major) + fmt.Sprintf("%03d", mv.Minor) + fmt.Sprintf("%03d", mv.Release)
ver_min := "1" + fmt.Sprintf("%03d", Min.Major) + fmt.Sprintf("%03d", Min.Minor) + fmt.Sprintf("%03d", Min.Release)
ver_max := "1" + fmt.Sprintf("%03d", Max.Major) + fmt.Sprintf("%03d", Max.Minor) + fmt.Sprintf("%03d", Max.Release)
sup := version.Compare(ver, ver_min, ">")
inf := version.Compare(ver_max, ver, ">")
if sup && inf {
return true
func (mv *MySQLVersion) ToInt(tokens int) int {
//Major
if tokens == 1 {
return mv.Major * 1000000
}
return false
//Minor
if tokens == 2 {
return (mv.Major * 1000000) + (mv.Minor * 1000)
}

return (mv.Major * 1000000) + (mv.Minor * 1000) + mv.Release
}

func (mv *MySQLVersion) ToString() string {
return fmt.Sprintf("%d.%d.%d", mv.Major, mv.Minor, mv.Release)
}

func (mv *MySQLVersion) Greater(vstring string) bool {
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
return mv.ToInt(tokens) > v.ToInt(tokens)
}

func (mv *MySQLVersion) GreaterEqual(vstring string) bool {
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
return mv.ToInt(tokens) >= v.ToInt(tokens)
}

func (mv *MySQLVersion) Lower(vstring string) bool {
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
return mv.ToInt(tokens) < v.ToInt(tokens)
}

func (mv *MySQLVersion) LowerEqual(vstring string) bool {
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
return mv.ToInt(tokens) <= v.ToInt(tokens)
}

func (mv *MySQLVersion) Equal(vstring string) bool {
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
return mv.ToInt(tokens) == v.ToInt(tokens)
}

func (mv *MySQLVersion) Greater(Min MySQLVersion) bool {
ver := "1" + fmt.Sprintf("%03d", mv.Major) + fmt.Sprintf("%03d", mv.Minor) + fmt.Sprintf("%03d", mv.Release)
ver_min := "1" + fmt.Sprintf("%03d", Min.Major) + fmt.Sprintf("%03d", Min.Minor) + fmt.Sprintf("%03d", Min.Release)
return version.Compare(ver, ver_min, ">")
func (mv *MySQLVersion) Between(minvstring string, maxvstring string) bool {
return mv.GreaterEqual(minvstring) && mv.LowerEqual(maxvstring)
}

func (mv *MySQLVersion) IsMySQL() bool {
Expand Down
Loading

0 comments on commit dd78b71

Please sign in to comment.