Permalink
Browse files

Move to sql for bulk inserts during the project transfers.

Closes #2. Thanks to pnorman for the sql queries.
  • Loading branch information...
1 parent 257c197 commit 670c9b1a32e9290b8bd8f29f2052acc3ce9e0e04 @gravitystorm committed Nov 21, 2012
Showing with 33 additions and 36 deletions.
  1. +33 −36 app/models/project.rb
View
@@ -25,43 +25,40 @@ def tagged_completion
entities > 0 ? ((changes.to_f / entities) * 100).to_i : 0
end
- # This might take a while...
+ # This uses raw sql queries, which isn't very rails-y, but at least scales exteremly well.
+ # An alternative would be to investigate gems like https://github.com/zdennis/activerecord-import
def transfer
- Node.all.each do |node|
- pn = self.nodes.new
- pn.update_from(node)
- pn.save!
- end
-
- Relation.all.each do |relation|
- pr = self.relations.new
- pr.update_from(relation)
- pr.save!
- end
-
- RelationMember.all.each do |relation_member|
- prm = self.relation_members.new
- prm.update_from(relation_member)
- prm.save!
- end
-
- WayNode.all.each do |way_node|
- pwn = self.way_nodes.new
- pwn.update_from(way_node)
- pwn.save!
- end
-
- Way.all.each do |way|
- pw = self.ways.new
- pw.update_from(way)
- pw.save!
- end
-
- User.all.each do |user|
- pu = self.users.new
- pu.update_from(user)
- pu.save!
- end
+ self.connection.execute(
+ "INSERT INTO project_nodes
+ (project_id,osm_id,version,user_id,tstamp,changeset_id,tags,geom)
+ SELECT #{self.id},id,version,user_id,tstamp,changeset_id,tags,geom FROM nodes;")
+
+ self.connection.execute(
+ "INSERT INTO project_relations
+ (project_id,osm_id,version,user_id,tstamp,changeset_id,tags)
+ SELECT #{self.id},id,version,user_id,tstamp,changeset_id,tags FROM relations;")
+
+ self.connection.execute(
+ "INSERT INTO project_relation_members
+ (project_id,relation_id,member_id,member_type,member_role,sequence_id)
+ SELECT #{self.id},relation_id,member_id,member_type,member_role,sequence_id FROM relation_members;")
+
+ self.connection.execute(
+ "INSERT INTO project_way_nodes
+ (project_id, way_id, node_id, sequence_id)
+ SELECT #{self.id},way_id, node_id, sequence_id FROM way_nodes;")
+
+ self.connection.execute(
+ "INSERT INTO project_ways
+ (project_id, osm_id, version, user_id, tstamp,changeset_id, tags)
+ SELECT #{self.id}, id, version, user_id, tstamp, changeset_id, tags FROM ways;")
+
+ self.connection.execute(
+ "INSERT INTO project_users
+ (project_id, osm_id, name)
+ SELECT #{self.id}, id, name FROM users;")
+
+ self.reload
end
def truncate_staging_tables

0 comments on commit 670c9b1

Please sign in to comment.