activerecord-import 1.0.8 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.markdown +12 -0
- data/lib/activerecord-import/import.rb +13 -1
- data/lib/activerecord-import/version.rb +1 -1
- data/test/import_test.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c61743fafaad0de04ccf2c6bff4439fe66aed03e951e5c42e4f7926cf8dac39
|
4
|
+
data.tar.gz: c58d8992957e546b73bb7f7b90eaf2a741f007d2109a16db0e3b3d71b376e1da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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-
|
11
|
+
date: 2021-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|