-
Notifications
You must be signed in to change notification settings - Fork 22
/
mysql.go
79 lines (64 loc) · 1.82 KB
/
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
package test_helpers
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
. "github.com/onsi/gomega"
"os"
)
func DbSetup(tableName string) {
var mysqlUsername = os.Getenv("MYSQL_USERNAME")
var mysqlPassword = os.Getenv("MYSQL_PASSWORD")
pxcConnectionString := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/",
mysqlUsername,
mysqlPassword,
DbHost(),
3306)
databaseConnection, err := sql.Open("mysql", pxcConnectionString)
Expect(err).NotTo(HaveOccurred())
statement := "CREATE DATABASE IF NOT EXISTS pxc_release_test_db"
_, err = databaseConnection.Exec(statement)
Expect(err).NotTo(HaveOccurred())
statement = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (test_data varchar(255) PRIMARY KEY)", tableName)
_, err = DbConn().Exec(statement)
Expect(err).NotTo(HaveOccurred())
}
func DbConnNoDb() *sql.DB {
var mysqlUsername = os.Getenv("MYSQL_USERNAME")
var mysqlPassword = os.Getenv("MYSQL_PASSWORD")
pxcConnectionString := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/",
mysqlUsername,
mysqlPassword,
DbHost(),
3306)
databaseConnection, err := sql.Open("mysql", pxcConnectionString)
Expect(err).NotTo(HaveOccurred())
return databaseConnection
}
func DbConn() *sql.DB {
var mysqlUsername = os.Getenv("MYSQL_USERNAME")
var mysqlPassword = os.Getenv("MYSQL_PASSWORD")
pxcConnectionString := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/pxc_release_test_db",
mysqlUsername,
mysqlPassword,
DbHost(),
3306)
databaseConnection, err := sql.Open("mysql", pxcConnectionString)
Expect(err).NotTo(HaveOccurred())
return databaseConnection
}
func DbCleanup() {
statement := "DROP DATABASE pxc_release_test_db"
_, err := DbConn().Exec(statement)
Expect(err).NotTo(HaveOccurred())
}
func DbHost() string {
dbHost, hostExists := os.LookupEnv("MYSQL_HOST")
if hostExists {
return dbHost
}
return os.Getenv("BOSH_ENVIRONMENT")
}