-
Notifications
You must be signed in to change notification settings - Fork 26
/
database.go
243 lines (225 loc) · 6.14 KB
/
database.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"context"
"database/sql"
"errors"
"io"
"net/url"
"strings"
"sync"
"github.com/google/uuid"
sqlite "github.com/mattn/go-sqlite3"
"github.com/samber/go-singleflightx"
"github.com/samber/lo"
"github.com/schollz/sqlite3dump"
"go.goblog.app/app/pkgs/utils"
)
type database struct {
a *goBlog
writeDb, readDb, dumpDb *sql.DB
// Other things
pc singleflightx.Group[string, []byte] // persistant cache
pcm sync.RWMutex // post creation
sp singleflightx.Group[string, string] // short path creation
debug bool
}
func (a *goBlog) initDatabase(logging bool) error {
if a.db != nil && a.db.writeDb != nil && a.db.readDb != nil {
return nil
}
if logging {
a.info("Initialize database")
}
// Setup db
dumpEnabled := a.cfg.Db.DumpFile != ""
db, err := a.openDatabase(a.cfg.Db.File, logging, dumpEnabled)
if err != nil {
return err
}
// Create appDB
a.db = db
a.shutdown.Add(func() {
if err := db.close(); err != nil {
a.error("Failed to close database", "err", err)
} else {
a.info("Closed database")
}
})
if dumpEnabled {
dumpFunc := func() {
if err := db.dump(a.cfg.Db.DumpFile); err != nil {
a.error("Error while dump db", "err", err)
}
}
a.hourlyHooks = append(a.hourlyHooks, func() {
dumpFunc()
})
dumpFunc()
}
if logging {
a.info("Initialized database")
}
return nil
}
func (a *goBlog) openDatabase(file string, logging, dump bool) (*database, error) {
file = lo.If(strings.Contains(file, "?"), file+"&").Else(file + "?")
// Register driver
dbDriverName := "goblog_db_" + uuid.NewString()
sql.Register(dbDriverName, &sqlite.SQLiteDriver{
ConnectHook: func(c *sqlite.SQLiteConn) error {
funcs := map[string]any{
"mdtext": a.renderTextSafe,
"tolocal": toLocalSafe,
"toutc": toUTCSafe,
"wordcount": wordCount,
"charcount": charCount,
"urlize": urlize,
"lowerx": strings.ToLower,
"lowerunescaped": lowerUnescapedPath,
}
for n, f := range funcs {
if err := c.RegisterFunc(n, f, true); err != nil {
return err
}
}
return nil
},
})
// Open write connection
writeParams := make(url.Values)
writeParams.Add("mode", "rwc")
writeParams.Add("_txlock", "immediate")
writeParams.Add("_journal_mode", "WAL")
writeParams.Add("_busy_timeout", "1000")
writeParams.Add("_synchronous", "NORMAL")
writeParams.Add("_foreign_keys", "true")
writeDb, err := sql.Open(dbDriverName, file+writeParams.Encode())
if err != nil {
return nil, err
}
writeDb.SetMaxOpenConns(1)
writeDb.SetMaxIdleConns(1)
if err := writeDb.Ping(); err != nil {
return nil, err
}
// Check available SQLite features
if err := checkSQLiteFeatures(writeDb); err != nil {
return nil, err
}
// Migrate DB
if err := a.migrateDb(writeDb, logging); err != nil {
return nil, err
}
// Open read connections
readParams := make(url.Values)
readParams.Add("mode", "ro") // read-only
readParams.Add("_txlock", "deferred")
readParams.Add("_journal_mode", "WAL")
readParams.Add("_busy_timeout", "1000")
readParams.Add("_synchronous", "NORMAL")
readParams.Add("_foreign_keys", "true")
readDb, err := sql.Open(dbDriverName, file+readParams.Encode())
if err != nil {
return nil, err
}
if err := readDb.Ping(); err != nil {
return nil, err
}
// Dump db
var dumpDb *sql.DB
if dump {
dumpDb, err = sql.Open("sqlite3", file+readParams.Encode())
if err != nil {
return nil, err
}
dumpDb.SetMaxIdleConns(0)
if err := dumpDb.Ping(); err != nil {
return nil, err
}
}
// Create custom database struct
return &database{
a: a,
writeDb: writeDb,
readDb: readDb,
dumpDb: dumpDb,
debug: a.cfg.Db != nil && a.cfg.Db.Debug,
}, nil
}
func checkSQLiteFeatures(db *sql.DB) error {
rows, err := db.Query("pragma compile_options")
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var option string
if err := rows.Scan(&option); err != nil {
return err
}
if option == "ENABLE_FTS5" {
return nil
}
}
return errors.New("sqlite not compiled with FTS5")
}
func (db *database) Exec(query string, args ...any) (sql.Result, error) {
return db.ExecContext(context.Background(), query, args...)
}
func (db *database) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
if db == nil || db.writeDb == nil {
return nil, errors.New("database not initialized")
}
ctx = db.dbBefore(ctx, query, args...)
defer db.dbAfter(ctx, query, args...)
return db.writeDb.ExecContext(ctx, query, args...)
}
func (db *database) Query(query string, args ...any) (*sql.Rows, error) {
return db.QueryContext(context.Background(), query, args...)
}
func (db *database) QueryContext(ctx context.Context, query string, args ...any) (rows *sql.Rows, err error) {
if db == nil || db.readDb == nil {
return nil, errors.New("database not initialized")
}
ctx = db.dbBefore(ctx, query, args...)
defer db.dbAfter(ctx, query, args...)
return db.readDb.QueryContext(ctx, query, args...)
}
func (db *database) QueryRow(query string, args ...any) (*sql.Row, error) {
return db.QueryRowContext(context.Background(), query, args...)
}
func (db *database) QueryRowContext(ctx context.Context, query string, args ...any) (row *sql.Row, err error) {
if db == nil || db.readDb == nil {
return nil, errors.New("database not initialized")
}
ctx = db.dbBefore(ctx, query, args...)
defer db.dbAfter(ctx, query, args...)
return db.readDb.QueryRowContext(ctx, query, args...), nil
}
func (db *database) dump(file string) error {
if db == nil || db.dumpDb == nil {
return errors.New("database not initialised")
}
pr, pw := io.Pipe()
go func() {
_ = pw.CloseWithError(sqlite3dump.DumpDB(db.dumpDb, pw, sqlite3dump.WithTransaction(false)))
}()
err := utils.SaveToFile(pr, file)
_ = pr.Close()
return err
}
func (db *database) close() error {
if db == nil {
return nil
}
var errs []error
for _, db := range []*sql.DB{db.writeDb, db.readDb, db.dumpDb} {
if db != nil {
errs = append(errs, db.Close())
}
}
return errors.Join(errs...)
}
func (db *database) rebuildFTSIndex() {
_, _ = db.Exec("insert into posts_fts(posts_fts) values ('rebuild')")
}