-
Notifications
You must be signed in to change notification settings - Fork 76
/
db.go
47 lines (38 loc) · 957 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
42
43
44
45
46
47
package ovpm
import (
"github.com/sirupsen/logrus"
"github.com/jinzhu/gorm"
// We blank import sqlite here because gorm needs it.
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var db *DB
// DB represents a persistent storage.
type DB struct {
*gorm.DB
}
// CreateDB prepares and returns new storage.
//
// It should be run at the start of the program.
func CreateDB(dialect string, args ...interface{}) *DB {
if len(args) > 0 && args[0] == "" {
args[0] = _DefaultDBPath
}
var err error
dbase, err := gorm.Open(dialect, args...)
if err != nil {
logrus.Fatalf("couldn't open sqlite database %v: %v", args, err)
}
dbase.AutoMigrate(&dbUserModel{})
dbase.AutoMigrate(&dbServerModel{})
dbase.AutoMigrate(&dbRevokedModel{})
dbase.AutoMigrate(&dbNetworkModel{})
dbPTR := &DB{DB: dbase}
db = dbPTR
return dbPTR
}
// Cease closes the database.
//
// It should be run at the exit of the program.
func (db *DB) Cease() {
db.DB.Close()
}