Skip to content

Commit 59b0e56

Browse files
authored
Merge pull request #544 from afumagalli98/fix-mysql-version
Fixed mysql version
2 parents 5448058 + ae44b45 commit 59b0e56

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

builder/common_builder/mysql.go

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ package common
1717

1818
import (
1919
"regexp"
20-
"strconv"
2120
"strings"
2221

2322
"github.com/ercole-io/ercole/v2/model"
2423
ercutils "github.com/ercole-io/ercole/v2/utils"
2524
"github.com/hashicorp/go-multierror"
25+
"github.com/hashicorp/go-version"
2626
)
2727

2828
func (b *CommonBuilder) getMySQLFeature() (*model.MySQLFeature, error) {
@@ -46,37 +46,25 @@ func (b *CommonBuilder) getMySQLFeature() (*model.MySQLFeature, error) {
4646
continue
4747
}
4848

49-
isOld, err := isOldVersion(version)
49+
isold := b.isOldVersion(version)
5050

51-
if err != nil {
52-
b.log.Errorf("Can't verify if MySQL is an old version: %s", conf.Host)
53-
54-
merr = multierror.Append(merr, ercutils.NewError(err))
55-
56-
continue
57-
}
58-
59-
if !isOld {
60-
instance, errInstance = b.fetcher.GetMySQLInstance(conf)
51+
if isold {
52+
instance, errInstance = b.fetcher.GetMySQLOldInstance(conf)
6153
if errInstance != nil {
62-
b.log.Errorf("Can't get MySQL instance: %s", conf.Host)
54+
b.log.Errorf("Can't get MySQL old instance: %s", conf.Host)
6355

6456
merr = multierror.Append(merr, ercutils.NewError(err))
6557

6658
continue
6759
}
68-
} else {
69-
pattern := "enterprise"
70-
regex := regexp.MustCompile(pattern)
71-
match := regex.FindString(strings.ToLower(instance.Version))
7260

73-
if match != "" {
61+
if strings.Contains(strings.ToLower(version), "enterprise") {
7462
instance.Edition = model.MySQLEditionEnterprise
7563
}
76-
77-
instance, errInstance = b.fetcher.GetMySQLOldInstance(conf)
64+
} else {
65+
instance, errInstance = b.fetcher.GetMySQLInstance(conf)
7866
if errInstance != nil {
79-
b.log.Errorf("Can't get MySQL old instance: %s", conf.Host)
67+
b.log.Errorf("Can't get MySQL instance: %s", conf.Host)
8068

8169
merr = multierror.Append(merr, ercutils.NewError(err))
8270

@@ -146,34 +134,26 @@ func (b *CommonBuilder) getMySQLFeature() (*model.MySQLFeature, error) {
146134
return mysql, merr
147135
}
148136

149-
func isOldVersion(version string) (bool, error) {
150-
var ver1, ver2 = 0, 0
151-
152-
var err error
137+
func (b *CommonBuilder) isOldVersion(dbversion string) bool {
138+
re := regexp.MustCompile(`\b\d+(\.\d+)*\b`)
139+
matches := re.FindStringSubmatch(dbversion)
153140

154-
res := strings.Split(version, ".")
141+
if len(matches) == 0 {
142+
b.log.Error("cannot find matches in db version")
143+
return false
144+
}
155145

156-
for i, v := range res {
157-
if i == 0 {
158-
ver1, err = strconv.Atoi(v)
159-
if err != nil {
160-
return false, err
161-
}
162-
} else if i == 1 {
163-
ver2, err = strconv.Atoi(v)
164-
if err != nil {
165-
return false, err
166-
}
167-
} else {
168-
break
169-
}
146+
v, err := version.NewVersion(matches[0])
147+
if err != nil {
148+
b.log.Error(err)
149+
return false
170150
}
171151

172-
if ver1 == 5 && ver2 < 7 {
173-
return true, nil
174-
} else if ver1 < 5 {
175-
return true, nil
176-
} else {
177-
return false, nil
152+
constraints, err := version.NewConstraint("< 5.7")
153+
if err != nil {
154+
b.log.Error(err)
155+
return false
178156
}
157+
158+
return constraints.Check(v)
179159
}

0 commit comments

Comments
 (0)