Skip to content

Commit

Permalink
refactor _set_entity_for_txn() in import.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Oct 12, 2024
1 parent 79c7bb3 commit 7b5c0db
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions kong/db/declarative/import.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,33 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete)

local item_key = item_key(entity_name, ws_id, pk)

local value, idx_value
-- if we are deleting, item_value and idx_value should be nil
local item_value, idx_value

-- if we are inserting or updating
-- value is serialized entity
-- idx_value is the lmdb item_key
if not is_delete then
local err

-- serialize item with possible nulls
value, err = marshall(item)
if not value then
item_value, err = marshall(item)
if not item_value then
return nil, err
end

idx_value = item_key
end

t:set(item_key, value)
-- store serialized entity into lmdb
t:set(item_key, item_value)

-- select_by_cache_key
if schema.cache_key then
local cache_key = dao:cache_key(item)
local key = unique_field_key(entity_name, ws_id, "cache_key", cache_key)

-- store item_key or nil into lmdb
t:set(key, idx_value)
end

Expand All @@ -296,37 +304,47 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete)
local value = item[fname]

-- value may be null, we should skip it
if value and value ~= null then
local value_str

if fdata.unique then
-- unique and not a foreign key, or is a foreign key, but non-composite
-- see: validate_foreign_key_is_single_primary_key, composite foreign
-- key is currently unsupported by the DAO
if type(value) == "table" then
assert(is_foreign)
value_str = pk_string(kong.db[fdata_reference].schema, value)
end

if fdata.unique_across_ws then
ws_id = kong.default_workspace
end
if not value or value == null then
goto continue
end

local key = unique_field_key(entity_name, ws_id, fname, value_str or value)
t:set(key, idx_value)
end
-- value should be a string or table

if is_foreign then
-- is foreign, generate page_for_foreign_field indexes
assert(type(value) == "table")
local value_str

if fdata.unique then
-- unique and not a foreign key, or is a foreign key, but non-composite
-- see: validate_foreign_key_is_single_primary_key, composite foreign
-- key is currently unsupported by the DAO
if type(value) == "table" then
assert(is_foreign)
value_str = pk_string(kong.db[fdata_reference].schema, value)
end

local key = foreign_field_key(entity_name, ws_id, fname, value_str, pk)
t:set(key, idx_value)
if fdata.unique_across_ws then
ws_id = kong.default_workspace
end

local key = unique_field_key(entity_name, ws_id, fname, value_str or value)

-- store item_key or nil into lmdb
t:set(key, idx_value)
end
end

if is_foreign then
-- is foreign, generate page_for_foreign_field indexes
assert(type(value) == "table")

value_str = pk_string(kong.db[fdata_reference].schema, value)

local key = foreign_field_key(entity_name, ws_id, fname, value_str, pk)

-- store item_key or nil into lmdb
t:set(key, idx_value)
end

::continue::
end -- for fname, fdata in schema:each_field()

return true
end
Expand Down

0 comments on commit 7b5c0db

Please sign in to comment.