Skip to content

Commit

Permalink
Fixes 4860: use timestamptz for date comparisons (#854)
Browse files Browse the repository at this point in the history
Fixes XXX - timezone
  • Loading branch information
jlsherrill authored Oct 18, 2024
1 parent 9f5e098 commit 4187a8a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/dao/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (sDao *snapshotDaoImpl) FetchSnapshotByVersionHref(ctx context.Context, rep

func (sDao *snapshotDaoImpl) FetchSnapshotsModelByDateAndRepository(ctx context.Context, orgID string, request api.ListSnapshotByDateRequest) ([]models.Snapshot, error) {
snaps := []models.Snapshot{}
date := request.Date.Format(time.RFC3339)
date := request.Date.UTC().Format(time.RFC3339)

// finds the snapshot for each repo that is just before (or equal to) our date
beforeQuery := sDao.db.WithContext(ctx).Raw(`
Expand All @@ -368,7 +368,7 @@ func (sDao *snapshotDaoImpl) FetchSnapshotsModelByDateAndRepository(ctx context.
INNER JOIN repository_configurations ON s.repository_configuration_uuid = repository_configurations.uuid
WHERE s.repository_configuration_uuid IN ?
AND repository_configurations.org_id IN ?
AND date_trunc('second', s.created_at::timestamp) <= ?
AND date_trunc('second', s.created_at::timestamptz) <= ?
ORDER BY s.repository_configuration_uuid, s.created_at DESC
`, request.RepositoryUUIDS, []string{orgID, config.RedHatOrg}, date)

Expand All @@ -378,7 +378,7 @@ func (sDao *snapshotDaoImpl) FetchSnapshotsModelByDateAndRepository(ctx context.
INNER JOIN repository_configurations ON s.repository_configuration_uuid = repository_configurations.uuid
WHERE s.repository_configuration_uuid IN ?
AND repository_configurations.org_id IN ?
AND date_trunc('second', s.created_at::timestamp) > ?
AND date_trunc('second', s.created_at::timestamptz) > ?
ORDER BY s.repository_configuration_uuid, s.created_at ASC
`, request.RepositoryUUIDS, []string{orgID, config.RedHatOrg}, date)
// For each repo, pick the oldest of this combined set (ideally the one just before our date, if that doesn't exist, the one after)
Expand Down
11 changes: 11 additions & 0 deletions pkg/dao/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,17 @@ func (s *SnapshotsSuite) TestFetchSnapshotsModelByDateAndRepositoryNew() {
assert.Equal(t, 1, len(response))
assert.Equal(t, second.Base.UUID, response[0].UUID)

// 31 minutes after should still use second, but specify EDT time
tz, err := time.LoadLocation("America/New_York")
require.NoError(t, err)
response, err = sDao.FetchSnapshotsModelByDateAndRepository(context.Background(), repoConfig.OrgID, api.ListSnapshotByDateRequest{
RepositoryUUIDS: []string{repoConfig.UUID},
Date: second.Base.CreatedAt.Add(time.Minute * 31).In(tz),
})
assert.NoError(t, err)
assert.Equal(t, 1, len(response))
assert.Equal(t, second.Base.UUID, response[0].UUID)

// 1 minute before should use first
response, err = sDao.FetchSnapshotsModelByDateAndRepository(context.Background(), repoConfig.OrgID, api.ListSnapshotByDateRequest{
RepositoryUUIDS: []string{repoConfig.UUID},
Expand Down

0 comments on commit 4187a8a

Please sign in to comment.