-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
41 lines (33 loc) · 877 Bytes
/
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
package main
import (
"log"
"os"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/ligurio/recidive/formats"
_ "github.com/mattn/go-sqlite3"
)
func initDb(dbpath string) *gorm.DB {
db, err := gorm.Open("sqlite3", dbpath)
db.AutoMigrate(&formats.Report{}, &formats.Suite{}, &formats.Test{})
if err != nil {
log.Println("gorm.Open failed")
return nil
}
if os.Getenv("DEBUG") == "true" {
log.Println("Debug mode enabled")
return db.Debug().LogMode(true)
}
if !db.HasTable(formats.Report{}) {
db.CreateTable(&formats.Report{})
}
if !db.HasTable(formats.Suite{}) {
db.CreateTable(&formats.Suite{})
}
db.Model(&formats.Report{}).Related(&formats.Suite{}, "ReportId")
if !db.HasTable(formats.Test{}) {
db.CreateTable(&formats.Test{})
}
db.Model(&formats.Suite{}).Related(&formats.Test{}, "SuiteId")
return db
}