Skip to content
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

fix(postgres) ensure APIs created_at has ms precision #2924

Merged
merged 1 commit into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kong/core/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ local function build_router(dao, version)
end
end

table.sort(apis, function(api_a, api_b)
return api_a.created_at < api_b.created_at
end)

router, err = Router.new(apis)
if not router then
return nil, "could not create router: " .. err
Expand Down
6 changes: 3 additions & 3 deletions kong/dao/db/postgres.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ local function deserialize_timestamps(self, row, schema)
for k, v in pairs(schema.fields) do
if v.type == "timestamp" and result[k] then
local query = fmt([[
SELECT (extract(epoch from timestamp '%s')*1000)::bigint as %s;
SELECT (extract(epoch from timestamp '%s') * 1000) as %s;
]], result[k], k)
local res, err = self:query(query)
if not res then return nil, err
Expand All @@ -363,8 +363,8 @@ local function serialize_timestamps(self, tbl, schema)
for k, v in pairs(schema.fields) do
if v.type == "timestamp" and result[k] then
local query = fmt([[
SELECT to_timestamp(%d/1000) at time zone 'UTC' as %s;
]], result[k], k)
SELECT to_timestamp(%f) at time zone 'UTC' as %s;
]], result[k] / 1000, k)
local res, err = self:query(query)
if not res then return nil, err
elseif #res <= 1 then
Expand Down
2 changes: 1 addition & 1 deletion kong/dao/migrations/postgres.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ return {
strip_request_path boolean NOT NULL,
upstream_url text,
preserve_host boolean NOT NULL,
created_at timestamp without time zone default (CURRENT_TIMESTAMP(0) at time zone 'utc')
created_at timestamp without time zone default (CURRENT_TIMESTAMP(3) at time zone 'utc')
);
DO $$
BEGIN
Expand Down
54 changes: 54 additions & 0 deletions spec/02-integration/05-proxy/01-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,60 @@ describe("Router", function()
end)
end)

describe("URI regexes order of evaluation", function()
setup(function()
helpers.dao:truncate_tables()

assert(helpers.dao.apis:insert {
name = "api-1",
uris = { "/status/(re)" },
upstream_url = helpers.mock_upstream_url .. "/status/200",
})

ngx.sleep(0.001)

assert(helpers.dao.apis:insert {
name = "api-2",
uris = { "/status/(r)" },
upstream_url = helpers.mock_upstream_url .. "/status/200",
})

ngx.sleep(0.001)

assert(helpers.dao.apis:insert {
name = "api-3",
uris = { "/status" },
upstream_url = helpers.mock_upstream_url .. "/status/200",
})

assert(helpers.start_kong({
nginx_conf = "spec/fixtures/custom_nginx.template",
}))
end)

teardown(function()
helpers.stop_kong()
end)

it("depends on created_at field", function()
local res = assert(client:send {
method = "GET",
path = "/status/r",
headers = { ["kong-debug"] = 1 },
})
assert.res_status(200, res)
assert.equal("api-2", res.headers["kong-api-name"])

res = assert(client:send {
method = "GET",
path = "/status/re",
headers = { ["kong-debug"] = 1 },
})
assert.res_status(200, res)
assert.equal("api-1", res.headers["kong-api-name"])
end)
end)

describe("URI arguments (querystring)", function()

setup(function()
Expand Down