use mysql reformat dsn function to add parseTime parameters

This commit is contained in:
c9s 2021-01-14 14:56:13 +08:00
parent cd9f2ba0e8
commit ad567dc360
9 changed files with 64 additions and 18 deletions

4
go.mod
View File

@ -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

8
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

34
pkg/cmd/migration.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}