Skip to content

Commit

Permalink
fix(dbless): construct right pk string for entity with multiple prima…
Browse files Browse the repository at this point in the history
…ry keys (#13826)

For the entity with multiple primary keys, like consumer_group_consumers, the
pk_string() function construct incorrect pk string like
`table: 0x028a49eda0:table: 0x0289c257b8`. Because it does not extract the
internal id value. Here, we fix it with original lmdb logic
get_cache_key_value(). Note this method directly gets id field name.

https://konghq.atlassian.net/browse/KAG-5732

---------

Co-authored-by: chronolaw <[email protected]>
  • Loading branch information
chobits and chronolaw authored Nov 8, 2024
1 parent 5f5f7d5 commit 7e9dbf9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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,
-- 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

1 comment on commit 7e9dbf9

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:7e9dbf99f1dc577556e9dba07d739883939ae22d
Artifacts available https://github.com/Kong/kong/actions/runs/11736706285

Please sign in to comment.