1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: change default value for time Fields to CURRENT_TIMESTAMP for better cross database support. Support Scanning String Dates for HeartBeatReqTime

This commit is contained in:
William Flores 2020-04-19 16:58:03 -07:00
parent 7a74ae251c
commit aa211713b2
No known key found for this signature in database
GPG Key ID: E4E51CFD36F843ED
2 changed files with 12 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package models
import (
"database/sql/driver"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
@ -24,7 +25,7 @@ type Heartbeat struct {
IsWrite bool `json:"is_write"`
Editor string `json:"editor"`
OperatingSystem string `json:"operating_system"`
Time HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time,idx_time_user"`
Time HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time,idx_time_user"`
languageRegex *regexp.Regexp
}
@ -62,14 +63,20 @@ func (j *HeartbeatReqTime) UnmarshalJSON(b []byte) error {
func (j *HeartbeatReqTime) Scan(value interface{}) error {
switch value.(type) {
case string:
t, err := time.Parse("2006-01-02 15:04:05-07:00", value.(string))
if err != nil {
return errors.New(fmt.Sprintf("unsupported date time format: %s", value))
}
*j = HeartbeatReqTime(t)
case int64:
*j = HeartbeatReqTime(time.Unix(123456, 0))
*j = HeartbeatReqTime(time.Unix(value.(int64), 0))
break
case time.Time:
*j = HeartbeatReqTime(value.(time.Time))
break
default:
return errors.New("unsupported type")
return errors.New(fmt.Sprintf("unsupported type: %T", value))
}
return nil
}

View File

@ -15,8 +15,8 @@ const (
type Summary struct {
ID uint `json:"-" gorm:"primary_key"`
UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"`
FromTime time.Time `json:"from" gorm:"not null; type:timestamp; default:now(); index:idx_time_summary_user"`
ToTime time.Time `json:"to" gorm:"not null; type:timestamp; default:now(); index:idx_time_summary_user"`
FromTime time.Time `json:"from" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user"`
ToTime time.Time `json:"to" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user"`
Projects []*SummaryItem `json:"projects"`
Languages []*SummaryItem `json:"languages"`
Editors []*SummaryItem `json:"editors"`