Skip to content

Commit

Permalink
fix(schema) changes in uuid generation
Browse files Browse the repository at this point in the history
This commit attempts to solve two problems:
* When updating an entity without including the entity's uuid, the first if would generate an invalid uuid for it (the `if field.uuid ...` branch was not entered, so the `if field.type = "string"` was activated, filling the uuid with a `random_string`

* When converting the old DAO schemas to the new DAO some string values can now be both ngx.null and "". So the auto-generation needs to take that into account and generate also when it encounters "".
  • Loading branch information
kikito committed Oct 3, 2018
1 parent 77eac4e commit e1432a6
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions kong/db/schema/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ end
-- for value creation and update.
-- @param input The table containing data to be processed.
-- @param context a string describing the CRUD context:
-- valid values are: "insert", "update"
-- valid values are: "insert", "update", "upsert"
-- @param nulls boolean: return nulls as explicit ngx.null values
-- @return A new table, with the auto fields containing
-- appropriate updated values.
Expand All @@ -1247,10 +1247,11 @@ function Schema:process_auto_fields(input, context, nulls)
for key, field in self:each_field(input) do

if field.auto then
if field.uuid and context == "insert" and output[key] == nil then
output[key] = utils.uuid()
elseif field.uuid and context == "upsert" and output[key] == nil then
output[key] = utils.uuid()
if field.uuid then
if (context == "insert" or context == "upsert") and
output[key] == nil or output[key] == "" then
output[key] = utils.uuid()
end

elseif (key == "created_at" and (context == "insert" or
context == "upsert")) or
Expand All @@ -1264,9 +1265,9 @@ function Schema:process_auto_fields(input, context, nulls)
output[key] = now_s
end

elseif field.type == "string" and output[key] == nil
and (context == "insert" or
context == "upsert") then
elseif field.type == "string" and
(context == "insert" or context == "upsert") and
(output[key] == nil or output[key] == "") then
output[key] = utils.random_string()
end
end
Expand Down

0 comments on commit e1432a6

Please sign in to comment.