Skip to content

Conversation

@Turbo87
Copy link
Member

@Turbo87 Turbo87 commented Feb 17, 2025

It generally appears to be recommended to use timestamptz (timestamp with time zone) instead of timestamp (timestamp without time zone), unless the saved timestamp explicitly should not have a timezone. Since we always treat these timestamps as UTC though, we should be using timestamptz instead.

This commit accordingly also switches NaiveDateTime for DateTime<Utc> in most cases in our codebase. This change also allowed us to remove the rfc3339 serde helper module, since we no longer need the DateTime<Utc> -> NaiveDateTime conversion performed by this module.

It generally appears to be recommended to use `timestamptz` (timestamp with time zone) instead of `timestamp` (timestamp without time zone), unless the saved timestamp explicitly should not have a timezone. Since we always treat these timestamps as UTC though, we should be using `timestamptz` instead.

This commit accordingly also switches `NaiveDateTime` for `DateTime<Utc>` in most cases in our codebase. This change also allowed us to remove the `rfc3339` serde helper module, since we no longer need the `DateTime<Utc> -> NaiveDateTime` conversion performed by this module.
@Turbo87 Turbo87 added C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear A-backend ⚙️ labels Feb 17, 2025
Comment on lines -83 to +80
.set(api_tokens::last_used_at.eq(now.nullable()))
.set(api_tokens::last_used_at.eq(now.into_sql::<Timestamptz>().nullable()))
Copy link
Member Author

Choose a reason for hiding this comment

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

this is unfortunately necessary due to a quirk in diesel. see diesel-rs/diesel#1514 (comment).

@@ -0,0 +1,45 @@
SET timezone = 'UTC';
Copy link
Member Author

Choose a reason for hiding this comment

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

this part is important since Postgres would otherwise perform a full table rewrite. if we set the timezone to UTC, Postgres can take a shortcut and doesn't have to rewrite any data.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this is interesting, which I also wanted to point out :)
Here is a SO link1 for reference that provides more details about this.

Footnotes

  1. https://dba.stackexchange.com/questions/322904/alter-timestamp-column-to-timestamptz-without-converting-data/322907#322907

Copy link
Member Author

Choose a reason for hiding this comment

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

migration started on staging at 2025-02-18T08:28:22.618774Z
migration ended on staging at 2025-02-18T08:28:22.653333Z

35ms seems reasonable IMHO :)

Copy link
Contributor

@eth3lbert eth3lbert left a comment

Choose a reason for hiding this comment

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

I'm good with this, especially since it won't cause a full table rewrite :D

@Turbo87 Turbo87 merged commit 11b1c18 into rust-lang:main Feb 18, 2025
10 checks passed
@Turbo87 Turbo87 deleted the timezones branch February 18, 2025 08:19
Turbo87 added a commit that referenced this pull request Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-backend ⚙️ C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants