From 06a9bfebfc5a3cb50ceae85e698d2be4948b23c2 Mon Sep 17 00:00:00 2001 From: Hao Date: Sat, 18 Mar 2017 06:15:01 -0700 Subject: [PATCH] MySQL Provider does not recreate user if user got deleted manually in MySQL (#12791) Issue No. #12767 --- builtin/providers/mysql/resource_grant.go | 13 ++++++++++++- builtin/providers/mysql/resource_user.go | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/builtin/providers/mysql/resource_grant.go b/builtin/providers/mysql/resource_grant.go index 5513628850..0414fe4418 100644 --- a/builtin/providers/mysql/resource_grant.go +++ b/builtin/providers/mysql/resource_grant.go @@ -88,7 +88,18 @@ func CreateGrant(d *schema.ResourceData, meta interface{}) error { } func ReadGrant(d *schema.ResourceData, meta interface{}) error { - // At this time, all attributes are supplied by the user + conn := meta.(*providerConfiguration).Conn + + stmtSQL := fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", + d.Get("user").(string), + d.Get("host").(string)) + + log.Println("Executing statement:", stmtSQL) + + _, _, err := conn.Query(stmtSQL) + if err != nil { + d.SetId("") + } return nil } diff --git a/builtin/providers/mysql/resource_user.go b/builtin/providers/mysql/resource_user.go index 7cdf5b8123..ce9bec1186 100644 --- a/builtin/providers/mysql/resource_user.go +++ b/builtin/providers/mysql/resource_user.go @@ -95,7 +95,21 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error { } func ReadUser(d *schema.ResourceData, meta interface{}) error { - // At this time, all attributes are supplied by the user + conn := meta.(*providerConfiguration).Conn + + stmtSQL := fmt.Sprintf("SELECT USER FROM mysql.user WHERE USER='%s'", + d.Get("user").(string)) + + log.Println("Executing statement:", stmtSQL) + + rows, _, err := conn.Query(stmtSQL) + log.Println("Returned rows:", len(rows)) + if err != nil { + return err + } + if len(rows) == 0 { + d.SetId("") + } return nil }