Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tapdb: fix time zone issue with timestamps #532

Merged
merged 1 commit into from
Sep 26, 2023
Merged

Conversation

guggero
Copy link
Member

@guggero guggero commented Sep 25, 2023

Fixes an issue where unit tests would fail when being run in certain time zones.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🦚

Only request is some additional detail in the commit message body:

  • Why did the comparison of the UTC vs localized timestamps encounter an issue here?
  • Why didn't the db translation layer handle normalizing the timestamps?
  • For all the other areas we use timestamps, do we always need to map them to UTC before inserting?

@@ -1682,7 +1682,7 @@ func (a *AssetStore) LeaseCoins(ctx context.Context, leaseOwner [32]byte,
err = q.UpdateUTXOLease(ctx, UpdateUTXOLease{
LeaseOwner: leaseOwner[:],
LeaseExpiry: sql.NullTime{
Time: expiry,
Time: expiry.UTC(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for doing this at the lowest level.

Because sqlc by default just uses the time.Time.String() method to turn
a timestamp into the DB value, the time zone of the timestamp has an
impact as any SQL query would basically be a string comparison that
doesn't look at the time zone at all.
For example:

	WHERE a < b

Would be true for

	a = '2023-09-26 14:00:00 GMT-8' (22:00 local time)
	a = '2023-09-26 15:00:00 UTC'   (15:00 local time)

Even though 10pm is clearly after 3pm.

To fix this, we explicitly call .UTC() on any time.Time we use in any
SQL query.

The only instances where that was not yet the case in the whole tapdb
package were the newly added queries for UTXO leases.

It seems that with a later release of the sqlite driver we could
customize this behavior of how a timestamp is being formatted. But for
now the .UTC() workaround should work as well.
@guggero guggero force-pushed the fix-lease-timestamp-utc branch from c8ba4a1 to 05e8ae1 Compare September 26, 2023 11:49
@guggero
Copy link
Member Author

guggero commented Sep 26, 2023

Added a commit message to clarify on this.

It looks like we could customize this by setting ?_time_format=xyz in the DSN query string when initializing the SQLite database (https://gitlab.com/cznic/sqlite/-/blob/v1.25.0/sqlite.go#L347-355). But then we would have different behavior between SQLite and Postgres.
So I think we'll eventually want to solve this on the sqlc level?

Copy link
Contributor

@jharveyb jharveyb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - didn't see any other use of UTC() in other tests.

@Roasbeef Roasbeef added this pull request to the merge queue Sep 26, 2023
Merged via the queue into main with commit 916b595 Sep 26, 2023
@guggero guggero deleted the fix-lease-timestamp-utc branch September 26, 2023 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

3 participants