activerecord-import 1.0.8 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b32b91e70dc3ec25461a37092b774709300798912b930aeb9e7ae57dba41230d
4
- data.tar.gz: 5b64d6bc14cae05785300a72cd830e552cd5b21d0dbb304e4e527f69de9865c5
3
+ metadata.gz: 4c61743fafaad0de04ccf2c6bff4439fe66aed03e951e5c42e4f7926cf8dac39
4
+ data.tar.gz: c58d8992957e546b73bb7f7b90eaf2a741f007d2109a16db0e3b3d71b376e1da
5
5
  SHA512:
6
- metadata.gz: 04a16537b5cd4ef535e7ed1c49397b1313d2717f5b60ed43cdee42f220d3a0094d0b4cce6241470c892a184b09d74b28ece67d28506508964f962c9282aa763c
7
- data.tar.gz: d84f9ffea3a04a2ca3b2bb3dfc05307a90876d5c7e9f8e155605dbc7a6bbcec8cd3cfefc7aac82a7e14de0ae5334cdeba3ac6467c00206ccd15e016aebe8854e
6
+ metadata.gz: 98638b63235eae1c16f27d3be932b8ce4e8f236b24350766792a4fc563d199cb1d048d25efa5d22abf3a21017fa07b9474c3168a4be6c1ba5aaa6e0843d584a3
7
+ data.tar.gz: 5c62bed2684f1b6d60d7e26b8ad13c6b7173a64835eeca9474082122425ab1f340f9063868224eda2539878210887701022e1ff6e6a37593872682bf90768d3a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## Changes in 1.1.0
2
+
3
+ ### New Features
4
+
5
+ * Add batch progress reporting. Thanks to @gee-forr via \##729.
6
+
1
7
  ## Changes in 1.0.8
2
8
 
3
9
  ### Fixes
@@ -6,6 +12,8 @@
6
12
 
7
13
  ## Changes in 1.0.7
8
14
 
15
+ ### New Features
16
+
9
17
  * Use @@max_allowed_packet session variable instead of querying SHOW VARIABLES. Thanks to @diclophis via \#706.
10
18
  * Add option :track_validation_failures. When this is set to true, failed_instances will be an array of arrays, with each inner array having the form [:index_in_dataset, :object_with_errors]. Thanks to @rorymckinley via \#684.
11
19
 
data/README.markdown CHANGED
@@ -231,6 +231,18 @@ columns = [ :title ]
231
231
  Book.import columns, books, batch_size: 2
232
232
  ```
233
233
 
234
+ If your import is particularly large or slow (possibly due to [callbacks](#callbacks)) whilst batch importing, you might want a way to report back on progress. This is supported by passing a callable as the `batch_progress` option. e.g:
235
+
236
+ ```ruby
237
+ my_proc = ->(rows_size, num_batches, current_batch_number, batch_duration_in_secs) {
238
+ # Using the arguments provided to the callable, you can
239
+ # send an email, post to a websocket,
240
+ # update slack, alert if import is taking too long, etc.
241
+ }
242
+
243
+ Book.import columns, books, batch_size: 2, batch_progress: my_proc
244
+ ```
245
+
234
246
  #### Recursive
235
247
 
236
248
  NOTE: This only works with PostgreSQL and ActiveRecord objects. This won't work with
@@ -805,17 +805,29 @@ class ActiveRecord::Base
805
805
  if supports_import?
806
806
  # generate the sql
807
807
  post_sql_statements = connection.post_sql_statements( quoted_table_name, options )
808
+ import_size = values_sql.size
809
+
810
+ batch_size = options[:batch_size] || import_size
811
+ run_proc = options[:batch_size].to_i.positive? && options[:batch_progress].respond_to?( :call )
812
+ progress_proc = options[:batch_progress]
813
+ current_batch = 0
814
+ batches = (import_size / batch_size.to_f).ceil
808
815
 
809
- batch_size = options[:batch_size] || values_sql.size
810
816
  values_sql.each_slice(batch_size) do |batch_values|
817
+ batch_started_at = Time.now.to_i
818
+
811
819
  # perform the inserts
812
820
  result = connection.insert_many( [insert_sql, post_sql_statements].flatten,
813
821
  batch_values,
814
822
  options,
815
823
  "#{model_name} Create Many" )
824
+
816
825
  number_inserted += result.num_inserts
817
826
  ids += result.ids
818
827
  results += result.results
828
+ current_batch += 1
829
+
830
+ progress_proc.call(import_size, batches, current_batch, Time.now.to_i - batch_started_at) if run_proc
819
831
  end
820
832
  else
821
833
  transaction(requires_new: true) do
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Import
3
- VERSION = "1.0.8".freeze
3
+ VERSION = "1.1.0".freeze
4
4
  end
5
5
  end
data/test/import_test.rb CHANGED
@@ -405,6 +405,15 @@ describe "#import" do
405
405
  assert_equal 3, result.num_inserts if Topic.supports_import?
406
406
  end
407
407
  end
408
+
409
+ it "should accept and call an optional callable to run after each batch" do
410
+ lambda_called = 0
411
+
412
+ my_proc = ->(_row_count, _batches, _batch, _duration) { lambda_called += 1 }
413
+ Topic.import Build(10, :topics), batch_size: 4, batch_progress: my_proc
414
+
415
+ assert_equal 3, lambda_called
416
+ end
408
417
  end
409
418
 
410
419
  context "with :synchronize option" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Dennis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-20 00:00:00.000000000 Z
11
+ date: 2021-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord