This repository has been archived by the owner on Apr 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
db.go
51 lines (44 loc) · 1.39 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package common
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"os"
"strings"
)
// Connection string parameters for Postgres - http://godoc.org/github.com/lib/pq, if you are using another
// database refer to the relevant driver's documentation.
// * dbname - The name of the database to connect to
// * user - The user to sign in as
// * password - The user's password
// * host - The host to connect to. Values that start with / are for unix domain sockets.
// (default is localhost)
// * port - The port to bind to. (default is 5432)
// * sslmode - Whether or not to use SSL (default is require, this is not the default for libpq)
// Valid SSL modes:
// * disable - No SSL
// * require - Always SSL (skip verification)
// * verify-full - Always SSL (require verification)
var DB gorm.DB
func init() {
var err error
cmd := os.Args[0]
if strings.Contains(cmd, "test") {
// We are doing testing!
DB, err = gorm.Open("sqlite3", ":memory:")
fmt.Println("TEST")
DB.AutoMigrate(Organization{})
DB.AutoMigrate(Repository{})
DB.AutoMigrate(Commit{})
DB.AutoMigrate(User{})
DB.AutoMigrate(Pull{})
DB.AutoMigrate(CommitOrgStats{})
DB.AutoMigrate(RepoStat{})
} else {
DB, err = gorm.Open("postgres", os.Getenv("PG_CONN_STR"))
}
if err != nil {
panic(fmt.Sprintf("Got error when connect database, the error is '%v'", err))
}
}