-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_db.go
36 lines (28 loc) · 813 Bytes
/
test_db.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
package utils
import (
"fmt"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
)
func TestDB() *gorm.DB {
dbuser, dbpwd, dbname := "qor", "qor", "qor_test"
if os.Getenv("TEST_ENV") == "CI" {
dbuser, dbpwd = os.Getenv("DB_USER"), os.Getenv("DB_PWD")
}
var db gorm.DB
var err error
if os.Getenv("TEST_DB") == "postgres" {
db, err = gorm.Open("postgres", fmt.Sprintf("postgres://%s:%s@localhost/%s?sslmode=disable", dbuser, dbpwd, dbname))
} else {
// CREATE USER 'qor'@'localhost' IDENTIFIED BY 'qor';
// CREATE DATABASE qor_test;
// GRANT ALL ON qor_test.* TO 'qor'@'localhost';
db, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@/%s?charset=utf8&parseTime=True&loc=Local", dbuser, dbpwd, dbname))
}
if err != nil {
panic(err)
}
return &db
}