-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
45 lines (38 loc) · 935 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
package vault
import (
"encoding/hex"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/keys-pub/vault/syncer"
"github.com/pkg/errors"
// For sqlite3 (sqlcipher driver)
_ "github.com/mutecomm/go-sqlcipher/v4"
)
func openDB(path string, mk *[32]byte) (*sqlx.DB, error) {
keyString := hex.EncodeToString(mk[:])
pragma := fmt.Sprintf("?_pragma_key=x'%s'&_pragma_cipher_page_size=4096", keyString)
db, err := sqlx.Open("sqlite3", path+pragma)
if err != nil {
return nil, errors.Wrapf(err, "failed to open db")
}
return db, nil
}
func initTables(db *sqlx.DB) error {
logger.Debugf("Initializing tables...")
stmts := []string{
`CREATE TABLE IF NOT EXISTS config (
key TEXT PRIMARY KEY NOT NULL,
value TEXT NOT NULL
);`,
// TODO: Indexes
}
for _, stmt := range stmts {
if _, err := db.Exec(stmt); err != nil {
return err
}
}
if err := syncer.InitTables(db); err != nil {
return err
}
return nil
}