-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.go
59 lines (50 loc) · 1.27 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
package database
import (
"context"
"database/sql/driver"
"io"
"time"
"github.com/dipdup-net/go-lib/config"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// SchemeCommenter -
type SchemeCommenter interface {
MakeTableComment(ctx context.Context, name string, comment string) error
MakeColumnComment(ctx context.Context, tableName string, columnName string, comment string) error
}
// Database -
type Database interface {
Connect(ctx context.Context, cfg config.Database) error
Exec(ctx context.Context, query string, args ...any) (int64, error)
CreateTable(ctx context.Context, model any, opts ...CreateTableOption) error
StateRepository
SchemeCommenter
driver.Pinger
io.Closer
}
// errors
var (
ErrConnectionIsNotInitialized = errors.New("connection is not initialized")
ErrUnsupportedDatabaseType = errors.New("unsupported database type")
)
// Wait -
func Wait(ctx context.Context, db driver.Pinger, checkPeriod time.Duration) {
log.Info().Msg("Waiting database is up and runnning")
if err := db.Ping(ctx); err == nil {
return
}
ticker := time.NewTicker(checkPeriod)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := db.Ping(ctx); err != nil {
log.Warn().Err(err).Msg("waiting...")
continue
}
return
}
}
}