Skip to content

Commit dd78b71

Browse files
authored
Merge pull request #533 from ahfa92/main
Database version test and various functions to compare with string like Equal Greater Between
2 parents 216cf29 + d90ca9e commit dd78b71

File tree

4 files changed

+314
-30
lines changed

4 files changed

+314
-30
lines changed

cluster/srv.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (cluster *Cluster) newServerMonitor(url string, user string, pass string, c
226226
server.Domain = domain
227227
server.TLSConfigUsed = ConstTLSCurrentConfig
228228
server.ClusterGroup = cluster
229-
server.DBVersion = dbhelper.NewMySQLVersion("Unknowed-0.0.0", "")
229+
server.DBVersion, _ = dbhelper.NewMySQLVersion("Unknowed-0.0.0", "")
230230
server.Name, server.Port, server.PostgressDB = misc.SplitHostPortDB(url)
231231
server.ServiceName = cluster.Name + "/svc/" + server.Name
232232
server.IsGroupReplicationSlave = false

utils/dbhelper/dbhelper.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func ChangeMaster(db *sqlx.DB, opt ChangeMasterOpt, myver *MySQLVersion) (string
668668
case "SLAVE_POS":
669669
cm += ", " + masterOrSource + "_USE_GTID=SLAVE_POS"
670670
case "CURRENT_POS":
671-
if myver.Greater(*NewMySQLVersion("10.10.0", "")) && myver.IsMariaDB() {
671+
if myver.Greater("10.10.0") && myver.IsMariaDB() {
672672
cm += ", " + masterOrSource + "_USE_GTID=SLAVE_POS, MASTER_DEMOTE_TO_SLAVE=1"
673673
} else {
674674
cm += ", " + masterOrSource + "_USE_GTID=CURRENT_POS"
@@ -734,12 +734,13 @@ func GetDBVersion(db *sqlx.DB) (*MySQLVersion, string, error) {
734734
if err != nil {
735735
return &MySQLVersion{}, stmt, err
736736
}
737-
v := NewMySQLVersion(version, "")
737+
v, _ := NewMySQLVersion(version, "")
738738
if !v.IsPPostgreSQL() {
739739
stmt = "SELECT @@version_comment"
740740
db.QueryRowx(stmt).Scan(&versionComment)
741741
}
742-
return NewMySQLVersion(version, versionComment), stmt, nil
742+
nv, _ := NewMySQLVersion(version, versionComment)
743+
return nv, stmt, nil
743744
}
744745

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

26832684
func AnalyzeTable(db *sqlx.DB, myver *MySQLVersion, table string) (string, error) {
26842685
query := "ANALYZE TABLE " + table
2685-
var v *MySQLVersion
2686-
v = NewMySQLVersion("10.4.0", "")
2687-
if myver.Greater(*v) && myver.IsMariaDB() {
2686+
if myver.Greater("10.4.0") && myver.IsMariaDB() {
26882687
query += " PERSISTENT FOR ALL"
26892688
}
26902689
_, err := db.Exec(query)

utils/dbhelper/version.go

+60-23
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
"fmt"
1414
"strconv"
1515
"strings"
16-
17-
version "github.com/mcuadros/go-version"
1816
)
1917

2018
type MySQLVersion struct {
@@ -24,11 +22,15 @@ type MySQLVersion struct {
2422
Release int `json:"release"`
2523
}
2624

27-
func NewMySQLVersion(version string, versionComment string) *MySQLVersion {
25+
/*
26+
Create new MySQLVersion object from string
27+
*/
28+
func NewMySQLVersion(version string, versionComment string) (*MySQLVersion, int) {
29+
var tokens []string
2830
mv := new(MySQLVersion)
29-
if strings.Contains(version, "MariaDB") {
31+
if strings.Contains(version, "MariaDB") || strings.Contains(versionComment, "MariaDB") {
3032
mv.Flavor = "MariaDB"
31-
} else if strings.Contains(version, "PostgreSQL") {
33+
} else if strings.Contains(version, "PostgreSQL") || strings.Contains(versionComment, "PostgreSQL") {
3234
mv.Flavor = "PostgreSQL"
3335
} else if strings.Contains(versionComment, "Percona") {
3436
mv.Flavor = "Percona"
@@ -38,38 +40,73 @@ func NewMySQLVersion(version string, versionComment string) *MySQLVersion {
3840
if mv.Flavor == "PostgreSQL" {
3941
infos := strings.Split(version, " ")
4042
version = infos[1]
41-
tokens := strings.Split(version, ".")
43+
tokens = strings.Split(version, ".")
4244
mv.Major, _ = strconv.Atoi(tokens[0])
43-
mv.Minor, _ = strconv.Atoi(tokens[1])
45+
if len(tokens) >= 2 {
46+
mv.Minor, _ = strconv.Atoi(tokens[1])
47+
}
48+
if len(tokens) >= 3 {
49+
mv.Release, _ = strconv.Atoi(tokens[2])
50+
}
4451
} else {
4552
infos := strings.Split(version, "-")
4653
version = infos[0]
47-
tokens := strings.Split(version, ".")
54+
tokens = strings.Split(version, ".")
55+
mv.Major, _ = strconv.Atoi(tokens[0])
4856
if len(tokens) >= 2 {
49-
mv.Major, _ = strconv.Atoi(tokens[0])
5057
mv.Minor, _ = strconv.Atoi(tokens[1])
58+
}
59+
if len(tokens) >= 3 {
5160
mv.Release, _ = strconv.Atoi(tokens[2])
5261
}
5362
}
54-
return mv
63+
return mv, len(tokens)
5564
}
5665

57-
func (mv *MySQLVersion) Between(Min MySQLVersion, Max MySQLVersion) bool {
58-
ver := "1" + fmt.Sprintf("%03d", mv.Major) + fmt.Sprintf("%03d", mv.Minor) + fmt.Sprintf("%03d", mv.Release)
59-
ver_min := "1" + fmt.Sprintf("%03d", Min.Major) + fmt.Sprintf("%03d", Min.Minor) + fmt.Sprintf("%03d", Min.Release)
60-
ver_max := "1" + fmt.Sprintf("%03d", Max.Major) + fmt.Sprintf("%03d", Max.Minor) + fmt.Sprintf("%03d", Max.Release)
61-
sup := version.Compare(ver, ver_min, ">")
62-
inf := version.Compare(ver_max, ver, ">")
63-
if sup && inf {
64-
return true
66+
func (mv *MySQLVersion) ToInt(tokens int) int {
67+
//Major
68+
if tokens == 1 {
69+
return mv.Major * 1000000
6570
}
66-
return false
71+
//Minor
72+
if tokens == 2 {
73+
return (mv.Major * 1000000) + (mv.Minor * 1000)
74+
}
75+
76+
return (mv.Major * 1000000) + (mv.Minor * 1000) + mv.Release
77+
}
78+
79+
func (mv *MySQLVersion) ToString() string {
80+
return fmt.Sprintf("%d.%d.%d", mv.Major, mv.Minor, mv.Release)
81+
}
82+
83+
func (mv *MySQLVersion) Greater(vstring string) bool {
84+
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
85+
return mv.ToInt(tokens) > v.ToInt(tokens)
86+
}
87+
88+
func (mv *MySQLVersion) GreaterEqual(vstring string) bool {
89+
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
90+
return mv.ToInt(tokens) >= v.ToInt(tokens)
91+
}
92+
93+
func (mv *MySQLVersion) Lower(vstring string) bool {
94+
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
95+
return mv.ToInt(tokens) < v.ToInt(tokens)
96+
}
97+
98+
func (mv *MySQLVersion) LowerEqual(vstring string) bool {
99+
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
100+
return mv.ToInt(tokens) <= v.ToInt(tokens)
101+
}
102+
103+
func (mv *MySQLVersion) Equal(vstring string) bool {
104+
v, tokens := NewMySQLVersion(vstring, mv.Flavor)
105+
return mv.ToInt(tokens) == v.ToInt(tokens)
67106
}
68107

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

75112
func (mv *MySQLVersion) IsMySQL() bool {

0 commit comments

Comments
 (0)