Skip to content

fix(coderd/database): migrate workspaces.last_used_at to timestamptz#9699

Merged
johnstcn merged 8 commits into
mainfrom
cj/workspaces-last-used-at-timestamptz
Sep 18, 2023
Merged

fix(coderd/database): migrate workspaces.last_used_at to timestamptz#9699
johnstcn merged 8 commits into
mainfrom
cj/workspaces-last-used-at-timestamptz

Conversation

@johnstcn

Copy link
Copy Markdown
Member

Partially resolves #9682 (will update users.last_seen_at in a separate PR).

@johnstcn johnstcn self-assigned this Sep 15, 2023
@johnstcn johnstcn requested review from mafredri and mtojek September 15, 2023 14:10
@johnstcn johnstcn marked this pull request as ready for review September 15, 2023 14:10

@mtojek mtojek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

ALTER TABLE ONLY workspaces
ALTER COLUMN last_used_at
SET DATA TYPE timestamp
USING last_used_at::timestamptz,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we want to be 100% correct, this could use an AT TIME ZONE 'UTC' too because during the up migration we are changing the time (correctly) if the db time zone is non-UTC.

This demonstrates how the timestamp would be coerced into the wrong time without

woop=# create temp table testy (ts timestamp, tsz timestamptz);
CREATE TABLE
woop=# insert into testy (ts, tsz) values ('0001-01-01 00:00:00+00', '0001-01-01 00:00:00+00'); select * from testy;
INSERT 0 1
         ts          |             tsz
---------------------+------------------------------
 0001-01-01 00:00:00 | 0001-01-01 01:39:49+01:39:49
(1 row)

woop=# update testy set ts = tsz::timestamptz; select * from testy;
UPDATE 1
         ts          |             tsz
---------------------+------------------------------
 0001-01-01 01:39:49 | 0001-01-01 01:39:49+01:39:49
(1 row)

Whereas with the time zone change:

woop=# truncate table testy;
TRUNCATE TABLE
woop=# insert into testy (ts, tsz) values ('0001-01-01 00:00:00+00', '0001-01-01 00:00:00+00'); select * from testy;
INSERT 0 1
         ts          |             tsz
---------------------+------------------------------
 0001-01-01 00:00:00 | 0001-01-01 01:39:49+01:39:49
(1 row)

woop=# update testy set ts = tsz at time zone 'utc'; select * from testy;
UPDATE 1
         ts          |             tsz
---------------------+------------------------------
 0001-01-01 00:00:00 | 0001-01-01 01:39:49+01:39:49
(1 row)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated!

@johnstcn johnstcn merged commit 9bcff30 into main Sep 18, 2023
@johnstcn johnstcn deleted the cj/workspaces-last-used-at-timestamptz branch September 18, 2023 10:07
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(coderd/database): workspaces.last_used_at is missing timezone component

3 participants