active_record_bulk_insert 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5617c164c167fc88a439beabb1e33362f51a7e6
4
- data.tar.gz: 35543d200074044baa24c5e131f88ced54cfcf26
3
+ metadata.gz: 480b59f2af018604382897c14b7429039abb6e92
4
+ data.tar.gz: c9ac794a5474b28fd6475536aa9dae2f1ee67af4
5
5
  SHA512:
6
- metadata.gz: d175599e02ca65ba95dd3dfbbad4138d6d75d5ec8e438033bec8e1ca586dd27c291fee65f5213854c1cc74974eeb924d851f1bdbba02848c19a2a4c9d12f7db6
7
- data.tar.gz: 7f6a9e92d3b090747f3a83ad65ad63fc54be37814dde88b0aba3fd15ab5b653be42ce3b7be3e05a10f28b071a72c131ca822f6375076de998a8efb74e434b3c9
6
+ metadata.gz: 326b9b67bfa9c11aff90d89a7056335a94b95c5d707a6b0780ac37aa4a0ae425d81e90f992445ba16fb8b48fdd8fe85070377299a1c1f098bcf5d10ebdefc167
7
+ data.tar.gz: c880802bcb693a22f981818fc6e0f7fffcca71be2530e6e68f296373b64e3c46be866c67e3d9315d639923123ec373780ad2ad14a6bc7a771c92f3893da0fb95
data/README.md CHANGED
@@ -57,6 +57,7 @@ users = [{:id => 200, :username => "foo", :firstname => "Foo", :lastname => "Bar
57
57
  User.bulk_insert(users, :use_provided_primary_key => true)
58
58
  ```
59
59
  *note this is only available from ActiveRecord 4.0 as id was protected from mass-assignment in ActiveRecord < 4.0*
60
+ *The return value is a list of invalid records*
60
61
 
61
62
  ### Disable default timestamps
62
63
 
@@ -3,10 +3,12 @@ ActiveRecord::Base.class_eval do
3
3
  batch_size = options.fetch(:batch_size, 1000)
4
4
  delay = options.fetch(:delay, nil)
5
5
 
6
- attrs.each_slice(batch_size).map do |sliced_attrs|
7
- bulk_insert(sliced_attrs, options)
6
+ invalid = []
7
+ attrs.each_slice(batch_size) do |sliced_attrs|
8
+ invalid += bulk_insert(sliced_attrs, options)
8
9
  sleep(delay) if delay
9
- end.flatten.compact
10
+ end
11
+ invalid
10
12
  end
11
13
 
12
14
  def self.bulk_insert(attrs, options = {})
@@ -15,12 +17,13 @@ ActiveRecord::Base.class_eval do
15
17
  use_provided_primary_key = options.fetch(:use_provided_primary_key, false)
16
18
  attributes = _resolve_record(attrs.first, options).keys.join(", ")
17
19
 
20
+ invalid = []
18
21
  if options.fetch(:validate, false)
19
22
  attrs, invalid = attrs.partition { |record| _validate(record) }
20
23
  end
21
24
 
22
25
  values_sql = attrs.map do |record|
23
- "(#{_resolve_record(record, options).values.map { |r| sanitize(r) }.join(', ')})"
26
+ "(#{_resolve_record(record, options).map {|k, v| connection.quote(v, try(:column_for_attribute, k)) }.join(', ')})"
24
27
  end.join(",")
25
28
 
26
29
  sql = <<-SQL
@@ -78,6 +78,13 @@ describe SampleRecord do
78
78
  SampleRecord.send(:validates, :name, :presence => true)
79
79
  expect {SampleRecord.bulk_insert([:age => 30], :validate => true)}.to_not change{SampleRecord.count}
80
80
  end
81
+
82
+ it "returns the invalid records" do
83
+ SampleRecord.send(:validates, :name, :presence => true)
84
+ records = [{:age => 30, :name => ""}, {:age => 29, :name => "Foo"}]
85
+ invalid_records = SampleRecord.bulk_insert(records, :validate => true, :disable_timestamps => true)
86
+ invalid_records.should == [{:age => 30, :name => ""}]
87
+ end
81
88
  end
82
89
 
83
90
  context "timestamps" do
@@ -119,5 +126,15 @@ describe SampleRecord do
119
126
  SampleRecord.should_receive(:sleep).with(2)
120
127
  SampleRecord.bulk_insert_in_batches(records, :delay => 2)
121
128
  end
129
+
130
+ context "validations" do
131
+ it "returns the invalid records" do
132
+ SampleRecord.send(:validates, :name, :presence => true)
133
+ records = [{:age => 30, :name => ""}, {:age => 29, :name => "Foo"}]
134
+
135
+ invalid_records = SampleRecord.bulk_insert_in_batches(records, :batch_size => 1, :validate => true, :disable_timestamps => true)
136
+ invalid_records.should == [{:age => 30, :name => ""}]
137
+ end
138
+ end
122
139
  end
123
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_bulk_insert
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abejide Ayodele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2016-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -73,12 +73,12 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - README.md
76
77
  - lib/active_record_bulk_insert.rb
77
78
  - spec/sample_record_spec.rb
78
79
  - spec/spec_helper.rb
79
80
  - spec/support/migrations/1_sample_record_migration.rb
80
81
  - spec/support/sample_record.rb
81
- - README.md
82
82
  homepage: https://github.com/bjhaid/active_record_bulk_insert
83
83
  licenses:
84
84
  - MIT
@@ -89,17 +89,17 @@ require_paths:
89
89
  - lib
90
90
  required_ruby_version: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - '>='
92
+ - - ">="
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - '>='
97
+ - - ">="
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
101
  rubyforge_project:
102
- rubygems_version: 2.0.14
102
+ rubygems_version: 2.4.5.1
103
103
  signing_key:
104
104
  specification_version: 4
105
105
  summary: bulk insert records into the DB