Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #27 from ArmaanT/fix/user_password
Browse files Browse the repository at this point in the history
Update logic in resource_user_password
  • Loading branch information
winebarrel authored Apr 11, 2021
2 parents 25a64c3 + fe31829 commit 853012c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions mysql/resource_user_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysql

import (
"fmt"
"log"

"github.com/gofrs/uuid"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -64,23 +65,27 @@ 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)
/* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */
serverVersion, err := serverVersion(db)
if err != nil {
return err
return fmt.Errorf("Could not determine server version: %s", err)
}

passSQL := fmt.Sprintf("'%s'", password)
if currentVersion.LessThan(requiredVersion) {
passSQL = fmt.Sprintf("PASSWORD(%s)", passSQL)
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
}
Expand Down

0 comments on commit 853012c

Please sign in to comment.