-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
55 lines (44 loc) · 1019 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
48
49
50
51
52
53
54
55
package main
import (
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func (t *MirageTool) initDB() error {
db, err := t.openDB()
if err != nil {
return err
}
t.db = db
err = db.AutoMigrate(&User{})
if err != nil {
return err
}
return err
}
func (t *MirageTool) openDB() (*gorm.DB, error) {
var db *gorm.DB
var err error
var log logger.Interface
log = logger.Default.LogMode(logger.Silent)
db, err = gorm.Open(
sqlite.Open(t.cfg.DB.Path+"?_synchronous=1&_journal_mode=WAL"),
&gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: log,
},
)
db.Exec("PRAGMA foreign_keys=ON")
// The pure Go SQLite library does not handle locking in
// the same way as the C based one and we cant use the gorm
// connection pool as of 2022/02/23.
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(1)
sqlDB.SetMaxOpenConns(1)
sqlDB.SetConnMaxIdleTime(time.Hour)
if err != nil {
return nil, err
}
return db, nil
}