Lock import resources on updates to prevent write collisions.

This commit is contained in:
Thorsten Eckel 2017-06-27 16:48:44 +02:00
parent 3c50f366f7
commit d214106f98

View file

@ -91,31 +91,35 @@ module Import
@resource = lookup_existing(resource, *args) @resource = lookup_existing(resource, *args)
return false if !@resource return false if !@resource
# delete since we have an update and # lock the current resource for write access
# the record is already created @resource.with_lock do
resource.delete(:created_by_id)
# store the current state of the associations # delete since we have an update and
# from the resource hash because if we assign # the record is already created
# them to the instance some (e.g. has_many) resource.delete(:created_by_id)
# will get stored even in the dry run :/
store_associations(:after, resource)
associations = tracked_associations # store the current state of the associations
@resource.assign_attributes(resource.except(*associations)) # from the resource hash because if we assign
# them to the instance some (e.g. has_many)
# will get stored even in the dry run :/
store_associations(:after, resource)
# the return value here is kind of misleading associations = tracked_associations
# and should not be trusted to indicate if a @resource.assign_attributes(resource.except(*associations))
# resource was actually updated.
# Use .action instead
return true if !attributes_changed?
@action = :updated # the return value here is kind of misleading
# and should not be trusted to indicate if a
# resource was actually updated.
# Use .action instead
return true if !attributes_changed?
return true if @dry_run @action = :updated
@resource.assign_attributes(resource.slice(*associations))
@resource.save! return true if @dry_run
true @resource.assign_attributes(resource.slice(*associations))
@resource.save!
true
end
end end
def lookup_existing(resource, *_args) def lookup_existing(resource, *_args)