/
database.go
54 lines (44 loc) · 1.15 KB
/
database.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
package testutil
import (
"fmt"
"log"
"math/rand"
"os"
"time"
"app/api/migration"
"app/api/pkg/database"
"app/api/pkg/logger"
"github.com/josephspurrier/rove/pkg/adapter/mysql"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// LoadDatabase will set up the DB and apply migrations for the tests.
func LoadDatabase(ml logger.ILog) *database.DBW {
unique := "T" + fmt.Sprint(rand.Intn(999999999))
password := "password"
if os.Getenv("TRAVIS") == "true" {
password = ""
}
// Set the database connection information.
con := &mysql.Connection{
Hostname: "127.0.0.1",
Username: "root",
Password: password,
Name: "gomithriltsapptest" + unique,
Port: 3306,
Parameter: "parseTime=true&allowNativePasswords=true&collation=utf8mb4_unicode_ci&multiStatements=true",
}
db, err := database.Migrate(ml, con, migration.Changesets)
if err != nil {
log.Fatalln(err.Error())
}
return database.New(db, con.Name)
}
// TeardownDatabase will destroy the test database.
func TeardownDatabase(db *database.DBW) {
_, err := db.Exec(`DROP DATABASE IF EXISTS ` + db.Name())
if err != nil {
fmt.Println("DB DROP TEARDOWN Error:", err)
}
}