Skip to content

Commit

Permalink
Set entity columns to be 'timestamp with time zone' to avoid TZ-relat…
Browse files Browse the repository at this point in the history
…ed test failures (#4720)
  • Loading branch information
evankanderson authored Oct 10, 2024
1 parent 11b9b31 commit 2fa96c9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions database/migrations/000107_entity_properties_timestamp_tz.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

BEGIN;

SET timezone = 'UTC';
ALTER TABLE properties
ALTER COLUMN updated_at TYPE TIMESTAMP,
ALTER updated_at SET DEFAULT NOW(); -- Default needs to be set explicitly after type change

ALTER TABLE entity_instances
ALTER created_at TYPE TIMESTAMP,
ALTER created_at SET DEFAULT NOW();

-- There are more TIMESTAMP columns, but these are affecting unit tests in some timezones.

COMMIT;
32 changes: 32 additions & 0 deletions database/migrations/000107_entity_properties_timestamp_tz.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

BEGIN;

-- Postgres only needs to rewrite metadata (and not the column data) when altering
-- a column from timezoneless when timezone='UTC' is set in the session.
-- Ref: https://www.postgresql.org/docs/release/12.0/

SET timezone = 'UTC';
ALTER TABLE properties
ALTER COLUMN updated_at TYPE TIMESTAMPTZ,
ALTER updated_at SET DEFAULT NOW()::timestamptz; -- Default needs to be set explicitly after type change

ALTER TABLE entity_instances
ALTER created_at TYPE TIMESTAMPTZ,
ALTER created_at SET DEFAULT NOW()::timestamptz;

-- There are more TIMESTAMP columns, but these are affecting unit tests in some timezones.

COMMIT;

0 comments on commit 2fa96c9

Please sign in to comment.