From 3630c16852574a2f8c70c4ab84803497f13af2c5 Mon Sep 17 00:00:00 2001 From: Armaan Tobaccowalla Date: Wed, 26 Feb 2020 15:37:05 -0500 Subject: [PATCH 1/2] Use ALTER_USER in user_password --- mysql/resource_user_password.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/mysql/resource_user_password.go b/mysql/resource_user_password.go index 077136ba..0efad56d 100644 --- a/mysql/resource_user_password.go +++ b/mysql/resource_user_password.go @@ -2,6 +2,7 @@ package mysql import ( "fmt" + "log" "github.com/gofrs/uuid" "github.com/hashicorp/go-version" @@ -67,23 +68,24 @@ func SetUserPassword(d *schema.ResourceData, meta interface{}) error { d.Set("key_fingerprint", fingerprint) d.Set("encrypted_password", encrypted) - requiredVersion, _ := version.NewVersion("8.0.0") - currentVersion, err := serverVersion(db) - if err != nil { - return err - } - - passSQL := fmt.Sprintf("'%s'", password) - if currentVersion.LessThan(requiredVersion) { - passSQL = fmt.Sprintf("PASSWORD(%s)", passSQL) + /* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */ + serverVersion, err := serverVersion(db) + ver, _ := version.NewVersion("5.7.6") + var stmtSQL string + if serverVersion.LessThan(ver) { + stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')", + d.Get("user").(string), + d.Get("host").(string), + password) + } else { + stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", + d.Get("user").(string), + d.Get("host").(string), + password) } - sql := fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = %s", - d.Get("user").(string), - d.Get("host").(string), - passSQL) - - _, err = db.Exec(sql) + log.Println("Executing query:", stmtSQL) + _, err = db.Exec(stmtSQL) if err != nil { return err } From fe318293e765dbf6c407991301531c1ec2e0c0f9 Mon Sep 17 00:00:00 2001 From: Armaan Tobaccowalla Date: Wed, 26 Feb 2020 15:43:14 -0500 Subject: [PATCH 2/2] Error check --- mysql/resource_user_password.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mysql/resource_user_password.go b/mysql/resource_user_password.go index 0efad56d..2ae82e65 100644 --- a/mysql/resource_user_password.go +++ b/mysql/resource_user_password.go @@ -70,6 +70,9 @@ func SetUserPassword(d *schema.ResourceData, meta interface{}) error { /* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */ serverVersion, err := serverVersion(db) + if err != nil { + return fmt.Errorf("Could not determine server version: %s", err) + } ver, _ := version.NewVersion("5.7.6") var stmtSQL string if serverVersion.LessThan(ver) {