From 295891a17a6af3168be93c7da5e88300ac04c161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 10 Jun 2022 14:30:30 +0200 Subject: [PATCH] Manager: ensure Gorm-generated timestamps are in UTC SQLite should store all timestamps in UTC, as the database is woefully unaware of timezones and will compare lexicographically. --- internal/manager/persistence/db.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/manager/persistence/db.go b/internal/manager/persistence/db.go index ac61e91f..489b7911 100644 --- a/internal/manager/persistence/db.go +++ b/internal/manager/persistence/db.go @@ -49,7 +49,8 @@ func openDB(ctx context.Context, dsn string) (*DB, error) { dblogger := NewDBLogger(log.Level(globalLogLevel)) config := gorm.Config{ - Logger: dblogger, + Logger: dblogger, + NowFunc: nowFunc, } return openDBWithConfig(dsn, &config) @@ -79,6 +80,12 @@ func openDBWithConfig(dsn string, config *gorm.Config) (*DB, error) { return &db, nil } +// nowFunc returns 'now' in UTC, so that GORM-managed times (createdAt, +// deletedAt, updatedAt) are stored in UTC. +func nowFunc() time.Time { + return time.Now().UTC() +} + // PeriodicMaintenanceLoop periodically vacuums the database. // This function only returns when the context is done. func (db *DB) PeriodicMaintenanceLoop(ctx context.Context) {