Skip to content

Commit

Permalink
Merge pull request #547 from DGivney/main
Browse files Browse the repository at this point in the history
[Feat] Allow reuse of an existing db connection when creating a MySQL storage driver
  • Loading branch information
ReneWerner87 authored Oct 19, 2022
2 parents e8a33f9 + 1137fb9 commit 20ba96f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
17 changes: 15 additions & 2 deletions mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ store := mysql.New(mysql.Config{
})

// Initialize custom config using connection string
store := postgres.New(postgres.Config{
ConnectionURI: "mysql://user:password@localhost:3306/fiber"
store := mysql.New(mysql.Config{
ConnectionURI: "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>"
Reset: false,
GCInterval: 10 * time.Second,
})

// Initialize custom config using sql db connection
db, _ := sql.Open("mysql", "<username>:<pw>@tcp(<HOST>:<port>)/<dbname>")
store := mysql.New(mysql.Config{
Db: db,
Reset: false,
GCInterval: 10 * time.Second,
})
Expand All @@ -61,6 +69,11 @@ store := postgres.New(postgres.Config{
### Config
```go
type Config struct {
// DB Will override ConnectionURI and all other authentication values if used
//
// Optional. Default is nil
Db *sql.DB

// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
Expand Down
7 changes: 7 additions & 0 deletions mysql/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package mysql

import (
"database/sql"
"fmt"
"time"
)

// Config defines the config for storage.
type Config struct {
// DB Will override ConnectionURI and all other authentication values if used
//
// Optional. Default is nil
Db *sql.DB

// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
Expand Down Expand Up @@ -63,6 +69,7 @@ type Config struct {

// ConfigDefault is the default config
var ConfigDefault = Config{
Db: nil,
ConnectionURI: "",
Host: "127.0.0.1",
Port: 3306,
Expand Down
26 changes: 17 additions & 9 deletions mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,27 @@ var (

// New creates a new storage
func New(config ...Config) *Storage {
var err error
var db *sql.DB

// Set default config
cfg := configDefault(config...)

// Create db
db, err := sql.Open("mysql", cfg.dsn())
if err != nil {
panic(err)
}
if cfg.Db != nil {
// Use passed db
db = cfg.Db
} else {
// Create db
db, err = sql.Open("mysql", cfg.dsn())
if err != nil {
panic(err)
}

// Set options
db.SetMaxOpenConns(cfg.maxOpenConns)
db.SetMaxIdleConns(cfg.maxIdleConns)
db.SetConnMaxLifetime(cfg.connMaxLifetime)
// Set options
db.SetMaxOpenConns(cfg.maxOpenConns)
db.SetMaxIdleConns(cfg.maxIdleConns)
db.SetConnMaxLifetime(cfg.connMaxLifetime)
}

// Ping database to ensure a connection has been made
if err := db.Ping(); err != nil {
Expand Down
33 changes: 32 additions & 1 deletion mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package mysql

import (
"database/sql"
"fmt"
"os"
"testing"
"time"

"github.com/gofiber/utils"
"time"
)

var testStore = New(Config{
Expand All @@ -16,6 +17,36 @@ var testStore = New(Config{
Reset: true,
})

func Test_MYSQL_New(t *testing.T) {
newConfigStore := New(Config{
Database: os.Getenv("MYSQL_DATABASE"),
Username: os.Getenv("MYSQL_USERNAME"),
Password: os.Getenv("MYSQL_PASSWORD"),
Reset: true,
})

utils.AssertEqual(t, true, newConfigStore.db != nil)
newConfigStore.Close()

dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", os.Getenv("MYSQL_USERNAME"), os.Getenv("MYSQL_PASSWORD"), "127.0.0.1", 3306, os.Getenv("MYSQL_DATABASE"))
newConfigStore = New(Config{
ConnectionURI: dsn,
Reset: true,
})

utils.AssertEqual(t, true, newConfigStore.db != nil)
newConfigStore.Close()

db, _ := sql.Open("mysql", dsn)
newConfigStore = New(Config{
Db: db,
Reset: true,
})

utils.AssertEqual(t, true, newConfigStore.db != nil)
newConfigStore.Close()
}

func Test_MYSQL_Set(t *testing.T) {
var (
key = "john"
Expand Down

0 comments on commit 20ba96f

Please sign in to comment.