-
Notifications
You must be signed in to change notification settings - Fork 0
/
conex_mysql.go
112 lines (89 loc) · 2.36 KB
/
conex_mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package mysql
import (
"database/sql"
"fmt"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/omeid/conex"
)
var (
// Image to use for the box.
Image = "mysql:latest"
// Port used for connect to MySQL.
Port = "3306"
// MySQLUpWaitTime dictates how long we should wait for post MySQL to accept connections on {{Port}}.
MySQLUpWaitTime = 15 * time.Second
)
func init() {
conex.Require(func() string { return Image })
}
// Config used to connect to the database.
type Config struct {
User string // leave blank for root, otherwise provide a password
Password string // can be blank for root user
Database string // defaults to `test`
host string
port string
}
// [username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
func (c *Config) url() string {
pass := ""
if c.Password != "" {
pass = ":" + c.Password
}
return fmt.Sprintf(
"%s%s@tcp(%s:%s)/%s?autocommit=true",
c.User, pass, c.host, c.port, c.Database,
)
}
// set env variables, see https://hub.docker.com/r/library/mysql/
func (c *Config) env() []string {
env := []string{}
// set up user/password access
if c.User == "" {
c.User = "root"
}
if c.User == "root" && c.Password == "" {
env = append(env, fmt.Sprintf("MYSQL_ALLOW_EMPTY_PASSWORD=yes"))
} else {
env = append(env, fmt.Sprintf("MYSQL_PASSWORD=%s", c.Password))
}
if c.User != "" && c.User != "root" {
if c.Password == "" {
panic("MySQL requires both user and password when a non-root user is specified.")
}
env = append(env, fmt.Sprintf("MYSQL_USER=%s", c.User))
env = append(env, fmt.Sprintf("MYSQL_PASSWORD=%s", c.Password))
}
// create a database or default to "test"
dbName := c.Database
if dbName == "" {
dbName = "test"
}
env = append(env, fmt.Sprintf("MYSQL_DATABASE=%s", dbName))
return env
}
// Box returns a MySQL client.
func Box(t testing.TB, config *Config) (*sql.DB, conex.Container) {
c := conex.Box(t, &conex.Config{
Image: Image,
Env: config.env(),
Expose: []string{Port},
})
config.host = c.Address()
config.port = Port
t.Logf("Waiting for MySQL to accept connections")
err := c.Wait(Port, MySQLUpWaitTime)
if err != nil {
c.Drop()
t.Fatal("MySQL failed to start.", err)
}
t.Log("MySQL is now accepting connections")
db, err := sql.Open("mysql", config.url())
if err != nil {
c.Drop()
t.Fatal(err)
}
return db, c
}