Skip to content

Commit

Permalink
fix(incremental sync): fix cache_key handling for select_by_cache_key
Browse files Browse the repository at this point in the history
  • Loading branch information
chobits committed Oct 31, 2024
1 parent 194bf88 commit 651b4ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion kong/db/dao/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ function DAO:cache_key(key, arg2, arg3, arg4, arg5, ws_id)
error("key must be a string or an entity table", 2)
end

if key.ws_id ~= nil and key.ws_id ~= null then
if key.ws_id ~= nil and key.ws_id ~= null and schema.workspaceable then
ws_id = key.ws_id
end

Expand Down
11 changes: 4 additions & 7 deletions kong/db/declarative/import.lua
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,10 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete)
-- select_by_cache_key
if schema.cache_key then
local cache_key = dao:cache_key(item)

for _, wid in ipairs {ws_id, GLOBAL_WORKSPACE_TAG} do
local key = unique_field_key(entity_name, wid, "cache_key", cache_key)

-- store item_key or nil into lmdb
t:set(key, idx_value)
end
-- ws_id is a placeholder here, because cache_key is unique globally.
local _ws_id = get_default_workspace() -- avoid overwriting the outer ws_id
local key = unique_field_key(entity_name, _ws_id, "cache_key", cache_key)
t:set(key, idx_value)
end

for fname, fdata in schema:each_field() do
Expand Down
24 changes: 16 additions & 8 deletions kong/db/strategies/off/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,26 @@ local function select_by_field(self, field, value, options)
_, value = next(value)
end

local ws_id = workspace_id(schema, options)

local key
local unique_across_ws = schema.fields[field].unique_across_ws
-- only accept global query by field if field is unique across workspaces
assert(not options or options.workspace ~= null or unique_across_ws)
local ws_id

if unique_across_ws then
if field == "cache_key" then
-- align with cache_key insertion logic in _set_entity_for_txn
ws_id = get_default_workspace()
-- value = the value generated by `:cache_key()`

else
ws_id = workspace_id(schema, options)

local unique_across_ws = schema.fields[field].unique_across_ws
-- only accept global query by field if field is unique across workspaces
assert(not options or options.workspace ~= null or unique_across_ws)

if unique_across_ws then
ws_id = get_default_workspace()
end
end

key = unique_field_key(schema.name, ws_id, field, value)
local key = unique_field_key(schema.name, ws_id, field, value)

return select_by_key(schema, key, true)
end
Expand Down

0 comments on commit 651b4ac

Please sign in to comment.