-
Notifications
You must be signed in to change notification settings - Fork 25
/
sqlite.go
57 lines (47 loc) · 1.7 KB
/
sqlite.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
// Package sqlite provides the sqlite implementation for the base store.
package sqlite
import (
"context"
"fmt"
"github.com/ipfs/go-log"
"github.com/synapsecns/sanguine/committee/db/base"
common_base "github.com/synapsecns/sanguine/core/dbcommon"
"github.com/synapsecns/sanguine/core/metrics"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"os"
)
// Store is the sqlite store. It extends the base store for sqlite specific queries.
type Store struct {
*base.Store
}
var logger = log.Logger("committee-sqlite")
// NewSqliteStore creates a new sqlite data store.
func NewSqliteStore(parentCtx context.Context, dbPath string, handler metrics.Handler) (_ *Store, err error) {
logger.Debugf("creating sqlite store at %s", dbPath)
ctx, span := handler.Tracer().Start(parentCtx, "start-sqlite")
defer func() {
metrics.EndSpanWithErr(span, err)
}()
// create the directory to the store if it doesn't exist
err = os.MkdirAll(dbPath, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("could not create sqlite store")
}
logger.Warnf("rfq database is at %s/synapse.db", dbPath)
gdb, err := gorm.Open(sqlite.Open(fmt.Sprintf("%s/%s", dbPath, "synapse.db")), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: common_base.GetGormLogger(logger),
FullSaveAssociations: true,
SkipDefaultTransaction: true,
})
if err != nil {
return nil, fmt.Errorf("could not connect to db %s: %w", dbPath, err)
}
handler.AddGormCallbacks(gdb)
err = gdb.WithContext(ctx).AutoMigrate(base.GetAllModels()...)
if err != nil {
return nil, fmt.Errorf("could not migrate models: %w", err)
}
return &Store{base.NewStore(gdb, handler)}, nil
}