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(dbless): construct right pk string for entity with multiple primary keys #13826

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 17 additions & 9 deletions kong/db/schema/others/declarative_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ local foreign_children = {}

do
local tb_nkeys = require("table.nkeys")
local request_aware_table = require("kong.tools.request_aware_table")

local CACHED_OUT
local buffer = require("string.buffer")

-- Generate a stable and unique string key from primary key defined inside
-- schema, supports both non-composite and composite primary keys
Expand All @@ -55,17 +53,27 @@ do
return tostring(object[primary_key[1]])
end

if not CACHED_OUT then
CACHED_OUT = request_aware_table.new()
end
local buf = buffer.new()

CACHED_OUT:clear()
-- The logic comes from get_cache_key_value(), which uses `id` directly to
-- extract foreign key.
-- TODO: extract primary key recursively using pk_string(), KAG-5750
for i = 1, count do
local k = primary_key[i]
insert(CACHED_OUT, tostring(object[k]))
local v = object[k]

if type(v) == "table" and schema.fields[k].type == "foreign" then
v = v.id
end

buf:put(tostring(v or ""))

if i < count then
buf:put(":")
end
end

return concat(CACHED_OUT, ":")
return buf:get()
end
end

Expand Down
4 changes: 4 additions & 0 deletions kong/tools/request_aware_table.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
--- NOTE: tool is designed to assist with **detecting** request contamination
-- issues on CI, during test runs. It does not offer security safeguards.
--
-- TODO: need to resolve the following issues in debug mode,
chobits marked this conversation as resolved.
Show resolved Hide resolved
-- 1. `:clear` could not clear elements inserted by `table.insert()`
-- 2. `table.concat()` couldn't work for elements assigned using `indexing`

local table_new = require("table.new")
local table_clear = require("table.clear")
Expand Down
Loading