From 349200fb0a36cbf9142487ee98c3361c8e4a655f Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 30 Mar 2024 10:57:06 +0300 Subject: [PATCH] Rename update element consistency check --- app/models/concerns/consistency_validations.rb | 2 +- app/models/node.rb | 4 ++-- app/models/relation.rb | 4 ++-- app/models/way.rb | 4 ++-- test/models/node_test.rb | 9 +++++++++ test/models/relation_test.rb | 9 +++++++++ test/models/way_test.rb | 9 +++++++++ 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/consistency_validations.rb b/app/models/concerns/consistency_validations.rb index 8b97e2b70..65226d87a 100644 --- a/app/models/concerns/consistency_validations.rb +++ b/app/models/concerns/consistency_validations.rb @@ -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 diff --git a/app/models/node.rb b/app/models/node.rb index 385bc732a..825336d16 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -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 diff --git a/app/models/relation.rb b/app/models/relation.rb index c87f70dad..f09647320 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -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 diff --git a/app/models/way.rb b/app/models/way.rb index 2a989f9f3..203d3b703 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -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? diff --git a/test/models/node_test.rb b/test/models/node_test.rb index 664bfb260..94cb5ec81 100644 --- a/test/models/node_test.rb +++ b/test/models/node_test.rb @@ -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 diff --git a/test/models/relation_test.rb b/test/models/relation_test.rb index 5d7d0d467..405dd353d 100644 --- a/test/models/relation_test.rb +++ b/test/models/relation_test.rb @@ -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 diff --git a/test/models/way_test.rb b/test/models/way_test.rb index 8d662b657..36debfac0 100644 --- a/test/models/way_test.rb +++ b/test/models/way_test.rb @@ -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 -- 2.39.5