-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.store.sqlite.go
More file actions
627 lines (568 loc) · 15 KB
/
Copy pathcommand.store.sqlite.go
File metadata and controls
627 lines (568 loc) · 15 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package store
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"time"
"github.com/gradientzero/comby-store-sqlite/internal"
"github.com/gradientzero/comby/v3"
_ "modernc.org/sqlite"
)
// Make sure it implements interfaces
var _ comby.CommandStore = (*commandStoreSQLite)(nil)
type commandStoreSQLite struct {
options comby.CommandStoreOptions
db *sql.DB
// sqlite specific options
path string
}
func NewCommandStoreSQLite(path string, opts ...comby.CommandStoreOption) comby.CommandStore {
cs := &commandStoreSQLite{
path: path,
}
for _, opt := range opts {
if _, err := opt(&cs.options); err != nil {
return nil
}
}
return cs
}
func (cs *commandStoreSQLite) connect(ctx context.Context) (*sql.DB, error) {
db, err := sql.Open("sqlite", cs.path)
if err != nil {
return nil, err
}
// WAL mode allows concurrent readers while a single writer holds the lock.
maxOpenConns := 10
if cs.options.MaxOpenConns > 0 {
maxOpenConns = cs.options.MaxOpenConns
}
db.SetMaxOpenConns(maxOpenConns)
if cs.options.MaxIdleConns > 0 {
db.SetMaxIdleConns(cs.options.MaxIdleConns)
}
if cs.options.ConnMaxIdleTime > 0 {
db.SetConnMaxIdleTime(cs.options.ConnMaxIdleTime)
} else {
db.SetConnMaxIdleTime(5 * time.Minute)
}
if cs.options.ConnMaxLifetime > 0 {
db.SetConnMaxLifetime(cs.options.ConnMaxLifetime)
}
// set sqlite specific pragmas
query := `
PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;
PRAGMA foreign_keys=1;
PRAGMA busy_timeout=5000;
`
if _, err := db.ExecContext(context.Background(), query); err != nil {
return nil, err
}
return db, nil
}
func (cs *commandStoreSQLite) migrate(ctx context.Context) error {
query := `
CREATE TABLE IF NOT EXISTS commands (id INTEGER,
instance_id INTEGER,
uuid TEXT,
tenant_uuid TEXT,
workspace_uuid TEXT,
domain TEXT,
created_at INTEGER,
data_type TEXT,
data_bytes TEXT,
req_ctx TEXT,
PRIMARY KEY (id)
);
CREATE INDEX IF NOT EXISTS "tenant_index" ON "commands" (
"tenant_uuid" ASC
);
CREATE INDEX IF NOT EXISTS "workspace_index" ON "commands" (
"workspace_uuid" ASC
);
CREATE UNIQUE INDEX IF NOT EXISTS "uuid_index" ON "commands" (
"uuid" ASC
);
CREATE INDEX IF NOT EXISTS "created_at_index" ON "commands" (
"created_at" ASC
);
`
if _, err := cs.db.ExecContext(ctx, query); err != nil {
return err
}
// migrate existing databases: add workspace_uuid column if it doesn't exist
var count int
if err := cs.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM pragma_table_info('commands') WHERE name='workspace_uuid'`).Scan(&count); err != nil {
return err
}
if count == 0 {
if _, err := cs.db.ExecContext(ctx, `ALTER TABLE commands ADD COLUMN workspace_uuid TEXT`); err != nil {
return err
}
}
return nil
}
// fullfilling CommandStore interface
func (cs *commandStoreSQLite) Init(ctx context.Context, opts ...comby.CommandStoreOption) error {
for _, opt := range opts {
if _, err := opt(&cs.options); err != nil {
return err
}
}
// connect to db (or create new one)
if db, err := cs.connect(ctx); err != nil {
return err
} else {
cs.db = db
}
// auto-migrate table
if !cs.options.ReadOnly {
if err := cs.migrate(ctx); err != nil {
return err
}
}
return nil
}
func (cs *commandStoreSQLite) Create(ctx context.Context, opts ...comby.CommandStoreCreateOption) error {
createOpts := comby.CommandStoreCreateOptions{
Command: nil,
}
for _, opt := range opts {
if _, err := opt(&createOpts); err != nil {
return err
}
}
if cs.options.ReadOnly {
return fmt.Errorf("'%s' failed to create command - instance is readonly", cs.String())
}
var cmd comby.Command = createOpts.Command
if cmd == nil {
return fmt.Errorf("'%s' failed to create command - command is nil", cs.String())
}
if len(cmd.GetCommandUuid()) < 1 {
return fmt.Errorf("'%s' failed to create command - command uuid is invalid", cs.String())
}
// sql statement
dbRecord, err := internal.BaseCommandToDbCommand(cmd)
if err != nil {
return err
}
// encrypt domain data if crypto service is provided
if cs.options.CryptoService != nil {
if err := cs.encryptDomainData(dbRecord); err != nil {
return err
}
}
// sql begin transaction
tx, err := cs.db.Begin()
if err != nil {
return err
}
defer func() {
if err != nil {
tx.Rollback()
}
}()
query := `INSERT INTO commands (
instance_id,
uuid,
tenant_uuid,
workspace_uuid,
domain,
created_at,
data_type,
data_bytes,
req_ctx
) VALUES (?,?,?,?,?,?,?,?,?);`
_, err = tx.ExecContext(
ctx,
query,
dbRecord.InstanceId,
dbRecord.Uuid,
dbRecord.TenantUuid,
dbRecord.WorkspaceUuid,
dbRecord.Domain,
dbRecord.CreatedAt,
dbRecord.DataType,
dbRecord.DataBytes,
dbRecord.ReqCtx,
)
if err != nil {
return err
}
return tx.Commit()
}
func (cs *commandStoreSQLite) Get(ctx context.Context, opts ...comby.CommandStoreGetOption) (comby.Command, error) {
getOpts := comby.CommandStoreGetOptions{}
for _, opt := range opts {
if _, err := opt(&getOpts); err != nil {
return nil, err
}
}
if len(getOpts.CommandUuid) == 0 {
return nil, fmt.Errorf("'%s' failed to get command - command uuid is required", cs.String())
}
query := `SELECT id, instance_id, uuid, tenant_uuid, COALESCE(workspace_uuid, ''), domain, created_at,
data_type, data_bytes, req_ctx
FROM commands WHERE uuid=? LIMIT 1;`
row := cs.db.QueryRowContext(ctx, query, getOpts.CommandUuid)
if row.Err() != nil {
return nil, row.Err()
}
// extract record
var dbRecord internal.Command
if err := row.Scan(
&dbRecord.ID,
&dbRecord.InstanceId,
&dbRecord.Uuid,
&dbRecord.TenantUuid,
&dbRecord.WorkspaceUuid,
&dbRecord.Domain,
&dbRecord.CreatedAt,
&dbRecord.DataType,
&dbRecord.DataBytes,
&dbRecord.ReqCtx,
); err != nil {
// Catch errors
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, err
}
}
// decrypt domain data if crypto service is provided
if cs.options.CryptoService != nil {
if err := cs.decryptDomainData(&dbRecord); err != nil {
return nil, err
}
}
// db record to command
cmd, err := internal.DbCommandToBaseCommand(&dbRecord)
if err != nil {
return nil, err
}
return cmd, err
}
func (cs *commandStoreSQLite) List(ctx context.Context, opts ...comby.CommandStoreListOption) ([]comby.Command, int64, error) {
listOpts := comby.CommandStoreListOptions{
Before: -1,
After: -1,
Offset: 0,
Limit: 100,
OrderBy: "created_at",
Ascending: true,
}
for _, opt := range opts {
if _, err := opt(&listOpts); err != nil {
return nil, 0, err
}
}
var whereSQL string = ""
var whereList []string = []string{}
var args []any
if len(listOpts.TenantUuid) > 0 {
whereList = append(whereList, "tenant_uuid=?")
args = append(args, listOpts.TenantUuid)
}
if len(listOpts.Domain) > 0 {
whereList = append(whereList, "domain=?")
args = append(args, listOpts.Domain)
}
if len(listOpts.DataType) > 0 {
whereList = append(whereList, "data_type=?")
args = append(args, listOpts.DataType)
}
if listOpts.Before >= 0 {
whereList = append(whereList, "created_at<?")
args = append(args, listOpts.Before)
}
if listOpts.After >= 0 {
whereList = append(whereList, "created_at>?")
args = append(args, listOpts.After)
}
// note the first empty character(s) below
for index, where := range whereList {
if index == 0 {
whereSQL = fmt.Sprintf(" WHERE %s", where)
} else {
whereSQL = fmt.Sprintf("%s AND %s", whereSQL, where)
}
}
// count the total number of records for this query
var queryTotal int64
var queryTotalQuery string = fmt.Sprintf("SELECT COUNT(id) FROM commands%s;", whereSQL)
var row *sql.Row
if len(args) > 0 {
row = cs.db.QueryRowContext(ctx, queryTotalQuery, args...)
} else {
row = cs.db.QueryRowContext(ctx, queryTotalQuery)
}
if err := row.Err(); err != nil {
return nil, 0, err
}
// extract record
if err := row.Scan(&queryTotal); err != nil {
return nil, 0, err
}
// prepare orderby
var orderBySQL string = ""
if len(listOpts.OrderBy) > 0 {
if listOpts.Ascending {
orderBySQL = fmt.Sprintf(" ORDER BY %s ASC", listOpts.OrderBy)
} else {
orderBySQL = fmt.Sprintf(" ORDER BY %s DESC", listOpts.OrderBy)
}
}
// prepare limit/offset
var limitSQL string = ""
var offsetSQL string = ""
if listOpts.Limit >= 0 {
limitSQL = fmt.Sprintf(" LIMIT %d", listOpts.Limit)
}
if listOpts.Offset >= 0 {
offsetSQL = fmt.Sprintf(" OFFSET %d", listOpts.Offset)
}
var query string = fmt.Sprintf("SELECT id, instance_id, uuid, tenant_uuid, COALESCE(workspace_uuid, ''), domain, created_at, data_type, data_bytes, req_ctx FROM commands%s%s%s%s;", whereSQL, orderBySQL, limitSQL, offsetSQL)
var rows *sql.Rows
var err error
if len(args) > 0 {
rows, err = cs.db.QueryContext(ctx, query, args...)
} else {
rows, err = cs.db.QueryContext(ctx, query)
}
switch {
case err == sql.ErrNoRows:
return nil, queryTotal, nil
case err != nil:
return nil, 0, err
}
if rows != nil {
defer rows.Close()
}
// extract results
var dbRecords []*internal.Command
for rows.Next() {
var dbRecord internal.Command
if err := rows.Scan(
&dbRecord.ID,
&dbRecord.InstanceId,
&dbRecord.Uuid,
&dbRecord.TenantUuid,
&dbRecord.WorkspaceUuid,
&dbRecord.Domain,
&dbRecord.CreatedAt,
&dbRecord.DataType,
&dbRecord.DataBytes,
&dbRecord.ReqCtx,
); err != nil {
return nil, 0, err
}
dbRecords = append(dbRecords, &dbRecord)
}
if err := rows.Close(); err != nil {
return nil, 0, err
}
if err := rows.Err(); err != nil {
return nil, 0, err
}
// decrypt domain data if crypto service is provided
if cs.options.CryptoService != nil {
for _, dbRecord := range dbRecords {
if err := cs.decryptDomainData(dbRecord); err != nil {
return nil, 0, err
}
}
}
// convert
cmds, err := internal.DbCommandsToBaseCommands(dbRecords)
if err != nil {
return nil, 0, err
}
return cmds, queryTotal, err
}
func (cs *commandStoreSQLite) Update(ctx context.Context, opts ...comby.CommandStoreUpdateOption) error {
updateOpts := comby.CommandStoreUpdateOptions{
Command: nil,
}
for _, opt := range opts {
if _, err := opt(&updateOpts); err != nil {
return err
}
}
if cs.options.ReadOnly {
return fmt.Errorf("'%s' failed to update command - instance is readonly", cs.String())
}
var cmd comby.Command = updateOpts.Command
if cmd == nil {
return fmt.Errorf("'%s' failed to update command - command is nil", cs.String())
}
if len(cmd.GetCommandUuid()) < 1 {
return fmt.Errorf("'%s' failed to update command - command uuid is invalid", cs.String())
}
// convert to db format
dbRecord, err := internal.BaseCommandToDbCommand(cmd)
if err != nil {
return err
}
// encrypt domain data if crypto service is provided
if cs.options.CryptoService != nil {
if err := cs.encryptDomainData(dbRecord); err != nil {
return err
}
}
// sql begin transaction
tx, err := cs.db.Begin()
if err != nil {
return err
}
defer func() {
if err != nil {
tx.Rollback()
}
}()
query := `UPDATE commands SET
instance_id=?,
tenant_uuid=?,
workspace_uuid=?,
domain=?,
created_at=?,
data_type=?,
data_bytes=?,
req_ctx=?
WHERE uuid=?;`
_, err = tx.ExecContext(ctx,
query,
dbRecord.InstanceId,
dbRecord.TenantUuid,
dbRecord.WorkspaceUuid,
dbRecord.Domain,
dbRecord.CreatedAt,
dbRecord.DataType,
dbRecord.DataBytes,
dbRecord.ReqCtx,
dbRecord.Uuid)
if err != nil {
return err
}
return tx.Commit()
}
func (cs *commandStoreSQLite) Delete(ctx context.Context, opts ...comby.CommandStoreDeleteOption) error {
deleteOpts := comby.CommandStoreDeleteOptions{}
for _, opt := range opts {
if _, err := opt(&deleteOpts); err != nil {
return err
}
}
if cs.options.ReadOnly {
return fmt.Errorf("'%s' failed to delete command - instance is readonly", cs.String())
}
var commandUuid string = deleteOpts.CommandUuid
if len(commandUuid) < 1 {
return fmt.Errorf("'%s' failed to delete command - command uuid '%s' is invalid", cs.String(), commandUuid)
}
_, err := cs.db.ExecContext(ctx, "DELETE FROM commands WHERE uuid=?;", commandUuid)
return err
}
func (cs *commandStoreSQLite) Total(ctx context.Context) int64 {
// run query (no args to not using prepared statement)
row := cs.db.QueryRowContext(ctx, `SELECT COUNT(id) FROM commands;`)
if err := row.Err(); err != nil {
return 0
}
// extract record
var dbTotal int64
if err := row.Scan(&dbTotal); err != nil {
return 0
}
return dbTotal
}
func (cs *commandStoreSQLite) Close(ctx context.Context) error {
return cs.db.Close()
}
func (cs *commandStoreSQLite) Options() comby.CommandStoreOptions {
return cs.options
}
func (cs *commandStoreSQLite) String() string {
return fmt.Sprintf("sqlite - %s", cs.path)
}
func (cs *commandStoreSQLite) Info(ctx context.Context) (*comby.CommandStoreInfoModel, error) {
row := cs.db.QueryRowContext(ctx, "SELECT COUNT(uuid) FROM commands;")
if err := row.Err(); err != nil {
return nil, err
}
// extract record
var dbTotal int64
if err := row.Scan(&dbTotal); err != nil {
return nil, err
}
row = cs.db.QueryRowContext(ctx, "SELECT COALESCE(MAX(created_at), 0) FROM commands;")
if err := row.Err(); err != nil {
return nil, err
}
// extract record
var dbLastCreatedAt int64
if err := row.Scan(&dbLastCreatedAt); err != nil {
return nil, err
}
return &comby.CommandStoreInfoModel{
StoreType: "sqlite",
LastItemCreatedAt: dbLastCreatedAt,
NumItems: dbTotal,
ConnectionInfo: cs.path,
}, nil
}
func (cs *commandStoreSQLite) Reset(ctx context.Context) error {
if cs.options.ReadOnly {
return fmt.Errorf("'%s' failed to reset - instance is readonly", cs.String())
}
//try to delete all files
files, err := filepath.Glob(cs.path + "*")
if err != nil {
return err
}
for _, file := range files {
err = os.Remove(file)
if err != nil {
return err
}
}
return nil
}
func (cs *commandStoreSQLite) encryptDomainData(dbRecord *internal.Command) error {
if cs.options.CryptoService == nil {
return fmt.Errorf("'%s' failed - crypto service is nil", cs.String())
}
domainData := []byte(dbRecord.DataBytes)
if len(domainData) < 1 {
return fmt.Errorf("'%s' failed - domain data is empty", cs.String())
}
if encryptedData, err := cs.options.CryptoService.Encrypt(domainData); err != nil {
return fmt.Errorf("'%s' failed - failed to encrypt domain data: %w", cs.String(), err)
} else {
dbRecord.DataBytes = hex.EncodeToString(encryptedData)
}
return nil
}
func (cs *commandStoreSQLite) decryptDomainData(dbRecord *internal.Command) error {
if cs.options.CryptoService == nil {
return fmt.Errorf("'%s' failed - crypto service is nil", cs.String())
}
encryptedData, err := hex.DecodeString(dbRecord.DataBytes)
if err != nil {
return fmt.Errorf("'%s' failed - failed to decode hex domain data: %w", cs.String(), err)
}
if len(encryptedData) < 1 {
return fmt.Errorf("'%s' failed - encrypted domain data is empty", cs.String())
}
if decryptedData, err := cs.options.CryptoService.Decrypt(encryptedData); err != nil {
return fmt.Errorf("'%s' failed - failed to decrypt domain data: %w", cs.String(), err)
} else {
dbRecord.DataBytes = string(decryptedData)
}
return nil
}