Skip to content

Commit 9297964

Browse files
committed
Make UnixMilli a sql.NullInt64
1 parent cf3a13d commit 9297964

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

pkg/icingadb/ha.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (h *HA) removeOldInstances(s *icingaredisv1.IcingaStatus) {
269269
case <-time.After(timeout):
270270
query := "DELETE FROM icingadb_instance " +
271271
"WHERE id != ? AND environment_id = ? AND endpoint_id = ? AND heartbeat < ?"
272-
heartbeat := types.UnixMilli(time.Now().Add(-timeout))
272+
heartbeat := types.UnixMilli{NullInt64: sql.NullInt64{Valid: true, Int64: utils.UnixMilli(time.Now().Add(-timeout))}}
273273
result, err := h.db.ExecContext(h.ctx, query, h.instanceId, s.EnvironmentID(),
274274
s.EndpointId, heartbeat)
275275
if err != nil {

pkg/types/unix_milli.go

+25-13
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,37 @@ import (
1313
)
1414

1515
// UnixMilli is a nullable millisecond UNIX timestamp in databases and JSON.
16-
type UnixMilli time.Time
16+
type UnixMilli struct {
17+
sql.NullInt64
18+
}
1719

1820
// Time returns the time.Time conversion of UnixMilli.
1921
func (t UnixMilli) Time() time.Time {
20-
return time.Time(t)
22+
return utils.FromUnixMilli(t.Int64)
2123
}
2224

2325
// MarshalJSON implements the json.Marshaler interface.
2426
// Marshals to milliseconds. Supports JSON null.
2527
func (t UnixMilli) MarshalJSON() ([]byte, error) {
26-
if time.Time(t).IsZero() {
28+
if !t.Valid {
2729
return nil, nil
2830
}
2931

30-
return []byte(strconv.FormatInt(utils.UnixMilli(time.Time(t)), 10)), nil
32+
return []byte(strconv.FormatInt(t.Int64, 10)), nil
3133
}
3234

3335
// UnmarshalText implements the encoding.TextUnmarshaler interface.
3436
func (t *UnixMilli) UnmarshalText(text []byte) error {
35-
parsed, err := strconv.ParseFloat(string(text), 64)
37+
ms, err := strconv.ParseFloat(string(text), 64)
3638
if err != nil {
3739
return internal.CantParseFloat64(err, string(text))
3840
}
3941

40-
*t = UnixMilli(utils.FromUnixMilli(int64(parsed)))
42+
*t = UnixMilli{sql.NullInt64{
43+
Int64: int64(ms),
44+
Valid: true,
45+
}}
46+
4147
return nil
4248
}
4349

@@ -52,8 +58,11 @@ func (t *UnixMilli) UnmarshalJSON(data []byte) error {
5258
if err != nil {
5359
return internal.CantParseFloat64(err, string(data))
5460
}
55-
tt := utils.FromUnixMilli(int64(ms))
56-
*t = UnixMilli(tt)
61+
62+
*t = UnixMilli{sql.NullInt64{
63+
Int64: int64(ms),
64+
Valid: true,
65+
}}
5766

5867
return nil
5968
}
@@ -65,24 +74,27 @@ func (t *UnixMilli) Scan(src interface{}) error {
6574
return nil
6675
}
6776

68-
v, ok := src.(int64)
77+
ms, ok := src.(int64)
6978
if !ok {
7079
return errors.Errorf("bad int64 type assertion from %#v", src)
7180
}
72-
tt := utils.FromUnixMilli(v)
73-
*t = UnixMilli(tt)
81+
82+
*t = UnixMilli{sql.NullInt64{
83+
Int64: ms,
84+
Valid: true,
85+
}}
7486

7587
return nil
7688
}
7789

7890
// Value implements the driver.Valuer interface.
7991
// Returns milliseconds. Supports SQL NULL.
8092
func (t UnixMilli) Value() (driver.Value, error) {
81-
if t.Time().IsZero() {
93+
if !t.Valid {
8294
return nil, nil
8395
}
8496

85-
return utils.UnixMilli(t.Time()), nil
97+
return t.Int64, nil
8698
}
8799

88100
// Assert interface compliance.

0 commit comments

Comments
 (0)