activerecord-import 1.0.6 → 1.0.7

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
  SHA256:
3
- metadata.gz: bedb8323d872ae8ca6daebc691f82fedc918a3f2c01e7e354a8534854c06d80e
4
- data.tar.gz: 2df3dfaef596b93d208854557700167a4a8e0e9bb151ff224e4a6255e374086f
3
+ metadata.gz: fdf26ba513a4d3365d24cf9b2bf1fc814db9918d06ccdb91c5f32dec026fb7b7
4
+ data.tar.gz: 35ba31f95ef4b585cbe8ca4b0819d8ddc1ba50b12db6a70d3be2352669fd9289
5
5
  SHA512:
6
- metadata.gz: c97ba946d5453394831959f384868812ca59833d1c694feccc19d68967d38e3501305fca11fcd39dec32ddc8c88d26982ea2f087d590e34a19da88ff7def1c9c
7
- data.tar.gz: 0fcdc64e20ac0ee42584ab97f060e116f2542a353f0dc815a616adbe3810d6a343f8b021c0204adde9eb484aac5befc4e0b4699b1bbdfbddb2d486f8418d1839
6
+ metadata.gz: 3ef741e92b76c9705c63145b58351e82813eb71b003993a73cff5db2d0779d6dc14b48d4dc43a354ad8ba6a24a6acd2aa54bdf167775dee8e0404db4905c03f2
7
+ data.tar.gz: f8634e6ae1e9345ca9b1d68bca8fe4e67c04b1ae51101fdf97608a609d1fc349737d424ce23f809548d4e6a42931eb38d246d6be354acf49282fcaf20d827fa0
data/.gitignore CHANGED
@@ -24,6 +24,7 @@ pkg
24
24
  log/*.log
25
25
  test.db
26
26
  test/database.yml
27
+ benchmarks/log/
27
28
 
28
29
  .ruby-*
29
30
  .bundle/
@@ -1,3 +1,12 @@
1
+ ## Changes in 1.0.7
2
+
3
+ * Use @@max_allowed_packet session variable instead of querying SHOW VARIABLES. Thanks to @diclophis via \#706.
4
+ * 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.
5
+
6
+ ### Fixes
7
+
8
+ * Prevent mass-assignment errors in Rails strict mode. Thanks to @diclophis via \##709.
9
+
1
10
  ## Changes in 1.0.6
2
11
 
3
12
  ### Fixes
@@ -5,7 +14,7 @@
5
14
  * Handle after_initialize callbacks. Thanks to @AhMohsen46 via \#691 and
6
15
  \#692.
7
16
  * Fix regression introduced in 1.0.4. Explicity allow adapters to
8
- support on duplicate key update. Thanks to @dsobiera, @jkowens via \#698.
17
+ support on duplicate key update. Thanks to @dsobiera, @jkowens via \#696.
9
18
 
10
19
  ## Changes in 1.0.5
11
20
 
@@ -283,7 +292,7 @@
283
292
  Thanks to @jkowens via \#301.
284
293
  * Allow for custom timestamp columns. Thanks to @mojidabckuu, @jkowens
285
294
  via \#401.
286
-
295
+
287
296
  ### Fixes
288
297
 
289
298
  * Fix ActiveRecord 5 issue coercing boolean values when serializing
@@ -295,7 +304,7 @@
295
304
 
296
305
  * Fix issue where PostgreSQL cannot recognize columns if names
297
306
  include mixed case characters. Thanks to @hugobgranja via \#379.
298
- * Fix an issue for ActiveRecord 5 where serialized fields with
307
+ * Fix an issue for ActiveRecord 5 where serialized fields with
299
308
  default values were not being typecast. Thanks to @whistlerbrk,
300
309
  @jkowens via \#386.
301
310
  * Add option :force_single_insert for MySQL to make sure a single
@@ -250,20 +250,21 @@ Book.import books, recursive: true
250
250
 
251
251
  ### Options
252
252
 
253
- Key | Options | Default | Description
254
- ----------------------- | --------------------- | ------------------ | -----------
255
- :validate | `true`/`false` | `true` | Whether or not to run `ActiveRecord` validations (uniqueness skipped). This option will always be true when using `import!`.
256
- :validate_uniqueness | `true`/`false` | `false` | Whether or not to run uniqueness validations, has potential pitfalls, use with caution (requires `>= v0.27.0`).
257
- :validate_with_context | `Symbol` |`:create`/`:update` | Allows passing an ActiveModel validation context for each model. Default is `:create` for new records and `:update` for existing ones.
258
- :on_duplicate_key_ignore| `true`/`false` | `false` | Allows skipping records with duplicate keys. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-ignore) for more details.
259
- :ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
260
- :on_duplicate_key_update| :all, `Array`, `Hash` | N/A | Allows upsert logic to be used. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-update) for more details.
261
- :synchronize | `Array` | N/A | An array of ActiveRecord instances. This synchronizes existing instances in memory with updates from the import.
262
- :timestamps | `true`/`false` | `true` | Enables/disables timestamps on imported records.
263
- :recursive | `true`/`false` | `false` | Imports has_many/has_one associations (PostgreSQL only).
264
- :batch_size | `Integer` | total # of records | Max number of records to insert per import
265
- :raise_error | `true`/`false` | `false` | Raises an exception at the first invalid record. This means there will not be a result object returned. The `import!` method is a shortcut for this.
266
- :all_or_none | `true`/`false` | `false` | Will not import any records if there is a record with validation errors.
253
+ Key | Options | Default | Description
254
+ ------------------------- | --------------------- | ------------------ | -----------
255
+ :validate | `true`/`false` | `true` | Whether or not to run `ActiveRecord` validations (uniqueness skipped). This option will always be true when using `import!`.
256
+ :validate_uniqueness | `true`/`false` | `false` | Whether or not to run uniqueness validations, has potential pitfalls, use with caution (requires `>= v0.27.0`).
257
+ :validate_with_context | `Symbol` |`:create`/`:update` | Allows passing an ActiveModel validation context for each model. Default is `:create` for new records and `:update` for existing ones.
258
+ :track_validation_failures| `true`/`false` | `false` | 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]`
259
+ :on_duplicate_key_ignore | `true`/`false` | `false` | Allows skipping records with duplicate keys. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-ignore) for more details.
260
+ :ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
261
+ :on_duplicate_key_update | :all, `Array`, `Hash` | N/A | Allows upsert logic to be used. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-update) for more details.
262
+ :synchronize | `Array` | N/A | An array of ActiveRecord instances. This synchronizes existing instances in memory with updates from the import.
263
+ :timestamps | `true`/`false` | `true` | Enables/disables timestamps on imported records.
264
+ :recursive | `true`/`false` | `false` | Imports has_many/has_one associations (PostgreSQL only).
265
+ :batch_size | `Integer` | total # of records | Max number of records to insert per import
266
+ :raise_error | `true`/`false` | `false` | Raises an exception at the first invalid record. This means there will not be a result object returned. The `import!` method is a shortcut for this.
267
+ :all_or_none | `true`/`false` | `false` | Will not import any records if there is a record with validation errors.
267
268
 
268
269
  #### Duplicate Key Ignore
269
270
 
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["zach.dennis@gmail.com"]
7
7
  gem.summary = "Bulk insert extension for ActiveRecord"
8
8
  gem.description = "A library for bulk inserting data using ActiveRecord."
9
- gem.homepage = "http://github.com/zdennis/activerecord-import"
9
+ gem.homepage = "https://github.com/zdennis/activerecord-import"
10
10
  gem.license = "MIT"
11
11
 
12
12
  gem.files = `git ls-files`.split($\)
@@ -56,9 +56,9 @@ module ActiveRecord::Import::MysqlAdapter
56
56
  # in a single packet
57
57
  def max_allowed_packet # :nodoc:
58
58
  @max_allowed_packet ||= begin
59
- result = execute( "SHOW VARIABLES like 'max_allowed_packet'" )
59
+ result = execute( "SELECT @@max_allowed_packet" )
60
60
  # original Mysql gem responds to #fetch_row while Mysql2 responds to #first
61
- val = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1]
61
+ val = result.respond_to?(:fetch_row) ? result.fetch_row[0] : result.first[0]
62
62
  val.to_i
63
63
  end
64
64
  end
@@ -547,7 +547,7 @@ class ActiveRecord::Base
547
547
  alias import! bulk_import! unless ActiveRecord::Base.respond_to? :import!
548
548
 
549
549
  def import_helper( *args )
550
- options = { validate: true, timestamps: true }
550
+ options = { validate: true, timestamps: true, track_validation_failures: false }
551
551
  options.merge!( args.pop ) if args.last.is_a? Hash
552
552
  # making sure that current model's primary key is used
553
553
  options[:primary_key] = primary_key
@@ -703,13 +703,18 @@ class ActiveRecord::Base
703
703
  # keep track of the instance and the position it is currently at. if this fails
704
704
  # validation we'll use the index to remove it from the array_of_attributes
705
705
  arr.each_with_index do |hsh, i|
706
- model = new(hsh)
706
+ # utilize block initializer syntax to prevent failure when 'mass_assignment_sanitizer = :strict'
707
+ model = new do |m|
708
+ hsh.each_pair { |k, v| m[k] = v }
709
+ end
710
+
707
711
  next if validator.valid_model?(model)
708
712
  raise(ActiveRecord::RecordInvalid, model) if options[:raise_error]
713
+
709
714
  array_of_attributes[i] = nil
710
715
  failure = model.dup
711
716
  failure.errors.send(:initialize_dup, model.errors)
712
- failed_instances << failure
717
+ failed_instances << (options[:track_validation_failures] ? [i, failure] : failure )
713
718
  end
714
719
  array_of_attributes.compact!
715
720
  end
@@ -807,7 +812,7 @@ class ActiveRecord::Base
807
812
  result = connection.insert_many( [insert_sql, post_sql_statements].flatten,
808
813
  batch_values,
809
814
  options,
810
- "#{model_name} Create Many Without Validations Or Callbacks" )
815
+ "#{model_name} Create Many" )
811
816
  number_inserted += result.num_inserts
812
817
  ids += result.ids
813
818
  results += result.results
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Import
3
- VERSION = "1.0.6".freeze
3
+ VERSION = "1.0.7".freeze
4
4
  end
5
5
  end
@@ -252,6 +252,16 @@ describe "#import" do
252
252
  end
253
253
  end
254
254
 
255
+ it "should index the failed instances by their poistion in the set if `track_failures` is true" do
256
+ index_offset = valid_values.length
257
+ results = Topic.import columns, valid_values + invalid_values, validate: true, track_validation_failures: true
258
+ assert_equal invalid_values.size, results.failed_instances.size
259
+ invalid_values.each_with_index do |value_set, index|
260
+ assert_equal index + index_offset, results.failed_instances[index].first
261
+ assert_equal value_set.first, results.failed_instances[index].last.title
262
+ end
263
+ end
264
+
255
265
  it "should set ids in valid models if adapter supports setting primary key of imported objects" do
256
266
  if ActiveRecord::Base.supports_setting_primary_key_of_imported_objects?
257
267
  Topic.import (invalid_models + valid_models), validate: true
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.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Dennis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-01 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -67,7 +67,7 @@ files:
67
67
  - benchmarks/models/test_innodb.rb
68
68
  - benchmarks/models/test_memory.rb
69
69
  - benchmarks/models/test_myisam.rb
70
- - benchmarks/schema/mysql_schema.rb
70
+ - benchmarks/schema/mysql2_schema.rb
71
71
  - gemfiles/3.2.gemfile
72
72
  - gemfiles/4.0.gemfile
73
73
  - gemfiles/4.1.gemfile
@@ -167,7 +167,7 @@ files:
167
167
  - test/travis/database.yml
168
168
  - test/value_sets_bytes_parser_test.rb
169
169
  - test/value_sets_records_parser_test.rb
170
- homepage: http://github.com/zdennis/activerecord-import
170
+ homepage: https://github.com/zdennis/activerecord-import
171
171
  licenses:
172
172
  - MIT
173
173
  metadata: {}
@@ -186,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  - !ruby/object:Gem::Version
187
187
  version: '0'
188
188
  requirements: []
189
- rubygems_version: 3.0.6
189
+ rubygems_version: 3.0.8
190
190
  signing_key:
191
191
  specification_version: 4
192
192
  summary: Bulk insert extension for ActiveRecord