-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.store.postgres.go
More file actions
862 lines (781 loc) · 23.5 KB
/
Copy pathevent.store.postgres.go
File metadata and controls
862 lines (781 loc) · 23.5 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
package store
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/gradientzero/comby-store-postgres/internal"
"github.com/gradientzero/comby/v3"
_ "github.com/lib/pq"
)
// Make sure it implements interfaces
var _ comby.EventStore = (*eventStorePostgres)(nil)
// pgQuerier abstracts *sql.DB and *sql.Tx so the same query code can run
// with or without an RLS-scoped transaction wrapper.
type pgQuerier interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
type eventStorePostgres struct {
options comby.EventStoreOptions
db *sql.DB // owner connection (BYPASSRLS)
appDb *sql.DB // optional non-BYPASSRLS connection used for tenant-scoped ops; nil if RLS is off
// potgres specific options
host string
port int
user string
password string
dbName string
}
func NewEventStorePostgres(host string, port int, user, password, dbName string, opts ...comby.EventStoreOption) comby.EventStore {
es := &eventStorePostgres{
host: host,
port: port,
user: user,
password: password,
dbName: dbName,
}
for _, opt := range opts {
if _, err := opt(&es.options); err != nil {
return nil
}
}
return es
}
func (es *eventStorePostgres) connect(ctx context.Context) (*sql.DB, error) {
return es.connectAs(ctx, es.user, es.password)
}
func (es *eventStorePostgres) connectAs(ctx context.Context, user, password string) (*sql.DB, error) {
var dbNameStr string
var passwordStr string
dbName := es.dbName
if len(dbName) != 0 {
dbNameStr = fmt.Sprintf("dbname=%s", dbName)
}
if len(password) != 0 {
passwordStr = fmt.Sprintf("password=%s", password)
}
dsn := fmt.Sprintf("host=%s port=%d user=%s %s %s sslmode=disable",
es.host, es.port, user, passwordStr, dbNameStr)
// create postgres connection
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, err
}
// open connection by ping
if err := db.Ping(); err != nil {
return nil, err
}
// PostgreSQL supports full MVCC — concurrent reads and writes are safe.
// Apply pool settings from options, falling back to sensible defaults.
maxOpenConns := 25
if es.options.MaxOpenConns > 0 {
maxOpenConns = es.options.MaxOpenConns
}
db.SetMaxOpenConns(maxOpenConns)
maxIdleConns := 5
if es.options.MaxIdleConns > 0 {
maxIdleConns = es.options.MaxIdleConns
}
db.SetMaxIdleConns(maxIdleConns)
if es.options.ConnMaxLifetime > 0 {
db.SetConnMaxLifetime(es.options.ConnMaxLifetime)
} else {
db.SetConnMaxLifetime(30 * time.Minute)
}
if es.options.ConnMaxIdleTime > 0 {
db.SetConnMaxIdleTime(es.options.ConnMaxIdleTime)
} else {
db.SetConnMaxIdleTime(5 * time.Minute)
}
return db, nil
}
func (es *eventStorePostgres) migrate(ctx context.Context) error {
query := `
CREATE TABLE IF NOT EXISTS events (
id SERIAL PRIMARY KEY,
instance_id INTEGER,
uuid TEXT,
tenant_uuid TEXT,
workspace_uuid TEXT,
command_uuid TEXT,
domain TEXT,
aggregate_uuid TEXT,
version INTEGER,
created_at BIGINT,
data_type TEXT,
data_bytes TEXT,
req_ctx TEXT
);
CREATE INDEX IF NOT EXISTS "tenant_index" ON "events" (
"tenant_uuid" ASC
);
CREATE INDEX IF NOT EXISTS "workspace_index" ON "events" (
"workspace_uuid" ASC
);
CREATE INDEX IF NOT EXISTS "aggregate_uuid_index" ON "events" (
"aggregate_uuid" ASC
);
CREATE UNIQUE INDEX IF NOT EXISTS "uuid_index" ON "events" (
"uuid" ASC
);
CREATE INDEX IF NOT EXISTS "created_at_index" ON "events" (
"created_at" ASC
);
`
if _, err := es.db.ExecContext(ctx, query); err != nil {
return err
}
// migrate existing databases: add req_ctx column if it doesn't exist
if _, err := es.db.ExecContext(ctx, `ALTER TABLE events ADD COLUMN IF NOT EXISTS req_ctx TEXT`); err != nil {
return err
}
// migrate existing databases: add workspace_uuid column if it doesn't exist
if _, err := es.db.ExecContext(ctx, `ALTER TABLE events ADD COLUMN IF NOT EXISTS workspace_uuid TEXT`); err != nil {
return err
}
return nil
}
// fullfilling EventStore interface
func (es *eventStorePostgres) Init(ctx context.Context, opts ...comby.EventStoreOption) error {
for _, opt := range opts {
if _, err := opt(&es.options); err != nil {
return err
}
}
// Idempotent re-init: if the owner pool is already open, the second call
// just merges any new options and returns. Without this, callers that
// pass the same store handle through both NewEventStorePostgres options
// AND a follow-up Init (e.g. comby.NewFacade auto-inits its registered
// stores) would open a brand-new connection pool every time, leaking the
// previous one and quickly exhausting Postgres' connection budget.
if es.db != nil {
return nil
}
// connect to db (or create new one) as the owner role
if db, err := es.connect(ctx); err != nil {
return err
} else {
es.db = db
}
// auto-migrate table
if !es.options.ReadOnly {
if err := es.migrate(ctx); err != nil {
return err
}
}
// optional RLS app-role pool — only when EventStoreOptionWithRLSAppRole was set
if es.options.RLSAppRoleUser != "" {
// Ensure RLS policy is present on the events table only — Command/
// Snapshot stores enable RLS for their own tables independently.
// Idempotent.
if err := EnablePostgresRLSForTable(ctx, es.db, "events"); err != nil {
return fmt.Errorf("eventStorePostgres.Init: EnablePostgresRLSForTable failed: %w", err)
}
// Ensure the app role exists with NOBYPASSRLS + table grants.
if err := EnsureAppRole(ctx, es.db, es.options.RLSAppRoleUser, es.options.RLSAppRolePassword); err != nil {
return fmt.Errorf("eventStorePostgres.Init: EnsureAppRole failed: %w", err)
}
// Open the app-role pool.
if appDb, err := es.connectAs(ctx, es.options.RLSAppRoleUser, es.options.RLSAppRolePassword); err != nil {
return fmt.Errorf("eventStorePostgres.Init: app-role connect failed: %w", err)
} else {
es.appDb = appDb
}
}
return nil
}
// rlsEnabled reports whether the store has been configured with an RLS app
// role and an active app-role pool.
func (es *eventStorePostgres) rlsEnabled() bool {
return es.appDb != nil
}
// withTenantConn picks the correct connection pool and runs fn against it.
//
// - If RLS is off, ctx is bypass-tagged, or tenantUuid is empty (cross-tenant
// query), fn runs on the owner pool (es.db) directly.
// - Otherwise fn runs on es.appDb inside a transaction whose
// app.tenant_uuid is set to tenantUuid via SET LOCAL. The RESTRICTIVE
// RLS policies on events/commands/snapshots then constrain reads and
// writes to that tenant.
//
// The fn callback receives a pgQuerier so it can use either *sql.DB or
// *sql.Tx polymorphically.
func (es *eventStorePostgres) withTenantConn(ctx context.Context, tenantUuid string, fn func(pgQuerier) error) error {
if !es.rlsEnabled() || comby.IsRLSBypass(ctx) || tenantUuid == "" {
return fn(es.db)
}
return WithRLSSession(ctx, es.appDb, tenantUuid, func(tx *sql.Tx) error {
return fn(tx)
})
}
func (es *eventStorePostgres) Create(ctx context.Context, opts ...comby.EventStoreCreateOption) error {
createOpts := comby.EventStoreCreateOptions{
Event: nil,
}
for _, opt := range opts {
if _, err := opt(&createOpts); err != nil {
return err
}
}
if es.options.ReadOnly {
return fmt.Errorf("'%s' failed to create event - instance is readonly", es.String())
}
var evt comby.Event = createOpts.Event
if evt == nil {
return fmt.Errorf("'%s' failed to create event - event is nil", es.String())
}
if len(evt.GetEventUuid()) < 1 {
return fmt.Errorf("'%s' failed to create event - event uuid is invalid", es.String())
}
// sql statement
dbRecord, err := internal.BaseEventToDbEvent(evt)
if err != nil {
return err
}
// encrypt domain data if crypto service is provided
if es.options.CryptoService != nil {
if err := es.encryptDomainData(dbRecord); err != nil {
return err
}
}
query := `INSERT INTO events (
instance_id,
uuid,
tenant_uuid,
workspace_uuid,
command_uuid,
domain,
aggregate_uuid,
version,
created_at,
data_type,
data_bytes,
req_ctx
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);`
return es.withTenantConn(ctx, dbRecord.TenantUuid, func(q pgQuerier) error {
_, err := q.ExecContext(
ctx,
query,
dbRecord.InstanceId,
dbRecord.Uuid,
dbRecord.TenantUuid,
dbRecord.WorkspaceUuid,
dbRecord.CommandUuid,
dbRecord.Domain,
dbRecord.AggregateUuid,
dbRecord.Version,
dbRecord.CreatedAt,
dbRecord.DataType,
dbRecord.DataBytes,
dbRecord.ReqCtx,
)
return err
})
}
func (es *eventStorePostgres) Get(ctx context.Context, opts ...comby.EventStoreGetOption) (comby.Event, error) {
getOpts := comby.EventStoreGetOptions{}
for _, opt := range opts {
if _, err := opt(&getOpts); err != nil {
return nil, err
}
}
if len(getOpts.EventUuid) == 0 {
return nil, fmt.Errorf("'%s' failed to get event - event uuid is required", es.String())
}
query := `SELECT id, instance_id, uuid, tenant_uuid, COALESCE(workspace_uuid, ''), command_uuid, domain,
aggregate_uuid, version, created_at, data_type, data_bytes, COALESCE(req_ctx, '')
FROM events WHERE uuid=$1 LIMIT 1;`
// Get-by-uuid has no tenant filter — must run on owner pool. Callers that
// need tenant scoping should use List(WithTenantUuid).
row := es.db.QueryRowContext(ctx, query, getOpts.EventUuid)
if row.Err() != nil {
return nil, row.Err()
}
// extract record
var dbRecord internal.Event
if err := row.Scan(
&dbRecord.ID,
&dbRecord.InstanceId,
&dbRecord.Uuid,
&dbRecord.TenantUuid,
&dbRecord.WorkspaceUuid,
&dbRecord.CommandUuid,
&dbRecord.Domain,
&dbRecord.AggregateUuid,
&dbRecord.Version,
&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 es.options.CryptoService != nil {
if err := es.decryptDomainData(&dbRecord); err != nil {
return nil, err
}
}
// db record to event
evt, err := internal.DbEventToBaseEvent(&dbRecord)
if err != nil {
return nil, err
}
return evt, err
}
func (es *eventStorePostgres) List(ctx context.Context, opts ...comby.EventStoreListOption) ([]comby.Event, int64, error) {
listOpts := comby.EventStoreListOptions{
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
}
}
// prepare statement: (do NOT used them for Query/QueryContext)
// 1. see different syntax for postgres:
// http://go-database-sql.org/prepared.html#parameter-placeholder-syntax
// 2. db.Query and db.QueryContext for some reason it does not work as expected
// (seems to be something internally in database/sql because for SQLite and Postgres
// simply does not return the expected result after sending new values to prepared statement)
var whereSQL string = ""
var whereList []string = []string{}
var args []any
paramIdx := 0
if len(listOpts.TenantUuid) > 0 {
paramIdx++
whereList = append(whereList, fmt.Sprintf("tenant_uuid=$%d", paramIdx))
args = append(args, listOpts.TenantUuid)
}
if len(listOpts.AggregateUuid) > 0 {
paramIdx++
whereList = append(whereList, fmt.Sprintf("aggregate_uuid=$%d", paramIdx))
args = append(args, listOpts.AggregateUuid)
}
if len(listOpts.DataType) > 0 {
paramIdx++
whereList = append(whereList, fmt.Sprintf("data_type=$%d", paramIdx))
args = append(args, listOpts.DataType)
}
if len(listOpts.Domains) > 0 {
placeholders := make([]string, len(listOpts.Domains))
for i, d := range listOpts.Domains {
paramIdx++
placeholders[i] = fmt.Sprintf("$%d", paramIdx)
args = append(args, d)
}
whereList = append(whereList, fmt.Sprintf("domain IN (%s)", strings.Join(placeholders, ",")))
}
if listOpts.Before >= 0 {
paramIdx++
whereList = append(whereList, fmt.Sprintf("created_at<$%d", paramIdx))
args = append(args, listOpts.Before)
}
if listOpts.After >= 0 {
paramIdx++
whereList = append(whereList, fmt.Sprintf("created_at>$%d", paramIdx))
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)
}
}
// prepare orderby statement
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 statements
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)
}
// build queries
var queryTotalQuery string = fmt.Sprintf("SELECT COUNT(id) FROM events%s;", whereSQL)
var query string = fmt.Sprintf("SELECT id, instance_id, uuid, tenant_uuid, COALESCE(workspace_uuid, ''), command_uuid, domain, aggregate_uuid, version, created_at, data_type, data_bytes, COALESCE(req_ctx, '') FROM events%s%s%s%s;", whereSQL, orderBySQL, limitSQL, offsetSQL)
// route the count + list through the same connection. If TenantUuid is set,
// we run on the app pool with SET LOCAL app.tenant_uuid; otherwise on owner.
var queryTotal int64
var dbRecords []*internal.Event
if rlsErr := es.withTenantConn(ctx, listOpts.TenantUuid, func(q pgQuerier) error {
// count
var row *sql.Row
if len(args) > 0 {
row = q.QueryRowContext(ctx, queryTotalQuery, args...)
} else {
row = q.QueryRowContext(ctx, queryTotalQuery)
}
if err := row.Err(); err != nil {
return err
}
if err := row.Scan(&queryTotal); err != nil {
return err
}
// list
var rows *sql.Rows
var err error
if len(args) > 0 {
rows, err = q.QueryContext(ctx, query, args...)
} else {
rows, err = q.QueryContext(ctx, query)
}
switch {
case err == sql.ErrNoRows:
return nil
case err != nil:
return err
}
if rows != nil {
defer rows.Close()
}
for rows.Next() {
var dbRecord internal.Event
if err := rows.Scan(
&dbRecord.ID,
&dbRecord.InstanceId,
&dbRecord.Uuid,
&dbRecord.TenantUuid,
&dbRecord.WorkspaceUuid,
&dbRecord.CommandUuid,
&dbRecord.Domain,
&dbRecord.AggregateUuid,
&dbRecord.Version,
&dbRecord.CreatedAt,
&dbRecord.DataType,
&dbRecord.DataBytes,
&dbRecord.ReqCtx,
); err != nil {
return err
}
dbRecords = append(dbRecords, &dbRecord)
}
if err = rows.Err(); err != nil {
return err
}
return rows.Close()
}); rlsErr != nil {
return nil, 0, rlsErr
}
// decrypt domain data if crypto service is provided
if es.options.CryptoService != nil {
for _, dbRecord := range dbRecords {
if err := es.decryptDomainData(dbRecord); err != nil {
return nil, 0, err
}
}
}
// convert
evts, err := internal.DbEventsToBaseEvents(dbRecords)
if err != nil {
return nil, 0, err
}
return evts, queryTotal, nil
}
func (es *eventStorePostgres) Update(ctx context.Context, opts ...comby.EventStoreUpdateOption) error {
updateOpts := comby.EventStoreUpdateOptions{
Event: nil,
}
for _, opt := range opts {
if _, err := opt(&updateOpts); err != nil {
return err
}
}
if es.options.ReadOnly {
return fmt.Errorf("'%s' failed to update event - instance is readonly", es.String())
}
var evt comby.Event = updateOpts.Event
if evt == nil {
return fmt.Errorf("'%s' failed to update event - event is nil", es.String())
}
if len(evt.GetEventUuid()) < 1 {
return fmt.Errorf("'%s' failed to update event - event uuid is invalid", es.String())
}
// convert to db format
dbRecord, err := internal.BaseEventToDbEvent(evt)
if err != nil {
return err
}
// encrypt domain data if crypto service is provided
if es.options.CryptoService != nil {
if err := es.encryptDomainData(dbRecord); err != nil {
return err
}
}
query := `UPDATE events SET
instance_id=$1,
tenant_uuid=$2,
workspace_uuid=$3,
command_uuid=$4,
domain=$5,
aggregate_uuid=$6,
version=$7,
created_at=$8,
data_type=$9,
data_bytes=$10,
req_ctx=$11
WHERE uuid=$12;`
return es.withTenantConn(ctx, dbRecord.TenantUuid, func(q pgQuerier) error {
_, err := q.ExecContext(ctx,
query,
dbRecord.InstanceId,
dbRecord.TenantUuid,
dbRecord.WorkspaceUuid,
dbRecord.CommandUuid,
dbRecord.Domain,
dbRecord.AggregateUuid,
dbRecord.Version,
dbRecord.CreatedAt,
dbRecord.DataType,
dbRecord.DataBytes,
dbRecord.ReqCtx,
dbRecord.Uuid)
return err
})
}
func (es *eventStorePostgres) Delete(ctx context.Context, opts ...comby.EventStoreDeleteOption) error {
deleteOpts := comby.EventStoreDeleteOptions{}
for _, opt := range opts {
if _, err := opt(&deleteOpts); err != nil {
return err
}
}
if es.options.ReadOnly {
return fmt.Errorf("'%s' failed to delete event - instance is readonly", es.String())
}
var eventUuid string = deleteOpts.EventUuid
if len(eventUuid) < 1 {
return fmt.Errorf("'%s' failed to delete event - event uuid '%s' is invalid", es.String(), eventUuid)
}
// run query with parameterized values. Delete-by-uuid has no tenant
// filter so it runs on the owner pool. Tenant-scoped admin tools should
// use a tenant-aware reset/delete-by-aggregate flow instead.
query := "DELETE FROM events WHERE uuid=$1;"
_, err := es.db.ExecContext(ctx, query, eventUuid)
return err
}
func (es *eventStorePostgres) Total(ctx context.Context) int64 {
// run query (no args to not using prepared statement)
row := es.db.QueryRowContext(ctx, `SELECT COUNT(id) FROM events;`)
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 (es *eventStorePostgres) UniqueList(ctx context.Context, opts ...comby.EventStoreUniqueListOption) ([]string, int64, error) {
listOpts := comby.EventStoreUniqueListOptions{
DbField: "tenant_uuid",
Offset: 0,
Limit: 100,
Ascending: true,
}
for _, opt := range opts {
if _, err := opt(&listOpts); err != nil {
return nil, 0, err
}
}
// prepare where
var whereSQL string = ""
var whereList []string = []string{}
var args []any
paramIdx := 0
if len(listOpts.TenantUuid) > 0 {
paramIdx++
whereList = append(whereList, fmt.Sprintf("tenant_uuid=$%d", paramIdx))
args = append(args, listOpts.TenantUuid)
}
if len(listOpts.Domain) > 0 {
paramIdx++
whereList = append(whereList, fmt.Sprintf("domain=$%d", paramIdx))
args = append(args, listOpts.Domain)
}
// 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)
}
}
// prepare orderby
var orderBySQL string = ""
if len(listOpts.DbField) > 0 {
if listOpts.Ascending {
orderBySQL = fmt.Sprintf(" ORDER BY %s ASC", listOpts.DbField)
} else {
orderBySQL = fmt.Sprintf(" ORDER BY %s DESC", listOpts.DbField)
}
}
// prepare limit/offset statements
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)
}
// run query with parameterized values
var query string = fmt.Sprintf("SELECT DISTINCT %s FROM events%s%s%s%s;", listOpts.DbField, whereSQL, orderBySQL, limitSQL, offsetSQL)
var totalQuery string = fmt.Sprintf("SELECT COUNT(DISTINCT %s) FROM events%s;", listOpts.DbField, whereSQL)
var dbUniqueValues []string
var dbTotal int64
if rlsErr := es.withTenantConn(ctx, listOpts.TenantUuid, func(q pgQuerier) error {
var rows *sql.Rows
var err error
if len(args) > 0 {
rows, err = q.QueryContext(ctx, query, args...)
} else {
rows, err = q.QueryContext(ctx, query)
}
switch {
case err == sql.ErrNoRows:
return nil
case err != nil:
return err
}
if rows != nil {
defer rows.Close()
}
for rows.Next() {
var v string
if err := rows.Scan(&v); err != nil {
return err
}
dbUniqueValues = append(dbUniqueValues, v)
}
if err := rows.Close(); err != nil {
return err
}
if err := rows.Err(); err != nil {
return err
}
var row *sql.Row
if len(args) > 0 {
row = q.QueryRowContext(ctx, totalQuery, args...)
} else {
row = q.QueryRowContext(ctx, totalQuery)
}
if err := row.Err(); err != nil {
return err
}
return row.Scan(&dbTotal)
}); rlsErr != nil {
return nil, 0, rlsErr
}
return dbUniqueValues, dbTotal, nil
}
func (es *eventStorePostgres) Close(ctx context.Context) error {
if es.appDb != nil {
_ = es.appDb.Close()
}
return es.db.Close()
}
func (es *eventStorePostgres) Options() comby.EventStoreOptions {
return es.options
}
func (es *eventStorePostgres) String() string {
return fmt.Sprintf("postgres://%s:***@%s:%d/%s", es.user, es.host, es.port, es.dbName)
}
func (es *eventStorePostgres) Info(ctx context.Context) (*comby.EventStoreInfoModel, error) {
// run extra total query (no args to not using prepared statement)
row := es.db.QueryRowContext(ctx, "SELECT COUNT(uuid) FROM events;")
if err := row.Err(); err != nil {
return nil, err
}
// extract record
var dbTotal int64
if err := row.Scan(&dbTotal); err != nil {
return nil, err
}
// run extra total query (no args to not using prepared statement)
row = es.db.QueryRowContext(ctx, "SELECT COALESCE(MAX(created_at), 0) FROM events;")
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.EventStoreInfoModel{
StoreType: "postgres",
LastItemCreatedAt: dbLastCreatedAt,
NumItems: dbTotal,
ConnectionInfo: fmt.Sprintf("postgres://%s:***@%s:%d/%s", es.user, es.host, es.port, es.dbName),
}, nil
}
func (es *eventStorePostgres) Reset(ctx context.Context) error {
if es.options.ReadOnly {
return fmt.Errorf("'%s' failed to reset - instance is readonly", es.String())
}
query := "TRUNCATE TABLE events CASCADE;"
if _, err := es.db.Exec(query); err != nil {
return err
}
return nil
}
func (es *eventStorePostgres) encryptDomainData(dbRecord *internal.Event) error {
if es.options.CryptoService == nil {
return fmt.Errorf("'%s' failed - crypto service is nil", es.String())
}
domainData := []byte(dbRecord.DataBytes)
if len(domainData) < 1 {
return fmt.Errorf("'%s' failed - domain data is empty", es.String())
}
if encryptedData, err := es.options.CryptoService.Encrypt(domainData); err != nil {
return fmt.Errorf("'%s' failed - failed to encrypt domain data: %w", es.String(), err)
} else {
dbRecord.DataBytes = hex.EncodeToString(encryptedData)
}
return nil
}
func (es *eventStorePostgres) decryptDomainData(dbRecord *internal.Event) error {
if es.options.CryptoService == nil {
return fmt.Errorf("'%s' failed - crypto service is nil", es.String())
}
encryptedData, err := hex.DecodeString(dbRecord.DataBytes)
if err != nil {
return fmt.Errorf("'%s' failed - failed to decode hex domain data: %w", es.String(), err)
}
if len(encryptedData) < 1 {
return fmt.Errorf("'%s' failed - encrypted domain data is empty", es.String())
}
if decryptedData, err := es.options.CryptoService.Decrypt(encryptedData); err != nil {
return fmt.Errorf("'%s' failed - failed to decrypt domain data: %w", es.String(), err)
} else {
dbRecord.DataBytes = string(decryptedData)
}
return nil
}