use hashicorp/go-version to parse mysql server version
This commit is contained in:
parent
70d9fe8db3
commit
b578b60b88
|
@ -2,9 +2,9 @@ package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-version"
|
||||||
mysqlc "github.com/ziutek/mymysql/mysql"
|
mysqlc "github.com/ziutek/mymysql/mysql"
|
||||||
mysqlts "github.com/ziutek/mymysql/thrsafe"
|
mysqlts "github.com/ziutek/mymysql/thrsafe"
|
||||||
|
|
||||||
|
@ -13,10 +13,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type providerConfiguration struct {
|
type providerConfiguration struct {
|
||||||
Conn mysqlc.Conn
|
Conn mysqlc.Conn
|
||||||
VersionMajor uint
|
ServerVersion *version.Version
|
||||||
VersionMinor uint
|
|
||||||
VersionPatch uint
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Provider() terraform.ResourceProvider {
|
func Provider() terraform.ResourceProvider {
|
||||||
|
@ -88,16 +86,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
major, minor, patch, err := mysqlVersion(conn)
|
ver, err := serverVersion(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &providerConfiguration{
|
return &providerConfiguration{
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
VersionMajor: major,
|
ServerVersion: ver,
|
||||||
VersionMinor: minor,
|
|
||||||
VersionPatch: patch,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,36 +103,14 @@ func quoteIdentifier(in string) string {
|
||||||
return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in))
|
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()")
|
rows, _, err := conn.Query("SELECT VERSION()")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(rows) == 0 {
|
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)
|
return version.NewVersion(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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-version"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -69,15 +71,14 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error {
|
||||||
var stmtSQL string
|
var stmtSQL string
|
||||||
|
|
||||||
/* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */
|
/* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */
|
||||||
if conf.VersionMajor > 5 ||
|
ver, _ := version.NewVersion("5.7.6")
|
||||||
(conf.VersionMajor == 5 && conf.VersionMinor > 7) ||
|
if conf.ServerVersion.LessThan(ver) {
|
||||||
(conf.VersionMajor == 5 && conf.VersionMinor == 7 && conf.VersionPatch >= 6) {
|
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("user").(string),
|
||||||
d.Get("host").(string),
|
d.Get("host").(string),
|
||||||
newpw.(string))
|
newpw.(string))
|
||||||
} else {
|
} 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("user").(string),
|
||||||
d.Get("host").(string),
|
d.Get("host").(string),
|
||||||
newpw.(string))
|
newpw.(string))
|
||||||
|
|
Loading…
Reference in New Issue