clean up legacy db connection handling with the new database service

This commit is contained in:
c9s 2021-02-06 09:56:26 +08:00
parent 276b6c1e48
commit 99b56003eb
4 changed files with 17 additions and 34 deletions

View File

@ -3,19 +3,4 @@ package bbgo
import ( import (
// register the go migrations // register the go migrations
_ "github.com/c9s/bbgo/pkg/migrations" _ "github.com/c9s/bbgo/pkg/migrations"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
) )
func ConnectMySQL(dsn string) (*sqlx.DB, error) {
config, err := mysql.ParseDSN(dsn)
if err != nil {
return nil, err
}
config.ParseTime = true
dsn = config.FormatDSN()
return sqlx.Connect("mysql", dsn)
}

View File

@ -3,12 +3,12 @@ package cmd
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/accounting/pnl" "github.com/c9s/bbgo/pkg/accounting/pnl"
"github.com/c9s/bbgo/pkg/backtest" "github.com/c9s/bbgo/pkg/backtest"
@ -96,10 +96,6 @@ var BacktestCmd = &cobra.Command{
return err return err
} }
db, err := bbgo.ConnectMySQL(viper.GetString("mysql-url"))
if err != nil {
return err
}
if userConfig.Backtest == nil { if userConfig.Backtest == nil {
return errors.New("backtest config is not defined") return errors.New("backtest config is not defined")
@ -116,14 +112,14 @@ var BacktestCmd = &cobra.Command{
} }
environ := bbgo.NewEnvironment() environ := bbgo.NewEnvironment()
if viper.IsSet("mysql-url") { if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
dsn := viper.GetString("mysql-url") err := environ.ConfigureDatabase(ctx, "mysql", dsn)
if err := environ.ConfigureDatabase(ctx, "mysql", dsn); err != nil { if err != nil {
return err return err
} }
} }
backtestService := &service.BacktestService{DB: db} backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
if wantSync { if wantSync {
log.Info("starting synchronization...") log.Info("starting synchronization...")

View File

@ -2,13 +2,13 @@ package cmd
import ( import (
"context" "context"
"os"
"strings" "strings"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/accounting" "github.com/c9s/bbgo/pkg/accounting"
"github.com/c9s/bbgo/pkg/accounting/pnl" "github.com/c9s/bbgo/pkg/accounting/pnl"
@ -51,20 +51,22 @@ var PnLCmd = &cobra.Command{
return err return err
} }
db, err := bbgo.ConnectMySQL(viper.GetString("mysql-url"))
environ := bbgo.NewEnvironment()
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
err := environ.ConfigureDatabase(ctx, "mysql", dsn)
if err != nil { if err != nil {
return err return err
} }
}
tradeService := &service.TradeService{DB: db}
var trades []types.Trade var trades []types.Trade
tradingFeeCurrency := exchange.PlatformFeeCurrency() tradingFeeCurrency := exchange.PlatformFeeCurrency()
if strings.HasPrefix(symbol, tradingFeeCurrency) { if strings.HasPrefix(symbol, tradingFeeCurrency) {
log.Infof("loading all trading fee currency related trades: %s", symbol) log.Infof("loading all trading fee currency related trades: %s", symbol)
trades, err = tradeService.QueryForTradingFeeCurrency(exchange.Name(), symbol, tradingFeeCurrency) trades, err = environ.TradeService.QueryForTradingFeeCurrency(exchange.Name(), symbol, tradingFeeCurrency)
} else { } else {
trades, err = tradeService.Query(service.QueryTradesOptions{ trades, err = environ.TradeService.Query(service.QueryTradesOptions{
Exchange: exchange.Name(), Exchange: exchange.Name(),
Symbol: symbol, Symbol: symbol,
}) })

View File

@ -2,6 +2,7 @@ package server
import ( import (
"context" "context"
"database/sql"
"net/http" "net/http"
"os" "os"
"syscall" "syscall"
@ -29,7 +30,7 @@ func (s *Server) setupTestDB(c *gin.Context) {
return return
} }
db, err := bbgo.ConnectMySQL(dsn) db, err := sql.Open("mysql", dsn)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
@ -142,4 +143,3 @@ func (s *Server) setupRestart(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true}) c.JSON(http.StatusOK, gin.H{"success": true})
} }