-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
104 lines (92 loc) · 1.74 KB
/
bootstrap.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package sqlite
import (
"fmt"
"os"
"strings"
"github.com/go-ap/errors"
)
func stringClean(qSql string) string {
return strings.ReplaceAll(qSql, "\n", "")
}
func Clean(conf Config) error {
p, err := getFullPath(conf)
if err != nil {
return err
}
return os.RemoveAll(p)
}
func Bootstrap(conf Config) error {
p, err := getFullPath(conf)
if err != nil {
return err
}
r := repo{
path: p,
logFn: defaultLogFn,
errFn: defaultLogFn,
}
if err = r.Open(); err != nil {
return err
}
defer func() {
if err = r.close(); err != nil {
r.errFn("error closing the db: %+s", err)
}
}()
exec := func(qRaw string, par ...interface{}) error {
qSql := fmt.Sprintf(qRaw, par...)
if _, err = r.conn.Exec(qSql); err != nil {
return errors.Annotatef(err, "unable to execute: %q", qSql)
}
return nil
}
if err = exec(createObjectsQuery); err != nil {
return err
}
if err = exec(createActivitiesQuery); err != nil {
return err
}
if err = exec(createActorsQuery); err != nil {
return err
}
if err = exec(createCollectionsQuery); err != nil {
return err
}
if err = exec(createMetaQuery); err != nil {
return err
}
if err = exec(createClientTable); err != nil {
return err
}
if err = exec(createAuthorizeTable); err != nil {
return err
}
if err = exec(createAccessTable); err != nil {
return err
}
if err = exec(createRefreshTable); err != nil {
return err
}
if err = exec(tuneQuery); err != nil {
return err
}
return nil
}
func (r *repo) Reset() {
tables := []string{
"objects",
"actors",
"activities",
"collections",
"meta",
"clients",
"authorize",
"access",
"refresh",
}
r.Open()
defer r.close()
for _, table := range tables {
r.conn.Exec(fmt.Sprintf("DELETE FROM %s;", table))
}
}