use hashicorp/go-version to parse mysql server version

This commit is contained in:
protomouse 2016-08-31 10:50:08 +02:00
parent 70d9fe8db3
commit b578b60b88
2 changed files with 16 additions and 41 deletions

View File

@ -2,9 +2,9 @@ package mysql
import (
"fmt"
"strconv"
"strings"
"github.com/hashicorp/go-version"
mysqlc "github.com/ziutek/mymysql/mysql"
mysqlts "github.com/ziutek/mymysql/thrsafe"
@ -14,9 +14,7 @@ import (
type providerConfiguration struct {
Conn mysqlc.Conn
VersionMajor uint
VersionMinor uint
VersionPatch uint
ServerVersion *version.Version
}
func Provider() terraform.ResourceProvider {
@ -88,16 +86,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, err
}
major, minor, patch, err := mysqlVersion(conn)
ver, err := serverVersion(conn)
if err != nil {
return nil, err
}
return &providerConfiguration{
Conn: conn,
VersionMajor: major,
VersionMinor: minor,
VersionPatch: patch,
ServerVersion: ver,
}, nil
}
@ -107,36 +103,14 @@ func quoteIdentifier(in string) string {
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
}
func mysqlVersion(conn mysqlc.Conn) (uint, uint, uint, error) {
func serverVersion(conn mysqlc.Conn) (*version.Version, error) {
rows, _, err := conn.Query("SELECT VERSION()")
if err != nil {
return 0, 0, 0, err
return nil, err
}
if len(rows) == 0 {
return 0, 0, 0, fmt.Errorf("SELECT VERSION() returned an empty set")
return nil, fmt.Errorf("SELECT VERSION() returned an empty set")
}
versionString := rows[0].Str(0)
version := strings.Split(versionString, ".")
invalidVersionErr := fmt.Errorf("Invalid major.minor.patch in %q", versionString)
if len(version) != 3 {
return 0, 0, 0, invalidVersionErr
}
major, err := strconv.ParseUint(version[0], 10, 32)
if err != nil {
return 0, 0, 0, invalidVersionErr
}
minor, err := strconv.ParseUint(version[1], 10, 32)
if err != nil {
return 0, 0, 0, invalidVersionErr
}
patch, err := strconv.ParseUint(version[2], 10, 32)
if err != nil {
return 0, 0, 0, invalidVersionErr
}
return uint(major), uint(minor), uint(patch), nil
return version.NewVersion(rows[0].Str(0))
}

View File

@ -4,6 +4,8 @@ import (
"fmt"
"log"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/helper/schema"
)
@ -69,15 +71,14 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error {
var stmtSQL string
/* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */
if conf.VersionMajor > 5 ||
(conf.VersionMajor == 5 && conf.VersionMinor > 7) ||
(conf.VersionMajor == 5 && conf.VersionMinor == 7 && conf.VersionPatch >= 6) {
stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'",
ver, _ := version.NewVersion("5.7.6")
if conf.ServerVersion.LessThan(ver) {
stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')",
d.Get("user").(string),
d.Get("host").(string),
newpw.(string))
} else {
stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')",
stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'",
d.Get("user").(string),
d.Get("host").(string),
newpw.(string))