Skip to content

Commit

Permalink
Merge pull request #1660 from c9s/c9s/fix-trade-insertion
Browse files Browse the repository at this point in the history
FIX: fix trade insertion for inserted_at field
  • Loading branch information
c9s authored Jun 19, 2024
2 parents 26d0fed + 6cdf991 commit 6be3855
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion migrations/mysql/20240531163411_trades_created.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME DEFAULT CURRENT_TIMESTAMP
-- +end

-- +begin
UPDATE `trades` SET `inserted_at`=`traded_at`;
UPDATE `trades` SET `inserted_at` = `traded_at`;
-- +end

-- +down
Expand Down
2 changes: 1 addition & 1 deletion pkg/bbgo/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ func (environ *Environment) syncSession(
log.Infof("syncing symbols %v from session %s", symbols, session.Name)

syncBufferPeriod := -defaultSyncBufferPeriod
if environ.environmentConfig.SyncBufferPeriod != nil {
if environ.environmentConfig != nil && environ.environmentConfig.SyncBufferPeriod != nil {
syncBufferPeriod = -environ.environmentConfig.SyncBufferPeriod.Duration()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/mysql/main_20240531163411_trades_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func up_main_tradesCreated(ctx context.Context, tx rockhopper.SQLExecutor) (err
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "UPDATE `trades` SET `inserted_at`=`traded_at`;")
_, err = tx.ExecContext(ctx, "UPDATE `trades` SET `inserted_at` = `traded_at`;")
if err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/service/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,24 @@ func placeholdersOf(record interface{}) []string {
rt = rt.Elem()
}

vt := reflect.ValueOf(record)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}

if rt.Kind() != reflect.Struct {
return nil
}

var dbFields []string
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
fieldValue := vt.Field(i)

if fieldType.Type.Kind() == reflect.Ptr && fieldValue.IsNil() {
continue
}

if tag, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" || tag == "-" || tag == "" {
continue
Expand All @@ -57,13 +68,25 @@ func fieldsNamesOf(record interface{}) []string {
rt = rt.Elem()
}

vt := reflect.ValueOf(record)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}

if rt.Kind() != reflect.Struct {
return nil
}

var dbFields []string
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
fieldValue := vt.Field(i)

// skip value=nil field
if fieldType.Type.Kind() == reflect.Ptr && fieldValue.IsNil() {
continue
}

if tag, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" || tag == "-" || tag == "" {
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error)
func (s *TradeService) Load(ctx context.Context, id int64) (*types.Trade, error) {
var trade types.Trade

rows, err := s.DB.NamedQuery("SELECT * FROM trades WHERE id = :id", map[string]interface{}{
rows, err := s.DB.NamedQueryContext(ctx, "SELECT * FROM trades WHERE id = :id", map[string]interface{}{
"id": id,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type Trade struct {
// PnL is the profit and loss value of the executed trade
PnL sql.NullFloat64 `json:"pnl" db:"pnl"`

InsertedAt Time `json:"insertedAt" db:"inserted_at"`
InsertedAt *Time `json:"insertedAt" db:"inserted_at"`
}

func (trade Trade) CsvHeader() []string {
Expand Down

0 comments on commit 6be3855

Please sign in to comment.