Skip to content

Commit

Permalink
fix(model) postgres strategy uses timestamp with time zone now instea…
Browse files Browse the repository at this point in the history
…d of without (and is more explicit about 'UTC')
  • Loading branch information
bungle authored and thibaultcha committed Feb 20, 2018
1 parent 38b9ea0 commit 5de5803
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
17 changes: 9 additions & 8 deletions kong/dao/migrations/postgres.lua
Original file line number Diff line number Diff line change
Expand Up @@ -637,31 +637,32 @@ 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[],
"paths" TEXT[],
"regex_priority" BIGINT,
"strip_path" BOOLEAN,
"preserve_host" BOOLEAN,
"service_id" UUID REFERENCES "services" ("id")
"service_id" UUID REFERENCES "services" ("id")
);
DO $$
BEGIN
Expand Down
7 changes: 7 additions & 0 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions kong/db/strategies/postgres/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5de5803

Please sign in to comment.