-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
47 lines (37 loc) · 907 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
46
47
package sql
import (
"fmt"
"github.com/digital-feather/cryptellation/services/candlesticks/internal/infrastructure/db/sql/entities"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type DB struct {
client *gorm.DB
config Config
}
func New() (*DB, error) {
var c Config
if err := c.Load().Validate(); err != nil {
return nil, fmt.Errorf("loading sqldb config: %w", err)
}
client, err := gorm.Open(postgres.Open(c.URL()), DefaultGormConfig)
if err != nil {
return nil, fmt.Errorf("opening sqldb connection: %w", err)
}
db := &DB{
client: client,
config: c,
}
return db, nil
}
func (d *DB) Reset() error {
entities := []interface{}{
&entities.Candlestick{},
}
for _, entity := range entities {
if err := d.client.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(entity).Error; err != nil {
return fmt.Errorf("emptying %T table: %w", entity, err)
}
}
return nil
}