Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -67,23 +68,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