fix trade insertion for inserted_at field

This commit is contained in:
c9s 2024-06-19 15:59:19 +08:00
parent 6afde4808f
commit 00b9c3156f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 20 additions and 3 deletions

View File

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

View File

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

View File

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