From ad567dc3603cb6d85ab1eebf1991860e9c33848b Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 14 Jan 2021 14:56:13 +0800 Subject: [PATCH] use mysql reformat dsn function to add parseTime parameters --- go.mod | 4 ++-- go.sum | 8 ++++---- pkg/cmd/backtest.go | 3 ++- pkg/cmd/cancel.go | 2 +- pkg/cmd/cmdutil/db.go | 17 ++++++++++------- pkg/cmd/migration.go | 34 ++++++++++++++++++++++++++++++++++ pkg/cmd/pnl.go | 3 ++- pkg/cmd/run.go | 8 +++++++- pkg/cmd/sync.go | 3 ++- 9 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 pkg/cmd/migration.go diff --git a/go.mod b/go.mod index 4f7e33bbf..7b0ece5f5 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/adshao/go-binance/v2 v2.2.1-0.20210108025425-9a582c63144e github.com/c9s/goose v0.0.0-20200415105707-8da682162a5b - github.com/c9s/rockhopper v1.1.1 + github.com/c9s/rockhopper v1.2.1-0.20210114064926-84f8d06c527b github.com/codingconcepts/env v0.0.0-20200821220118-a8fbf8d84482 github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/go-redis/redis/v8 v8.4.0 @@ -45,7 +45,7 @@ require ( github.com/valyala/fastjson v1.5.1 github.com/x-cray/logrus-prefixed-formatter v0.5.2 golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect - golang.org/x/sys v0.0.0-20210113131315-ba0562f347e0 // indirect + golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 // indirect golang.org/x/text v0.3.5 // indirect golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 gonum.org/v1/gonum v0.8.1 diff --git a/go.sum b/go.sum index 23f812f40..40f4d86a4 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8 github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/c9s/goose v0.0.0-20200415105707-8da682162a5b h1:4qsZTw8wHHTzFnwrfs3zLwz+cU2diGBdwoKRKiWOMvc= github.com/c9s/goose v0.0.0-20200415105707-8da682162a5b/go.mod h1:RaBe6PIVbQRqwrnjjSoHhlLM601JWdT7KZ0p6rhgI7I= -github.com/c9s/rockhopper v1.1.1 h1:0vcrJHgGy/iOHGkTPNyAaKgFx6V8zDbbFSnYUqjvclE= -github.com/c9s/rockhopper v1.1.1/go.mod h1:KJnQjZSrWA83jjwGF/+O7Y96VCVirYTYEvXJJOc6kMU= +github.com/c9s/rockhopper v1.2.1-0.20210114064926-84f8d06c527b h1:D4TpmOWK8GVV7bvazJiW17cwTp1ke8aADrr7BalCB4E= +github.com/c9s/rockhopper v1.2.1-0.20210114064926-84f8d06c527b/go.mod h1:KJnQjZSrWA83jjwGF/+O7Y96VCVirYTYEvXJJOc6kMU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= @@ -435,8 +435,8 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210113131315-ba0562f347e0 h1:rTJ72jiMIolMfCaiZLkdlJLN2Og7LKF6xE1cgwQnwzQ= -golang.org/x/sys v0.0.0-20210113131315-ba0562f347e0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 h1:nVuTkr9L6Bq62qpUqKo/RnZCFfzDBL0bYo6w9OJUqZY= +golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/pkg/cmd/backtest.go b/pkg/cmd/backtest.go index 9b9cbaf83..c46866436 100644 --- a/pkg/cmd/backtest.go +++ b/pkg/cmd/backtest.go @@ -8,6 +8,7 @@ import ( "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" @@ -95,7 +96,7 @@ var BacktestCmd = &cobra.Command{ return err } - db, err := cmdutil.ConnectMySQL() + db, err := cmdutil.ConnectMySQL(viper.GetString("mysql-url")) if err != nil { return err } diff --git a/pkg/cmd/cancel.go b/pkg/cmd/cancel.go index bf17eace7..f7e61f8ef 100644 --- a/pkg/cmd/cancel.go +++ b/pkg/cmd/cancel.go @@ -66,7 +66,7 @@ var CancelCmd = &cobra.Command{ environ := bbgo.NewEnvironment() if viper.IsSet("mysql-url") { - db, err := cmdutil.ConnectMySQL() + db, err := cmdutil.ConnectMySQL(viper.GetString("mysql-url")) if err != nil { return err } diff --git a/pkg/cmd/cmdutil/db.go b/pkg/cmd/cmdutil/db.go index f50c3f7f2..2e9f48e0d 100644 --- a/pkg/cmd/cmdutil/db.go +++ b/pkg/cmd/cmdutil/db.go @@ -1,14 +1,17 @@ package cmdutil import ( - "fmt" - + "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" - "github.com/spf13/viper" ) -func ConnectMySQL() (*sqlx.DB, error) { - mysqlURL := viper.GetString("mysql-url") - mysqlURL = fmt.Sprintf("%s?parseTime=true", mysqlURL) - return sqlx.Connect("mysql", mysqlURL) +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) } diff --git a/pkg/cmd/migration.go b/pkg/cmd/migration.go new file mode 100644 index 000000000..7c17a804c --- /dev/null +++ b/pkg/cmd/migration.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "context" + "database/sql" + + "github.com/c9s/rockhopper" +) + +func upgradeDB(ctx context.Context, driver string, db *sql.DB) error { + dialect, err := rockhopper.LoadDialect(driver) + if err != nil { + return err + } + + loader := &rockhopper.GoMigrationLoader{} + migrations, err := loader.Load() + if err != nil { + return err + } + + rh := rockhopper.New(driver, dialect, db) + + currentVersion, err := rh.CurrentVersion() + if err != nil { + return err + } + + if err := rockhopper.Up(ctx, rh, migrations, currentVersion, 0); err != nil { + return err + } + + return nil +} diff --git a/pkg/cmd/pnl.go b/pkg/cmd/pnl.go index 35fd6756a..e53290eb3 100644 --- a/pkg/cmd/pnl.go +++ b/pkg/cmd/pnl.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" "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" @@ -50,7 +51,7 @@ var PnLCmd = &cobra.Command{ return err } - db, err := cmdutil.ConnectMySQL() + db, err := cmdutil.ConnectMySQL(viper.GetString("mysql-url")) if err != nil { return err } diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 23b5b20c9..9a9afdba6 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -85,10 +85,16 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error { environ := bbgo.NewEnvironment() if viper.IsSet("mysql-url") { - db, err := cmdutil.ConnectMySQL() + dsn := viper.GetString("mysql-url") + db, err := cmdutil.ConnectMySQL(dsn) if err != nil { return err } + + if err := upgradeDB(ctx, "mysql", db.DB); err != nil { + return err + } + environ.SetDB(db) } diff --git a/pkg/cmd/sync.go b/pkg/cmd/sync.go index 65a175b74..6074b360a 100644 --- a/pkg/cmd/sync.go +++ b/pkg/cmd/sync.go @@ -6,6 +6,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/c9s/bbgo/pkg/cmd/cmdutil" "github.com/c9s/bbgo/pkg/service" @@ -46,7 +47,7 @@ var SyncCmd = &cobra.Command{ return err } - db, err := cmdutil.ConnectMySQL() + db, err := cmdutil.ConnectMySQL(viper.GetString("mysql-url")) if err != nil { return err }