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 #16 from winebarrel/share_connection
Browse files Browse the repository at this point in the history
Share DB connection
  • Loading branch information
winebarrel authored Mar 24, 2020
2 parents 047ba34 + 832db34 commit e1bc8fd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 86 deletions.
6 changes: 1 addition & 5 deletions mysql/data_source_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ func dataSourceTables() *schema.Resource {
}

func ShowTables(d *schema.ResourceData, meta interface{}) error {
db, err := connectToMySQL(meta.(*MySQLConfiguration))

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

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

type MySQLConfiguration struct {
Config *mysql.Config
Db *sql.DB
MaxConnLifetime time.Duration
MaxOpenConns int
ConnectRetryTimeoutSec time.Duration
Expand Down Expand Up @@ -150,12 +151,22 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return dialer.Dial("tcp", network)
})

return &MySQLConfiguration{
mysqlConf := &MySQLConfiguration{
Config: &conf,
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,
}, nil
}

db, err := connectToMySQL(mysqlConf)

if err != nil {
return nil, err
}

mysqlConf.Db = db

return mysqlConf, nil
}

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

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

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

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

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

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

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

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

// 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 @@ -98,7 +89,7 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {

log.Println("Executing query:", 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 @@ -155,16 +146,13 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
}

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

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
21 changes: 4 additions & 17 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ func supportsRoles(db *sql.DB) (bool, error) {
}

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

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

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

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

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

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

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

db, err := connectToMySQL(meta.(*MySQLConfiguration))

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

sql := fmt.Sprintf("SHOW GRANTS FOR '%s'@'%s'", user, host)
rows, err := db.Query(sql)
Expand Down
21 changes: 6 additions & 15 deletions mysql/resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ func resourceRole() *schema.Resource {
}

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

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

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

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

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

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

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

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

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

_, err = db.Exec(sql)
_, err := db.Exec(sql)
if err != nil {
return err
}
Expand Down
30 changes: 7 additions & 23 deletions mysql/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ func resourceUser() *schema.Resource {
}

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

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

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

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

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

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

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

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 @@ -261,14 +249,10 @@ func ImportUser(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceDat
user := userHost[0]
host := userHost[1]

db, err := connectToMySQL(meta.(*MySQLConfiguration))

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

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: 1 addition & 4 deletions mysql/resource_user_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ func resourceUserPassword() *schema.Resource {
}

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

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

0 comments on commit e1bc8fd

Please sign in to comment.