From f35c3d316732d9229b5bd17a6d85bc9c652382b1 Mon Sep 17 00:00:00 2001 From: Chrono Date: Thu, 31 Oct 2024 17:12:24 +0800 Subject: [PATCH] refactor(dbless): clean logic of select_by_field (#13817) * refactor(dbless): clean logic of selec_by_field * check schema_field --- kong/db/strategies/off/init.lua | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/kong/db/strategies/off/init.lua b/kong/db/strategies/off/init.lua index d15a2c6c7aa4..a80772224f27 100644 --- a/kong/db/strategies/off/init.lua +++ b/kong/db/strategies/off/init.lua @@ -208,23 +208,16 @@ local function select_by_field(self, field, value, options) _, value = next(value) end - local ws_id + local schema_field = schema.fields[field] + local unique_across_ws = schema_field and schema_field.unique_across_ws - if field == "cache_key" then - -- align with cache_key insertion logic in _set_entity_for_txn - ws_id = get_default_workspace() + -- only accept global query by field if field is unique across workspaces + assert(not options or options.workspace ~= null or unique_across_ws) - 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 + -- align with cache_key insertion logic in _set_entity_for_txn + local ws_id = (unique_across_ws or field == "cache_key") and + get_default_workspace() or + workspace_id(schema, options) local key = unique_field_key(schema.name, ws_id, field, value)