diff --git a/migrations/mysql/20240531163411_trades_created.sql b/migrations/mysql/20240531163411_trades_created.sql index 20cfa3f26..001b267c9 100644 --- a/migrations/mysql/20240531163411_trades_created.sql +++ b/migrations/mysql/20240531163411_trades_created.sql @@ -1,10 +1,10 @@ -- +up -- +begin -ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL AFTER `traded_at`; +ALTER TABLE `trades` ADD COLUMN `inserted_at` DATETIME(3) DEFAULT CURRENT_TIMESTAMP NOT NULL AFTER `traded_at`; -- +end -- +begin -UPDATE `trades` SET `inserted_at`=`traded_at`; +UPDATE `trades` SET `inserted_at` = `traded_at`; -- +end -- +down diff --git a/pkg/service/reflect.go b/pkg/service/reflect.go index 1f8ce68a6..bcf76b08b 100644 --- a/pkg/service/reflect.go +++ b/pkg/service/reflect.go @@ -27,7 +27,9 @@ func tableNameOf(record interface{}) string { } func placeholdersOf(record interface{}) []string { + vt := reflect.ValueOf(record) rt := reflect.TypeOf(record) + if rt.Kind() == reflect.Ptr { rt = rt.Elem() } @@ -39,6 +41,12 @@ func placeholdersOf(record interface{}) []string { 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 @@ -52,7 +60,9 @@ func placeholdersOf(record interface{}) []string { } func fieldsNamesOf(record interface{}) []string { + vt := reflect.ValueOf(record) rt := reflect.TypeOf(record) + if rt.Kind() == reflect.Ptr { rt = rt.Elem() } @@ -64,6 +74,13 @@ func fieldsNamesOf(record interface{}) []string { 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 diff --git a/pkg/types/trade.go b/pkg/types/trade.go index ca465656a..3e9a6c520 100644 --- a/pkg/types/trade.go +++ b/pkg/types/trade.go @@ -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 {