Skip to content

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

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

Merged
merged 8 commits into from
Sep 18, 2023
2 changes: 1 addition & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE ONLY workspaces
ALTER COLUMN last_used_at
SET DATA TYPE timestamp
USING last_used_at::timestamptz,
Copy link
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
Member Author

Choose a reason for hiding this comment

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

Updated!

ALTER COLUMN last_used_at
SET DEFAULT '0001-01-01 00:00:00'::timestamp;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE ONLY workspaces
ALTER COLUMN last_used_at
SET DATA TYPE timestamptz
USING last_used_at::timestamp AT TIME ZONE 'UTC',
ALTER COLUMN last_used_at
SET DEFAULT '0001-01-01 00:00:00+00:00'::timestamptz;
4 changes: 0 additions & 4 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,12 +1515,8 @@ func TestWorkspaceFilterManual(t *testing.T) {
t.Run("LastUsed", func(t *testing.T) {
t.Parallel()

// nolint:gocritic // https://github.com/coder/coder/issues/9682
db, ps := dbtestutil.NewDB(t, dbtestutil.WithTimezone("UTC"))
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{
IncludeProvisionerDaemon: true,
Database: db,
Pubsub: ps,
})
user := coderdtest.CreateFirstUser(t, client)
authToken := uuid.NewString()
Expand Down
5 changes: 0 additions & 5 deletions enterprise/coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/cryptorand"
Expand Down Expand Up @@ -369,14 +368,10 @@ func TestTemplates(t *testing.T) {
t.Run("UpdateLastUsedAt", func(t *testing.T) {
t.Parallel()

// nolint:gocritic // https://github.com/coder/coder/issues/9682
db, ps := dbtestutil.NewDB(t, dbtestutil.WithTimezone("UTC"))
ctx := testutil.Context(t, testutil.WaitMedium)
client, user := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
IncludeProvisionerDaemon: true,
Database: db,
Pubsub: ps,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
Expand Down