Merge pull request #1660 from c9s/c9s/fix-trade-insertion

FIX: fix trade insertion for inserted_at field
This commit is contained in:
c9s 2024-06-19 16:18:42 +08:00 committed by GitHub
commit 6be38558e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 28 additions and 5 deletions

View File

@ -4,7 +4,7 @@ ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME DEFAULT CURRENT_TIMESTAMP
-- +end -- +end
-- +begin -- +begin
UPDATE `trades` SET `inserted_at`=`traded_at`; UPDATE `trades` SET `inserted_at` = `traded_at`;
-- +end -- +end
-- +down -- +down

View File

@ -648,7 +648,7 @@ func (environ *Environment) syncSession(
log.Infof("syncing symbols %v from session %s", symbols, session.Name) log.Infof("syncing symbols %v from session %s", symbols, session.Name)
syncBufferPeriod := -defaultSyncBufferPeriod syncBufferPeriod := -defaultSyncBufferPeriod
if environ.environmentConfig.SyncBufferPeriod != nil { if environ.environmentConfig != nil && environ.environmentConfig.SyncBufferPeriod != nil {
syncBufferPeriod = -environ.environmentConfig.SyncBufferPeriod.Duration() syncBufferPeriod = -environ.environmentConfig.SyncBufferPeriod.Duration()
} }

View File

@ -16,7 +16,7 @@ func up_main_tradesCreated(ctx context.Context, tx rockhopper.SQLExecutor) (err
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }

View File

@ -32,6 +32,11 @@ func placeholdersOf(record interface{}) []string {
rt = rt.Elem() rt = rt.Elem()
} }
vt := reflect.ValueOf(record)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}
if rt.Kind() != reflect.Struct { if rt.Kind() != reflect.Struct {
return nil return nil
} }
@ -39,6 +44,12 @@ func placeholdersOf(record interface{}) []string {
var dbFields []string var dbFields []string
for i := 0; i < rt.NumField(); i++ { for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(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, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" || tag == "-" || tag == "" { if tag == "gid" || tag == "-" || tag == "" {
continue continue
@ -57,6 +68,11 @@ func fieldsNamesOf(record interface{}) []string {
rt = rt.Elem() rt = rt.Elem()
} }
vt := reflect.ValueOf(record)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}
if rt.Kind() != reflect.Struct { if rt.Kind() != reflect.Struct {
return nil return nil
} }
@ -64,6 +80,13 @@ func fieldsNamesOf(record interface{}) []string {
var dbFields []string var dbFields []string
for i := 0; i < rt.NumField(); i++ { for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(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, ok := fieldType.Tag.Lookup("db"); ok {
if tag == "gid" || tag == "-" || tag == "" { if tag == "gid" || tag == "-" || tag == "" {
continue continue

View File

@ -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) { func (s *TradeService) Load(ctx context.Context, id int64) (*types.Trade, error) {
var trade types.Trade 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, "id": id,
}) })
if err != nil { if err != nil {

View File

@ -96,7 +96,7 @@ type Trade struct {
// PnL is the profit and loss value of the executed trade // PnL is the profit and loss value of the executed trade
PnL sql.NullFloat64 `json:"pnl" db:"pnl"` 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 { func (trade Trade) CsvHeader() []string {