-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c161049
commit c7b329e
Showing
8 changed files
with
380 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"io" | ||
"time" | ||
|
||
"github.com/dipdup-net/go-lib/config" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Database - | ||
type Database interface { | ||
Connect(ctx context.Context, cfg config.Database) error | ||
|
||
StateRepository | ||
|
||
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) { | ||
logrus.Info("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 { | ||
logrus.Warn(err) | ||
continue | ||
} | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/dipdup-net/go-lib/config" | ||
pg "github.com/go-pg/pg/v10" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// PgGo - | ||
type PgGo struct { | ||
conn *pg.DB | ||
} | ||
|
||
// NewPgGo - | ||
func NewPgGo() *PgGo { | ||
return new(PgGo) | ||
} | ||
|
||
// DB - | ||
func (db *PgGo) DB() *pg.DB { | ||
return db.conn | ||
} | ||
|
||
// Connect - | ||
func (db *PgGo) Connect(ctx context.Context, cfg config.Database) error { | ||
if cfg.Kind != config.DBKindPostgres { | ||
return errors.Wrap(ErrUnsupportedDatabaseType, cfg.Kind) | ||
} | ||
var conn *pg.DB | ||
if cfg.Path != "" { | ||
opt, err := pg.ParseURL(cfg.Path) | ||
if err != nil { | ||
return err | ||
} | ||
conn = pg.Connect(opt) | ||
} else { | ||
conn = pg.Connect(&pg.Options{ | ||
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), | ||
User: cfg.User, | ||
Password: cfg.Password, | ||
Database: cfg.Database, | ||
}) | ||
} | ||
db.conn = conn | ||
return nil | ||
} | ||
|
||
// Close - | ||
func (db *PgGo) Close() error { | ||
return db.conn.Close() | ||
} | ||
|
||
// Ping - | ||
func (db *PgGo) Ping(ctx context.Context) error { | ||
if db.conn == nil { | ||
return ErrConnectionIsNotInitialized | ||
} | ||
return db.conn.Ping(ctx) | ||
} | ||
|
||
// State - | ||
func (db *PgGo) State(indexName string) (s State, err error) { | ||
err = db.conn.Model(&s).Where("index_name = ?", indexName).Limit(1).Select() | ||
return | ||
} | ||
|
||
// CreateState - | ||
func (db *PgGo) CreateState(s State) error { | ||
_, err := db.conn.Model(&s).Insert() | ||
return err | ||
} | ||
|
||
// UpdateState - | ||
func (db *PgGo) UpdateState(s State) error { | ||
_, err := db.conn.Model(&s).WherePK().Update() | ||
return err | ||
} | ||
|
||
// DeleteState - | ||
func (db *PgGo) DeleteState(s State) error { | ||
_, err := db.conn.Model(&s).WherePK().Delete() | ||
return err | ||
} |
Oops, something went wrong.