remove deleteway logic from amf_controller (yay!) TomH: check with RichardF this is sane before deploying. worksforme.

This commit is contained in:
Steve Coast 2008-02-23 16:05:45 +00:00
parent 72e30a3955
commit d1f2b4ece9
2 changed files with 17 additions and 62 deletions

View file

@ -500,52 +500,18 @@ class AmfController < ApplicationController
# also removes ways/nodes from any relations they're in # also removes ways/nodes from any relations they're in
# out: [0] 0 (success), [1] way id (unchanged) # out: [0] 0 (success), [1] way id (unchanged)
def deleteway(args) #:doc: def deleteway(args) #:doc:
usertoken,way_id=args
usertoken,way=args RAILS_DEFAULT_LOGGER.info(" Message: deleteway, id=#{way_id}")
RAILS_DEFAULT_LOGGER.info(" Message: deleteway, id=#{way}")
uid=getuserid(usertoken) uid=getuserid(usertoken)
if !uid then return -1,"You are not logged in, so the way could not be deleted." end if !uid then return -1,"You are not logged in, so the way could not be deleted." end
way=way.to_i user = User.find(uid)
db_uqn='unin'+(rand*100).to_i.to_s+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
db_now='@now'+(rand*100).to_i.to_s+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
# - delete any otherwise unused nodes way = Way.find(way_id)
createuniquenodes(way,db_uqn,[]) way.delete_with_relations_and_nodes_and_history(user)
# unless (preserve.empty?) then return [0,way_id]
# ActiveRecord::Base.connection.execute("DELETE FROM #{db_uqn} WHERE node_id IN ("+preserve.join(',')+")")
# end
sql=<<-EOF
INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
FROM current_nodes AS cn,#{db_uqn}
WHERE cn.id=node_id
EOF
ActiveRecord::Base.connection.insert(sql)
sql=<<-EOF
UPDATE current_nodes AS cn, #{db_uqn}
SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
WHERE cn.id=node_id
EOF
ActiveRecord::Base.connection.update(sql)
deleteuniquenoderelations(db_uqn,uid,db_now)
ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE #{db_uqn}")
# - delete way
ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},0)")
ActiveRecord::Base.connection.update("UPDATE current_ways SET user_id=#{uid},timestamp=#{db_now},visible=0 WHERE id=#{way}")
ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}")
ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
deleteitemrelations(way,'way',uid,db_now)
[0,way]
end end
def readwayquery(id,insistonvisible) #:doc: def readwayquery(id,insistonvisible) #:doc:

View file

@ -227,38 +227,27 @@ class Way < ActiveRecord::Base
end end
# delete a way and it's nodes that aren't part of other ways, with history # delete a way and it's nodes that aren't part of other ways, with history
# WARNING, INCOMPLETE - Read the code
def delete_with_relations_and_nodes_and_history(user) def delete_with_relations_and_nodes_and_history(user)
return false # until complete, just return false
node_ids = self.nodes.collect {|node| node.id } node_ids = self.nodes.collect {|node| node.id }
node_ids_not_to_delete = [] node_ids_not_to_delete = []
way_nodes = WayNode.find(:all, :conditions => "node_id in [#{node_ids.join(',')}] and id not #{self.id}") way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}")
node_ids_not_to_delete = way_nodes.collect {|way_node| way_node.node_id} node_ids_not_to_delete = way_nodes.collect {|way_node| way_node.node_id}
nodes_to_delete = node_ids - node_ids_not_to_delete node_ids_to_delete = node_ids - node_ids_not_to_delete
# update the visibility etc on the current nodes # delete the nodes not used by other ways
update_time = Time.now() node_ids_to_delete.each do |node_id|
n = Node.find(node_id)
Node.update(node_ids_to_delete, {:user_id => user.id, :timestamp => update_time, :visibility => false}) n.user_id = user.id
n.visible = false
# create old nodes n.save_with_history!
old_nodes_to_create = []
OldNode.find(node_ids_to_delete).each do |old_node|
old_nodes_to_create << {:id => old_node.id, :timestamp => update_time, :latitude => old_node.latitude, :longitude => old_node.longitude, :visible => false}
end end
OldNode.create(old_nodes_to_create)
# FIXME - the old nodes need to have their tile updated and I have no idea how that works
# they also need their tags copied over, and that depends on normalising the tags out to their own table that nicks working on
# finally, delete the way
self.delete_with_relations_and_history self.user_id = user.id
self.delete_with_relations_and_history(user)
end end
end end