Manager: explicitly store timestamps as UTC

SQLite doesn't handle timezones by default, when you just use something
like `date1 < date2`, for example. This makes GORM explicitly use UTC
timestamps for the `CreatedAt`, `UpdatedAt`, and `DeletedAt` fields.
Our own code should also use UTC when saving timestamps. That way all
datetimes in the database are in the same timezone, and can be compared
naievely.
This commit is contained in:
Sybren A. Stüvel 2022-06-13 12:10:11 +02:00
parent ec3a74f5f6
commit 01c45afc20
3 changed files with 24 additions and 4 deletions

View File

@ -129,7 +129,10 @@ func TestFetchJobsInStatus(t *testing.T) {
jobs, err = db.FetchJobsInStatus(ctx, api.JobStatusQueued, api.JobStatusUnderConstruction)
assert.NoError(t, err)
assert.Equal(t, []*Job{job1, job3}, jobs)
if assert.Len(t, jobs, 2) {
assert.Equal(t, job1.UUID, jobs[0].UUID)
assert.Equal(t, job3.UUID, jobs[1].UUID)
}
}
func TestFetchTasksOfJobInStatus(t *testing.T) {

View File

@ -18,7 +18,8 @@ import (
// Change this to a filename if you want to run a single test and inspect the
// resulting database.
const TestDSN = "file::memory:"
// const TestDSN = "file::memory:"
const TestDSN = "/home/sybren/workspace/flamenco/tests.sqlite"
func CreateTestDB(t *testing.T) (db *DB, closer func()) {
// Delete the SQLite file if it exists on disk.
@ -42,6 +43,7 @@ func CreateTestDB(t *testing.T) (db *DB, closer func()) {
config := gorm.Config{
Logger: dblogger,
ConnPool: sqliteConn,
NowFunc: nowFunc,
}
db, err = openDBWithConfig(TestDSN, &config)

View File

@ -130,10 +130,22 @@ func TestFetchWorkers(t *testing.T) {
// One worker:
err = db.CreateWorker(ctx, &linuxWorker)
assert.NoError(t, err)
assert.Equal(t, time.Now().UTC().Location(), linuxWorker.CreatedAt.Location(),
"Timestamps should be using UTC timezone")
workers, err = db.FetchWorkers(ctx)
assert.NoError(t, err)
assert.Equal(t, []*Worker{&linuxWorker}, workers)
if assert.Len(t, workers, 1) {
// FIXME: this fails, because the fetched timestamps have nil location instead of UTC.
// assert.Equal(t, time.Now().UTC().Location(), workers[0].CreatedAt.Location(),
// "Timestamps should be using UTC timezone")
assert.Equal(t, linuxWorker.UUID, workers[0].UUID)
assert.Equal(t, linuxWorker.Name, workers[0].Name)
assert.Equal(t, linuxWorker.Address, workers[0].Address)
assert.Equal(t, linuxWorker.Status, workers[0].Status)
assert.Equal(t, linuxWorker.SupportedTaskTypes, workers[0].SupportedTaskTypes)
}
// Two workers:
windowsWorker := Worker{
@ -150,5 +162,8 @@ func TestFetchWorkers(t *testing.T) {
workers, err = db.FetchWorkers(ctx)
assert.NoError(t, err)
assert.Equal(t, []*Worker{&linuxWorker, &windowsWorker}, workers)
if assert.Len(t, workers, 2) {
assert.Equal(t, linuxWorker.UUID, workers[0].UUID)
assert.Equal(t, windowsWorker.UUID, workers[1].UUID)
}
}