Skip to content

Commit b578b60

Browse files
author
protomouse
committed
use hashicorp/go-version to parse mysql server version
1 parent 70d9fe8 commit b578b60

2 files changed

Lines changed: 16 additions & 41 deletions

File tree

builtin/providers/mysql/provider.go

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package mysql
22

33
import (
44
"fmt"
5-
"strconv"
65
"strings"
76

7+
"github.com/hashicorp/go-version"
88
mysqlc "github.com/ziutek/mymysql/mysql"
99
mysqlts "github.com/ziutek/mymysql/thrsafe"
1010

@@ -13,10 +13,8 @@ import (
1313
)
1414

1515
type providerConfiguration struct {
16-
Conn mysqlc.Conn
17-
VersionMajor uint
18-
VersionMinor uint
19-
VersionPatch uint
16+
Conn mysqlc.Conn
17+
ServerVersion *version.Version
2018
}
2119

2220
func Provider() terraform.ResourceProvider {
@@ -88,16 +86,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
8886
return nil, err
8987
}
9088

91-
major, minor, patch, err := mysqlVersion(conn)
89+
ver, err := serverVersion(conn)
9290
if err != nil {
9391
return nil, err
9492
}
9593

9694
return &providerConfiguration{
97-
Conn: conn,
98-
VersionMajor: major,
99-
VersionMinor: minor,
100-
VersionPatch: patch,
95+
Conn: conn,
96+
ServerVersion: ver,
10197
}, nil
10298
}
10399

@@ -107,36 +103,14 @@ func quoteIdentifier(in string) string {
107103
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
108104
}
109105

110-
func mysqlVersion(conn mysqlc.Conn) (uint, uint, uint, error) {
106+
func serverVersion(conn mysqlc.Conn) (*version.Version, error) {
111107
rows, _, err := conn.Query("SELECT VERSION()")
112108
if err != nil {
113-
return 0, 0, 0, err
109+
return nil, err
114110
}
115111
if len(rows) == 0 {
116-
return 0, 0, 0, fmt.Errorf("SELECT VERSION() returned an empty set")
117-
}
118-
119-
versionString := rows[0].Str(0)
120-
version := strings.Split(versionString, ".")
121-
invalidVersionErr := fmt.Errorf("Invalid major.minor.patch in %q", versionString)
122-
if len(version) != 3 {
123-
return 0, 0, 0, invalidVersionErr
124-
}
125-
126-
major, err := strconv.ParseUint(version[0], 10, 32)
127-
if err != nil {
128-
return 0, 0, 0, invalidVersionErr
129-
}
130-
131-
minor, err := strconv.ParseUint(version[1], 10, 32)
132-
if err != nil {
133-
return 0, 0, 0, invalidVersionErr
134-
}
135-
136-
patch, err := strconv.ParseUint(version[2], 10, 32)
137-
if err != nil {
138-
return 0, 0, 0, invalidVersionErr
112+
return nil, fmt.Errorf("SELECT VERSION() returned an empty set")
139113
}
140114

141-
return uint(major), uint(minor), uint(patch), nil
115+
return version.NewVersion(rows[0].Str(0))
142116
}

builtin/providers/mysql/resource_user.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"log"
66

7+
"github.com/hashicorp/go-version"
8+
79
"github.com/hashicorp/terraform/helper/schema"
810
)
911

@@ -69,15 +71,14 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error {
6971
var stmtSQL string
7072

7173
/* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */
72-
if conf.VersionMajor > 5 ||
73-
(conf.VersionMajor == 5 && conf.VersionMinor > 7) ||
74-
(conf.VersionMajor == 5 && conf.VersionMinor == 7 && conf.VersionPatch >= 6) {
75-
stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'",
74+
ver, _ := version.NewVersion("5.7.6")
75+
if conf.ServerVersion.LessThan(ver) {
76+
stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')",
7677
d.Get("user").(string),
7778
d.Get("host").(string),
7879
newpw.(string))
7980
} else {
80-
stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')",
81+
stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'",
8182
d.Get("user").(string),
8283
d.Get("host").(string),
8384
newpw.(string))

0 commit comments

Comments
 (0)