diff --git a/app/models/relation.rb b/app/models/relation.rb index 3d3c317aa..cb9621822 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -246,8 +246,10 @@ class Relation < ActiveRecord::Base # use reflection to look up the appropriate class model = Kernel.const_get(m[0].capitalize) - # get the element with that ID - element = model.find_by(:id => m[1]) + # get the element with that ID. and, if found, lock the element to + # ensure it can't be deleted until after the current transaction + # commits. + element = model.lock("for share").find_by(:id => m[1]) # and check that it is OK to use. unless element && element.visible? && element.preconditions_ok? diff --git a/app/models/way.rb b/app/models/way.rb index 34e568e4a..6d49735f1 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -199,7 +199,9 @@ class Way < ActiveRecord::Base new_nds = (nds - old_nodes).sort.uniq unless new_nds.empty? - db_nds = Node.where(:id => new_nds, :visible => true) + # NOTE: nodes are locked here to ensure they can't be deleted before + # the current transaction commits. + db_nds = Node.where(:id => new_nds, :visible => true).lock("for share") if db_nds.length < new_nds.length missing = new_nds - db_nds.collect(&:id)