FIX: Wrap custom fields database statements in a transaction.

Kind of strange that we don't do it because a database statement
may fail and leave us in a weird state.
This commit is contained in:
Guo Xiang Tan 2018-10-18 12:23:04 +08:00
parent 287e780f22
commit 22408f93c9
1 changed files with 39 additions and 38 deletions

View File

@ -194,9 +194,9 @@ module HasCustomFields
def save_custom_fields(force = false)
if force || !custom_fields_clean?
dup = @custom_fields.dup
array_fields = {}
ActiveRecord::Base.transaction do
_custom_fields.reload.each do |f|
if dup[f.name].is_a?(Array)
# we need to collect Arrays fully before we can compare them
@ -205,7 +205,7 @@ module HasCustomFields
else
array_fields[f.name] << f
end
elsif dup[f.name].is_a? Hash
elsif dup[f.name].is_a?(Hash)
if dup[f.name].to_json != f.value
f.destroy!
else
@ -228,7 +228,7 @@ module HasCustomFields
if fields.length == dup[field_name].length && fields.map(&:value) == dup[field_name]
dup.delete(field_name)
else
fields.each(&:destroy)
fields.each(&:destroy!)
end
end
@ -244,6 +244,7 @@ module HasCustomFields
)
end
end
end
refresh_custom_fields_from_db
end