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 #33 from socotra/lazy-db-conn
Browse files Browse the repository at this point in the history
Acquire DB connection lazily.
  • Loading branch information
winebarrel authored Apr 22, 2021
2 parents f774198 + c5a3aad commit 298d149
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 41 deletions.
5 changes: 4 additions & 1 deletion mysql/data_source_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func dataSourceTables() *schema.Resource {
}

func ShowTables(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

database := d.Get("database").(string)
pattern := d.Get("pattern").(string)
Expand Down
22 changes: 13 additions & 9 deletions mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@ const (

type MySQLConfiguration struct {
Config *mysql.Config
Db *sql.DB
MaxConnLifetime time.Duration
MaxOpenConns int
ConnectRetryTimeoutSec time.Duration
db *sql.DB
}

func (c *MySQLConfiguration) GetDbConn() (*sql.DB, error) {
if c.db == nil {
db, err := connectToMySQL(c)
if err != nil {
return nil, err
}
c.db = db
}
return c.db, nil
}

func Provider() terraform.ResourceProvider {
Expand Down Expand Up @@ -157,16 +168,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
MaxConnLifetime: time.Duration(d.Get("max_conn_lifetime_sec").(int)) * time.Second,
MaxOpenConns: d.Get("max_open_conns").(int),
ConnectRetryTimeoutSec: time.Duration(d.Get("connect_retry_timeout_sec").(int)) * time.Second,
db: nil,
}

db, err := connectToMySQL(mysqlConf)

if err != nil {
return nil, err
}

mysqlConf.Db = db

return mysqlConf, nil
}

Expand Down
28 changes: 20 additions & 8 deletions mysql/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ func resourceDatabase() *schema.Resource {
}

func CreateDatabase(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

stmtSQL := databaseConfigSQL("CREATE", d)
log.Println("Executing statement:", stmtSQL)

_, err := db.Exec(stmtSQL)
_, err = db.Exec(stmtSQL)
if err != nil {
return err
}
Expand All @@ -63,12 +66,15 @@ func CreateDatabase(d *schema.ResourceData, meta interface{}) error {
}

func UpdateDatabase(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

stmtSQL := databaseConfigSQL("ALTER", d)
log.Println("Executing statement:", stmtSQL)

_, err := db.Exec(stmtSQL)
_, err = db.Exec(stmtSQL)
if err != nil {
return err
}
Expand All @@ -77,7 +83,10 @@ func UpdateDatabase(d *schema.ResourceData, meta interface{}) error {
}

func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

// This is kinda flimsy-feeling, since it depends on the formatting
// of the SHOW CREATE DATABASE output... but this data doesn't seem
Expand All @@ -89,7 +98,7 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {

log.Println("Executing statement:", stmtSQL)
var createSQL, _database string
err := db.QueryRow(stmtSQL).Scan(&_database, &createSQL)
err = db.QueryRow(stmtSQL).Scan(&_database, &createSQL)
if err != nil {
if mysqlErr, ok := err.(*mysql.MySQLError); ok {
if mysqlErr.Number == unknownDatabaseErrCode {
Expand Down Expand Up @@ -146,13 +155,16 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
}

func DeleteDatabase(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

name := d.Id()
stmtSQL := "DROP DATABASE " + quoteIdentifier(name)
log.Println("Executing statement:", stmtSQL)

_, err := db.Exec(stmtSQL)
_, err = db.Exec(stmtSQL)
if err == nil {
d.SetId("")
}
Expand Down
25 changes: 20 additions & 5 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ func supportsRoles(db *sql.DB) (bool, error) {
}

func CreateGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

hasRoles, err := supportsRoles(db)
if err != nil {
Expand Down Expand Up @@ -229,7 +232,10 @@ func CreateGrant(d *schema.ResourceData, meta interface{}) error {
}

func ReadGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

hasRoles, err := supportsRoles(db)
if err != nil {
Expand Down Expand Up @@ -276,7 +282,10 @@ func ReadGrant(d *schema.ResourceData, meta interface{}) error {
}

func UpdateGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

hasRoles, err := supportsRoles(db)

Expand Down Expand Up @@ -351,7 +360,10 @@ func updatePrivileges(d *schema.ResourceData, db *sql.DB, user string, database
}

func DeleteGrant(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

database := formatDatabaseName(d.Get("database").(string))

Expand Down Expand Up @@ -422,7 +434,10 @@ func ImportGrant(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceDa
user := userHost[0]
host := userHost[1]

db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return nil, err
}

grants, err := showGrants(db, fmt.Sprintf("'%s'@'%s'", user, host))

Expand Down
21 changes: 15 additions & 6 deletions mysql/resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ func resourceRole() *schema.Resource {
}

func CreateRole(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

roleName := d.Get("name").(string)

stmtSQL := fmt.Sprintf("CREATE ROLE '%s'", roleName)
log.Printf("[DEBUG] SQL: %s", stmtSQL)

_, err := db.Exec(stmtSQL)
_, err = db.Exec(stmtSQL)
if err != nil {
return fmt.Errorf("error creating role: %s", err)
}
Expand All @@ -42,12 +45,15 @@ func CreateRole(d *schema.ResourceData, meta interface{}) error {
}

func ReadRole(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

stmtSQL := fmt.Sprintf("SHOW GRANTS FOR '%s'", d.Id())
log.Printf("[DEBUG] SQL: %s", stmtSQL)

_, err := db.Exec(stmtSQL)
_, err = db.Exec(stmtSQL)
if err != nil {
log.Printf("[WARN] Role (%s) not found; removing from state", d.Id())
d.SetId("")
Expand All @@ -60,12 +66,15 @@ func ReadRole(d *schema.ResourceData, meta interface{}) error {
}

func DeleteRole(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

stmtSQL := fmt.Sprintf("DROP ROLE '%s'", d.Get("name").(string))
log.Printf("[DEBUG] SQL: %s", stmtSQL)

_, err := db.Exec(stmtSQL)
_, err = db.Exec(stmtSQL)
if err != nil {
return err
}
Expand Down
14 changes: 10 additions & 4 deletions mysql/resource_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ func resourceSql() *schema.Resource {
}

func CreateSql(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}
name := d.Get("name").(string)
create_sql := d.Get("create_sql").(string)

log.Println("Executing SQL", create_sql)

_, err := db.Exec(create_sql)
_, err = db.Exec(create_sql)

if err != nil {
return err
Expand All @@ -55,12 +58,15 @@ func ReadSql(d *schema.ResourceData, meta interface{}) error {
}

func DeleteSql(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}
delete_sql := d.Get("delete_sql").(string)

log.Println("Executing SQL:", delete_sql)

_, err := db.Exec(delete_sql)
_, err = db.Exec(delete_sql)

if err == nil {
d.SetId("")
Expand Down
29 changes: 22 additions & 7 deletions mysql/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func resourceUser() *schema.Resource {
}

func CreateUser(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

var authStm string
var auth string
Expand Down Expand Up @@ -129,7 +132,10 @@ func CreateUser(d *schema.ResourceData, meta interface{}) error {
}

func UpdateUser(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

var auth string
if v, ok := d.GetOk("auth_plugin"); ok {
Expand Down Expand Up @@ -204,7 +210,10 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error {
}

func ReadUser(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

stmtSQL := fmt.Sprintf("SELECT USER FROM mysql.user WHERE USER='%s'",
d.Get("user").(string))
Expand All @@ -224,15 +233,18 @@ func ReadUser(d *schema.ResourceData, meta interface{}) error {
}

func DeleteUser(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

stmtSQL := fmt.Sprintf("DROP USER '%s'@'%s'",
d.Get("user").(string),
d.Get("host").(string))

log.Println("Executing statement:", stmtSQL)

_, err := db.Exec(stmtSQL)
_, err = db.Exec(stmtSQL)
if err == nil {
d.SetId("")
}
Expand All @@ -249,10 +261,13 @@ func ImportUser(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceDat
user := userHost[0]
host := userHost[1]

db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return nil, err
}

var count int
err := db.QueryRow("SELECT COUNT(1) FROM mysql.user WHERE user = ? AND host = ?", user, host).Scan(&count)
err = db.QueryRow("SELECT COUNT(1) FROM mysql.user WHERE user = ? AND host = ?", user, host).Scan(&count)

if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion mysql/resource_user_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func resourceUserPassword() *schema.Resource {
}

func SetUserPassword(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
db, err := meta.(*MySQLConfiguration).GetDbConn()
if err != nil {
return err
}

uuid, err := uuid.NewV4()
if err != nil {
Expand Down

0 comments on commit 298d149

Please sign in to comment.