From 5de5803698993eabefcc710c48c400e939a7c609 Mon Sep 17 00:00:00 2001 From: Aapo Talvensaari Date: Wed, 15 Nov 2017 23:31:05 +0200 Subject: [PATCH] fix(model) postgres strategy uses timestamp with time zone now instead of without (and is more explicit about 'UTC') --- kong/dao/migrations/postgres.lua | 17 +++++++++-------- kong/db/strategies/postgres/connector.lua | 7 +++++++ kong/db/strategies/postgres/init.lua | 8 ++++---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/kong/dao/migrations/postgres.lua b/kong/dao/migrations/postgres.lua index 858054853c50..3eba6fed7f55 100644 --- a/kong/dao/migrations/postgres.lua +++ b/kong/dao/migrations/postgres.lua @@ -637,23 +637,24 @@ return { name = "2017-09-14-121200_routes_and_services", up = [[ CREATE TABLE IF NOT EXISTS "services" ( - "id" UUID PRIMARY KEY, - "created_at" TIMESTAMP, - "updated_at" TIMESTAMP, + "id" UUID PRIMARY KEY, + "created_at" TIMESTAMP WITH TIME ZONE, + "updated_at" TIMESTAMP WITH TIME ZONE, "name" TEXT, + "retries" BIGINT, "protocol" TEXT, "host" TEXT, "port" BIGINT, "path" TEXT, - "retries" BIGINT, "connect_timeout" BIGINT, "write_timeout" BIGINT, "read_timeout" BIGINT ); + CREATE TABLE IF NOT EXISTS "routes" ( - "id" UUID PRIMARY KEY, - "created_at" TIMESTAMP, - "updated_at" TIMESTAMP, + "id" UUID PRIMARY KEY, + "created_at" TIMESTAMP WITH TIME ZONE, + "updated_at" TIMESTAMP WITH TIME ZONE, "protocols" TEXT[], "methods" TEXT[], "hosts" TEXT[], @@ -661,7 +662,7 @@ return { "regex_priority" BIGINT, "strip_path" BOOLEAN, "preserve_host" BOOLEAN, - "service_id" UUID REFERENCES "services" ("id") + "service_id" UUID REFERENCES "services" ("id") ); DO $$ BEGIN diff --git a/kong/db/strategies/postgres/connector.lua b/kong/db/strategies/postgres/connector.lua index 4b8404c3cf3f..f3355526638a 100644 --- a/kong/db/strategies/postgres/connector.lua +++ b/kong/db/strategies/postgres/connector.lua @@ -128,6 +128,13 @@ function _mt:connect() self.connection = db + if db.sock:getreusedtimes() == 0 then + ok, err = self:query("SET TIME ZONE 'UTC';"); + if not ok then + return nil, err + end + end + return true end diff --git a/kong/db/strategies/postgres/init.lua b/kong/db/strategies/postgres/init.lua index a976f9cff0bb..844493b0b246 100644 --- a/kong/db/strategies/postgres/init.lua +++ b/kong/db/strategies/postgres/init.lua @@ -213,7 +213,7 @@ local function escape_identifier(connector, identifier, field) if field then if field.timestamp then - return concat { "EXTRACT(EPOCH FROM ", identifier, ") AS ", identifier } + return concat { "EXTRACT(EPOCH FROM ", identifier, " AT TIME ZONE 'UTC') AS ", identifier } end end @@ -287,7 +287,7 @@ end local function field_type_to_postgres_type(field) if field.timestamp then - return "TIMESTAMP" + return "TIMESTAMP WITH TIME ZONE" elseif field.uuid then return "UUID" @@ -311,7 +311,7 @@ local function field_type_to_postgres_type(field) local elements = field.elements if elements.timestamp then - return "TIMESTAMP[]", 1 + return "TIMESTAMP[] WITH TIME ZONE", 1 elseif field.uuid then return "UUID[]", 1 @@ -343,7 +343,7 @@ local function field_type_to_postgres_type(field) local brackets = rep("[]", dm) if el.timestamp then - return "TIMESTAMP" .. brackets, dm + return "TIMESTAMP" .. brackets .. " WITH TIME ZONE", dm elseif field.uuid then return "UUID" .. brackets, dm