mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
clean up legacy db connection handling with the new database service
This commit is contained in:
parent
276b6c1e48
commit
99b56003eb
|
@ -3,19 +3,4 @@ package bbgo
|
|||
import (
|
||||
// register the go 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)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,12 +3,12 @@ package cmd
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/accounting/pnl"
|
||||
"github.com/c9s/bbgo/pkg/backtest"
|
||||
|
@ -96,10 +96,6 @@ var BacktestCmd = &cobra.Command{
|
|||
return err
|
||||
}
|
||||
|
||||
db, err := bbgo.ConnectMySQL(viper.GetString("mysql-url"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if userConfig.Backtest == nil {
|
||||
return errors.New("backtest config is not defined")
|
||||
|
@ -116,14 +112,14 @@ var BacktestCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
environ := bbgo.NewEnvironment()
|
||||
if viper.IsSet("mysql-url") {
|
||||
dsn := viper.GetString("mysql-url")
|
||||
if err := environ.ConfigureDatabase(ctx, "mysql", dsn); err != nil {
|
||||
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
|
||||
err := environ.ConfigureDatabase(ctx, "mysql", dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
backtestService := &service.BacktestService{DB: db}
|
||||
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
|
||||
|
||||
if wantSync {
|
||||
log.Info("starting synchronization...")
|
||||
|
|
|
@ -2,13 +2,13 @@ package cmd
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/accounting"
|
||||
"github.com/c9s/bbgo/pkg/accounting/pnl"
|
||||
|
@ -51,20 +51,22 @@ var PnLCmd = &cobra.Command{
|
|||
return err
|
||||
}
|
||||
|
||||
db, err := bbgo.ConnectMySQL(viper.GetString("mysql-url"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tradeService := &service.TradeService{DB: db}
|
||||
environ := bbgo.NewEnvironment()
|
||||
if dsn, ok := os.LookupEnv("MYSQL_URL"); ok {
|
||||
err := environ.ConfigureDatabase(ctx, "mysql", dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var trades []types.Trade
|
||||
tradingFeeCurrency := exchange.PlatformFeeCurrency()
|
||||
if strings.HasPrefix(symbol, tradingFeeCurrency) {
|
||||
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 {
|
||||
trades, err = tradeService.Query(service.QueryTradesOptions{
|
||||
trades, err = environ.TradeService.Query(service.QueryTradesOptions{
|
||||
Exchange: exchange.Name(),
|
||||
Symbol: symbol,
|
||||
})
|
||||
|
|
|
@ -2,6 +2,7 @@ package server
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"os"
|
||||
"syscall"
|
||||
|
@ -29,7 +30,7 @@ func (s *Server) setupTestDB(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
db, err := bbgo.ConnectMySQL(dsn)
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
@ -142,4 +143,3 @@ func (s *Server) setupRestart(c *gin.Context) {
|
|||
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user