Rename update element consistency check

This commit is contained in:
Anton Khorev 2024-03-30 10:57:06 +03:00
parent 09f6b83486
commit 349200fb0a
7 changed files with 34 additions and 7 deletions

View file

@ -7,7 +7,7 @@ module ConsistencyValidations
# code in 6 places for all the updates and deletes. Some of these tests are
# needed for creates, but are currently not run :-(
# This will throw an exception if there is an inconsistency
def check_consistency(old, new, user)
def check_update_element_consistency(old, new, user)
if new.id != old.id || new.id.nil? || old.id.nil?
raise OSM::APIPreconditionFailedError, "New and old IDs don't match on #{new.class}. #{new.id} != #{old.id}."
elsif new.version != old.version

View file

@ -145,7 +145,7 @@ class Node < ApplicationRecord
# shouldn't be possible to get race conditions.
Node.transaction do
lock!
check_consistency(self, new_node, user)
check_update_element_consistency(self, new_node, user)
ways = Way.joins(:way_nodes).where(:visible => true, :current_way_nodes => { :node_id => id }).order(:id)
raise OSM::APIPreconditionFailedError, "Node #{id} is still used by ways #{ways.collect(&:id).join(',')}." unless ways.empty?
@ -166,7 +166,7 @@ class Node < ApplicationRecord
def update_from(new_node, user)
Node.transaction do
lock!
check_consistency(self, new_node, user)
check_update_element_consistency(self, new_node, user)
# update changeset first
self.changeset_id = new_node.changeset_id

View file

@ -166,7 +166,7 @@ class Relation < ApplicationRecord
# shouldn't be possible to get race conditions.
Relation.transaction do
lock!
check_consistency(self, new_relation, user)
check_update_element_consistency(self, new_relation, user)
# This will check to see if this relation is used by another relation
rel = RelationMember.joins(:relation).find_by("visible = ? AND member_type = 'Relation' and member_id = ? ", true, id)
raise OSM::APIPreconditionFailedError, "The relation #{new_relation.id} is used in relation #{rel.relation.id}." unless rel.nil?
@ -182,7 +182,7 @@ class Relation < ApplicationRecord
def update_from(new_relation, user)
Relation.transaction do
lock!
check_consistency(self, new_relation, user)
check_update_element_consistency(self, new_relation, user)
raise OSM::APIPreconditionFailedError, "Cannot update relation #{id}: data or member data is invalid." unless new_relation.preconditions_ok?(members)
self.changeset_id = new_relation.changeset_id

View file

@ -142,7 +142,7 @@ class Way < ApplicationRecord
def update_from(new_way, user)
Way.transaction do
lock!
check_consistency(self, new_way, user)
check_update_element_consistency(self, new_way, user)
raise OSM::APIPreconditionFailedError, "Cannot update way #{id}: data is invalid." unless new_way.preconditions_ok?(nds)
self.changeset_id = new_way.changeset_id
@ -193,7 +193,7 @@ class Way < ApplicationRecord
# shouldn't be possible to get race conditions.
Way.transaction do
lock!
check_consistency(self, new_way, user)
check_update_element_consistency(self, new_way, user)
rels = Relation.joins(:relation_members).where(:visible => true, :current_relation_members => { :member_type => "Way", :member_id => id }).order(:id)
raise OSM::APIPreconditionFailedError, "Way #{id} is still used by relations #{rels.collect(&:id).join(',')}." unless rels.empty?

View file

@ -435,4 +435,13 @@ class NodeTest < ActiveSupport::TestCase
node.update_from(new_node, user)
end
end
test "raises id precondition exception when deleting" do
user = create(:user)
node = Node.new(:id => 23, :visible => true)
new_node = Node.new(:id => 42, :visible => false)
assert_raises OSM::APIPreconditionFailedError do
node.delete_with_history!(new_node, user)
end
end
end

View file

@ -323,4 +323,13 @@ class RelationTest < ActiveSupport::TestCase
relation.update_from(new_relation, user)
end
end
test "raises id precondition exception when deleting" do
user = create(:user)
relation = Relation.new(:id => 23, :visible => true)
new_relation = Relation.new(:id => 42, :visible => false)
assert_raises OSM::APIPreconditionFailedError do
relation.delete_with_history!(new_relation, user)
end
end
end

View file

@ -290,4 +290,13 @@ class WayTest < ActiveSupport::TestCase
way.update_from(new_way, user)
end
end
test "raises id precondition exception when deleting" do
user = create(:user)
way = Way.new(:id => 23, :visible => true)
new_way = Way.new(:id => 42, :visible => false)
assert_raises OSM::APIPreconditionFailedError do
way.delete_with_history!(new_way, user)
end
end
end