]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/2131'
authorTom Hughes <tom@compton.nu>
Sat, 23 Mar 2019 12:08:41 +0000 (12:08 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 23 Mar 2019 12:08:41 +0000 (12:08 +0000)
Gemfile
Gemfile.lock
app/models/trace.rb
lib/gpx.rb
test/models/trace_test.rb

diff --git a/Gemfile b/Gemfile
index 372b7d021740b2cecc5949a33d7122b42d0c532f..83f298101fcd38c2459a8ce25087333864c376d7 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -46,6 +46,7 @@ gem "image_optim_rails"
 # Load rails plugins
 gem "actionpack-page_caching"
 gem "active_record_union"
+gem "activerecord-import"
 gem "cancancan"
 gem "composite_primary_keys", "~> 11.1.0"
 gem "config"
index 2b5cc1dd5a6538be1095af7342c0d86e6436dfdb..57feba1fd0c4623ab2f34d99ed4f3f4c5d2ea0e3 100644 (file)
@@ -40,6 +40,8 @@ GEM
       activemodel (= 5.2.2.1)
       activesupport (= 5.2.2.1)
       arel (>= 9.0)
+    activerecord-import (0.28.1)
+      activerecord (>= 3.2)
     activestorage (5.2.2.1)
       actionpack (= 5.2.2.1)
       activerecord (= 5.2.2.1)
@@ -423,6 +425,7 @@ DEPENDENCIES
   aasm
   actionpack-page_caching
   active_record_union
+  activerecord-import
   annotate
   autoprefixer-rails (~> 8.6.3)
   better_errors
index 892d41a2a217b5872cacc8a5724f63a36da5690c..0e876332874487acbafb3c5b17013e0cbb2b579b 100644 (file)
@@ -289,21 +289,34 @@ class Trace < ActiveRecord::Base
     # If there are any existing points for this trace then delete them
     Tracepoint.where(:gpx_id => id).delete_all
 
-    gpx.points do |point|
-      if first
-        f_lat = point.latitude
-        f_lon = point.longitude
-        first = false
+    gpx.points.each_slice(1_000) do |points|
+      # Gather the trace points together for a bulk import
+      tracepoints = []
+
+      points.each do |point|
+        if first
+          f_lat = point.latitude
+          f_lon = point.longitude
+          first = false
+        end
+
+        tp = Tracepoint.new
+        tp.lat = point.latitude
+        tp.lon = point.longitude
+        tp.altitude = point.altitude
+        tp.timestamp = point.timestamp
+        tp.gpx_id = id
+        tp.trackid = point.segment
+        tracepoints << tp
       end
 
-      tp = Tracepoint.new
-      tp.lat = point.latitude
-      tp.lon = point.longitude
-      tp.altitude = point.altitude
-      tp.timestamp = point.timestamp
-      tp.gpx_id = id
-      tp.trackid = point.segment
-      tp.save!
+      # Run the before_save and before_create callbacks, and then import them in bulk with activerecord-import
+      tracepoints.each do |tp|
+        tp.run_callbacks(:save) { false }
+        tp.run_callbacks(:create) { false }
+      end
+
+      Tracepoint.import!(tracepoints)
     end
 
     if gpx.actual_points.positive?
index 5664e0099b131b255ab44455e2c08e308ea10e69..721e5608b5cd9e22d59f2b13ab0a9965b20d51d7 100644 (file)
@@ -13,6 +13,8 @@ module GPX
     end
 
     def points
+      return enum_for(:points) unless block_given?
+
       @possible_points = 0
       @actual_points = 0
       @tracksegs = 0
index f03488a6115324e631624edf397f461d1960f8ed..81120f0e776e0c1103560f153f50912eed50d096 100644 (file)
@@ -206,6 +206,10 @@ class TraceTest < ActiveSupport::TestCase
 
       trace.reload
       assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
+
+      # Check that the tile has been set prior to the bulk import
+      # i.e. that the callbacks have been run correctly
+      assert_equal 3221331576, Tracepoint.where(:gpx_id => trace.id).first.tile
     end
   end