refactor database configuration with env vars

This commit is contained in:
c9s 2021-02-06 11:33:49 +08:00
parent 51d399a49d
commit dd9dbee903
7 changed files with 36 additions and 31 deletions

View File

@ -112,11 +112,9 @@ var BacktestCmd = &cobra.Command{
}
environ := bbgo.NewEnvironment()
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
err := environ.ConfigureDatabase(ctx, "mysql", dsn)
if err != nil {
return err
}
if err := configureDB(ctx, environ) ; err != nil {
return err
}
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
@ -289,3 +287,24 @@ func InBaseAsset(balances types.BalanceMap, market types.Market, price float64)
base := balances[market.BaseCurrency]
return (base.Locked.Float64() + base.Available.Float64()) + ((quote.Locked.Float64() + quote.Available.Float64()) / price)
}
// configureDB configures the database service based on the environment variable
func configureDB(ctx context.Context, environ *bbgo.Environment) error {
if driver, ok := os.LookupEnv("DB_DRIVER") ; ok {
if dsn, ok := os.LookupEnv("DB_DSN") ; ok {
return environ.ConfigureDatabase(ctx, driver, dsn)
}
} else if dsn, ok := os.LookupEnv("SQLITE3_DSN"); ok {
return environ.ConfigureDatabase(ctx, "sqlite3", dsn)
} else if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
return environ.ConfigureDatabase(ctx, "mysql", dsn)
}
return nil
}

View File

@ -3,7 +3,6 @@ package cmd
import (
"context"
"fmt"
"os"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -63,12 +62,8 @@ var CancelCmd = &cobra.Command{
}
environ := bbgo.NewEnvironment()
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
err := environ.ConfigureDatabase(ctx, "mysql", dsn)
if err != nil {
return err
}
if err := configureDB(ctx, environ) ; err != nil {
return err
}
if err := environ.AddExchangesFromConfig(userConfig); err != nil {

View File

@ -2,7 +2,6 @@ package cmd
import (
"context"
"os"
"strings"
"time"
@ -53,11 +52,8 @@ var PnLCmd = &cobra.Command{
environ := bbgo.NewEnvironment()
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
err := environ.ConfigureDatabase(ctx, "mysql", dsn)
if err != nil {
return err
}
if err := configureDB(ctx, environ) ; err != nil {
return err
}
var trades []types.Trade

View File

@ -96,10 +96,8 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
}
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
if err := environ.ConfigureDatabase(ctx, "mysql", dsn); err != nil {
return err
}
if err := configureDB(ctx, environ) ; err != nil {
return err
}
if err := environ.AddExchangesFromConfig(userConfig); err != nil {

View File

@ -9,7 +9,6 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/bbgo"
)
@ -52,12 +51,8 @@ var SyncCmd = &cobra.Command{
}
environ := bbgo.NewEnvironment()
if viper.IsSet("mysql-url") {
dsn := viper.GetString("mysql-url")
if err := environ.ConfigureDatabase(ctx, "mysql", dsn); err != nil {
return err
}
if err := configureDB(ctx, environ) ; err != nil {
return err
}
if err := environ.AddExchangesFromConfig(userConfig); err != nil {

View File

@ -59,7 +59,8 @@ func (s *Server) setupConfigureDB(c *gin.Context) {
return
}
if err := s.Environ.ConfigureDatabase(c, "mysql", dsn); err != nil {
driver := "mysql"
if err := s.Environ.ConfigureDatabase(c, driver, dsn); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

View File

@ -48,7 +48,7 @@ func (s *DatabaseService) Upgrade(ctx context.Context) error {
}
loader := &rockhopper.GoMigrationLoader{}
migrations, err := loader.Load()
migrations, err := loader.LoadByPackageSuffix(s.Driver)
if err != nil {
return err
}
@ -74,6 +74,7 @@ func ReformatMysqlDSN(dsn string) (string, error) {
return "", err
}
// we need timestamp and datetime fields to be parsed into time.Time struct
config.ParseTime = true
dsn = config.FormatDSN()
return dsn, nil