-
Notifications
You must be signed in to change notification settings - Fork 179
/
init.go
45 lines (36 loc) · 1.28 KB
/
init.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
package badger
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/storage/badger/operation"
)
// InitPublic initializes a public database by checking and setting the database
// type marker. If an existing, inconsistent type marker is set, this method will
// return an error. Once a database type marker has been set using these methods,
// the type cannot be changed.
func InitPublic(opts badger.Options) (*badger.DB, error) {
db, err := badger.Open(opts)
if err != nil {
return nil, fmt.Errorf("could not open db: %w", err)
}
err = db.Update(operation.InsertPublicDBMarker)
if err != nil {
return nil, fmt.Errorf("could not assert db type: %w", err)
}
return db, nil
}
// InitSecret initializes a secrets database by checking and setting the database
// type marker. If an existing, inconsistent type marker is set, this method will
// return an error. Once a database type marker has been set using these methods,
// the type cannot be changed.
func InitSecret(opts badger.Options) (*badger.DB, error) {
db, err := badger.Open(opts)
if err != nil {
return nil, fmt.Errorf("could not open db: %w", err)
}
err = db.Update(operation.InsertSecretDBMarker)
if err != nil {
return nil, fmt.Errorf("could not assert db type: %w", err)
}
return db, nil
}