Skip to content

Commit

Permalink
Make test pass - cache connections
Browse files Browse the repository at this point in the history
Reorder grant-revoke, escape db name, reduce conns.

Without connection caching, program goes crazy and utilizes everything
it can - that results in failed test.
  • Loading branch information
petoju committed Feb 22, 2021
1 parent b99d216 commit 6d9c758
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test: fmtcheck
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc: fmtcheck
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout=30s

vet:
@echo "go vet ."
Expand Down
21 changes: 21 additions & 0 deletions mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"regexp"
"strings"
"sync"
"time"

"github.com/go-sql-driver/mysql"
Expand All @@ -33,6 +34,18 @@ type MySQLConfiguration struct {
ConnectRetryTimeoutSec time.Duration
}

var (
connectionCacheMtx sync.Mutex
connectionCache map[string]*sql.DB
)

func init() {
connectionCacheMtx.Lock()
defer connectionCacheMtx.Unlock()

connectionCache = map[string]*sql.DB{}
}

func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -234,8 +247,15 @@ func serverVersionString(db *sql.DB) (string, error) {
}

func connectToMySQL(conf *MySQLConfiguration) (*sql.DB, error) {
// This is fine - we'll connect serially, but we don't expect more than
// 1 or 2 connections at once.
connectionCacheMtx.Lock()
defer connectionCacheMtx.Unlock()

dsn := conf.Config.FormatDSN()
if connectionCache[dsn] != nil {
return connectionCache[dsn], nil
}
var db *sql.DB
var err error

Expand All @@ -260,6 +280,7 @@ func connectToMySQL(conf *MySQLConfiguration) (*sql.DB, error) {
if retryError != nil {
return nil, fmt.Errorf("Could not connect to server: %s", retryError)
}
connectionCache[dsn] = db
db.SetConnMaxLifetime(conf.MaxConnLifetime)
db.SetMaxOpenConns(conf.MaxOpenConns)
return db, nil
Expand Down
22 changes: 11 additions & 11 deletions mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ func updatePrivileges(d *schema.ResourceData, db *sql.DB, user string, database
grantIfs := newPrivs.Difference(oldPrivs).List()
revokeIfs := oldPrivs.Difference(newPrivs).List()

if len(grantIfs) > 0 {
grants := make([]string, len(grantIfs))
if len(revokeIfs) > 0 {
revokes := make([]string, len(revokeIfs))

for i, v := range grantIfs {
grants[i] = v.(string)
for i, v := range revokeIfs {
revokes[i] = v.(string)
}

sql := fmt.Sprintf("GRANT %s ON %s.%s TO %s", strings.Join(grants, ","), database, table, user)
sql := fmt.Sprintf("REVOKE %s ON %s.%s FROM %s", strings.Join(revokes, ","), formatDatabaseName(database), formatTableName(table), user)

log.Printf("[DEBUG] SQL: %s", sql)

Expand All @@ -330,14 +330,14 @@ func updatePrivileges(d *schema.ResourceData, db *sql.DB, user string, database
}
}

if len(revokeIfs) > 0 {
revokes := make([]string, len(revokeIfs))
if len(grantIfs) > 0 {
grants := make([]string, len(grantIfs))

for i, v := range revokeIfs {
revokes[i] = v.(string)
for i, v := range grantIfs {
grants[i] = v.(string)
}

sql := fmt.Sprintf("REVOKE %s ON %s.%s FROM %s", strings.Join(revokes, ","), database, table, user)
sql := fmt.Sprintf("GRANT %s ON %s.%s TO %s", strings.Join(grants, ","), formatDatabaseName(database), formatTableName(table), user)

log.Printf("[DEBUG] SQL: %s", sql)

Expand Down Expand Up @@ -493,7 +493,7 @@ func showGrants(db *sql.DB, user string) ([]*MySQLGrant, error) {
}

privsStr := m[1]
priv_list := strings.Split(privsStr, ",")
priv_list := extractPermTypes(privsStr)
privileges := make([]string, len(priv_list))

for i, priv := range priv_list {
Expand Down

0 comments on commit 6d9c758

Please sign in to comment.