Skip to content

Commit

Permalink
fix sql insert
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Nov 6, 2024
1 parent a14591f commit 299e96e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
34 changes: 11 additions & 23 deletions kong/clustering/services/sync/hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,20 @@ local function gen_delta(entity, name, options, ws_id, is_delete)
assert(schema:validate_primary_key(pk))

local delta = {
{
type = name,
pk = pk,
ws_id = ws_id,
entity = is_delete and ngx_null or entity,
},
}

return delta
end


function _M:entity_delta_writer(entity, name, options, ws_id, is_delete)
local d = gen_delta(entity, name, options, ws_id, is_delete)
local deltas = { gen_delta(entity, name, options, ws_id, is_delete) }

local res, err = self.strategy:insert_delta(d)
local res, err = self.strategy:insert_delta(deltas)
if not res then
self.strategy:cancel_txn()
return nil, err
Expand Down Expand Up @@ -170,36 +168,26 @@ function _M:register_dao_hooks()

-- set lmdb value to ngx_null then return entity

local d = gen_delta(entity, name, options, ws_id, true)

local res, err = self.strategy:insert_delta(d)
if not res then
self.strategy:cancel_txn()
return nil, err
end

-- only one entity deleted
if not cascade_entries then
goto done
end
local deltas = { gen_delta(entity, name, options, ws_id, true) }

-- delete other related entities
for _, item in ipairs(cascade_entries) do
for i, item in ipairs(cascade_entries or EMPTY) do
local e = item.entity
local name = item.dao.schema.name

ngx_log(ngx_DEBUG, "[kong.sync.v2] new delta due to cascade deleting ", name)

local d = gen_delta(e, name, options, e.ws_id, true)

local res, err = self.strategy:insert_delta(d)
if not res then
self.strategy:cancel_txn()
return nil, err
end
-- #1 item is initial entity
deltas[i + 1] = d
end

::done::
local res, err = self.strategy:insert_delta(deltas)
if not res then
self.strategy:cancel_txn()
return nil, err
end

res, err = self.strategy:commit_txn()
if not res then
Expand Down
13 changes: 12 additions & 1 deletion kong/clustering/services/sync/strategies/postgres.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,27 @@ local NEW_VERSION_QUERY = [[
-- }
function _M:insert_delta(deltas)
local buf = buffer.new()
for _, d in ipairs(deltas) do

local count = #deltas
for i = 1, count do
local d = deltas[i]

buf:putf("(new_version, %s, %s, %s, %s)",
self.connector:escape_literal(d.type),
self.connector:escape_literal(cjson_encode(d.pk)),
self.connector:escape_literal(d.ws_id or kong.default_workspace),
self.connector:escape_literal(cjson_encode(d.entity)))

-- sql values should be separated by comma
if i < count then
buf:put(",")
end
end

local sql = string_format(NEW_VERSION_QUERY, buf:get())

ngx_log(ngx_DEBUG, "[incremental] insert_delta sql: ", sql)

return self.connector:query(sql)
end

Expand Down

0 comments on commit 299e96e

Please sign in to comment.