From 7e9dbf99f1dc577556e9dba07d739883939ae22d Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Fri, 8 Nov 2024 13:33:09 +0800 Subject: [PATCH] fix(dbless): construct right pk string for entity with multiple primary 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 --- kong/db/schema/others/declarative_config.lua | 26 +++++++++++++------- kong/tools/request_aware_table.lua | 4 +++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/kong/db/schema/others/declarative_config.lua b/kong/db/schema/others/declarative_config.lua index 42b165f7e7ff..7cae8ffe3c64 100644 --- a/kong/db/schema/others/declarative_config.lua +++ b/kong/db/schema/others/declarative_config.lua @@ -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 @@ -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 diff --git a/kong/tools/request_aware_table.lua b/kong/tools/request_aware_table.lua index beaa08f4b6e9..3a1cc8894373 100644 --- a/kong/tools/request_aware_table.lua +++ b/kong/tools/request_aware_table.lua @@ -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")