-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
60 lines (47 loc) · 1.09 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
52
53
54
55
56
57
58
59
60
package streamer
import (
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func (s *GitLabLogStreamer) initDB() error {
db, err := s.openDB()
if err != nil {
return err
}
s.db = db
return nil
}
func (s *GitLabLogStreamer) openDB() (*gorm.DB, error) {
var db *gorm.DB
var err error
gormLogger := logger.Default.LogMode(logger.Silent)
db, err = gorm.Open(
sqlite.Open(s.cfg.DBpath+"?_synchronous=1&_journal_mode=WAL"),
&gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: gormLogger,
},
)
if err != nil {
return nil, err
}
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)
err = db.AutoMigrate(&AuditEvent{})
if err != nil {
return nil, err
}
err = db.AutoMigrate(&AuthEvent{})
if err != nil {
return nil, err
}
return db, nil
}