activerecord-import 1.0.5 → 1.0.6

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: 123bf726b813b21067d888221d627a03796f262de7e1b70c318def898b26d3c0
4
- data.tar.gz: d6e6a3031944ac841587a8c29c701bc1a15667a888771333ad7198b085b2e8b4
3
+ metadata.gz: bedb8323d872ae8ca6daebc691f82fedc918a3f2c01e7e354a8534854c06d80e
4
+ data.tar.gz: 2df3dfaef596b93d208854557700167a4a8e0e9bb151ff224e4a6255e374086f
5
5
  SHA512:
6
- metadata.gz: 4409c4e0f048ea6272329d34de680d6314168784fdd4f244712ea37725254c66e4f091d302c0598e8232c31fbb9b94152197ee5dd660aa36d5df937839bf197d
7
- data.tar.gz: 5c0e49836fdf38e4f95c863aa5c0841592d169063b5a455a90d2a16c8a65736b8837e05f371227b9c8e4a23c406e990421e1e3f7a1cb37e1e4d3f9b91b36521e
6
+ metadata.gz: c97ba946d5453394831959f384868812ca59833d1c694feccc19d68967d38e3501305fca11fcd39dec32ddc8c88d26982ea2f087d590e34a19da88ff7def1c9c
7
+ data.tar.gz: 0fcdc64e20ac0ee42584ab97f060e116f2542a353f0dc815a616adbe3810d6a343f8b021c0204adde9eb484aac5befc4e0b4699b1bbdfbddb2d486f8418d1839
@@ -1,3 +1,12 @@
1
+ ## Changes in 1.0.6
2
+
3
+ ### Fixes
4
+
5
+ * Handle after_initialize callbacks. Thanks to @AhMohsen46 via \#691 and
6
+ \#692.
7
+ * Fix regression introduced in 1.0.4. Explicity allow adapters to
8
+ support on duplicate key update. Thanks to @dsobiera, @jkowens via \#698.
9
+
1
10
  ## Changes in 1.0.5
2
11
 
3
12
  ### Fixes
@@ -529,7 +529,7 @@ require 'activerecord-import'
529
529
  ### Load Path Setup
530
530
  To understand how rubygems loads code you can reference the following:
531
531
 
532
- http://guides.rubygems.org/patterns/#loading_code
532
+ http://guides.rubygems.org/patterns/#loading-code
533
533
 
534
534
  And an example of how active_record dynamically load adapters:
535
535
 
@@ -66,7 +66,7 @@ module ActiveRecord::Import::AbstractAdapter
66
66
  end
67
67
 
68
68
  def supports_on_duplicate_key_update?
69
- true
69
+ false
70
70
  end
71
71
  end
72
72
  end
@@ -1,5 +1,6 @@
1
1
  module ActiveRecord::Import::MysqlAdapter
2
2
  include ActiveRecord::Import::ImportSupport
3
+ include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
3
4
 
4
5
  NO_MAX_PACKET = 0
5
6
  QUERY_OVERHEAD = 8 # This was shown to be true for MySQL, but it's not clear where the overhead is from.
@@ -1,5 +1,6 @@
1
1
  module ActiveRecord::Import::PostgreSQLAdapter
2
2
  include ActiveRecord::Import::ImportSupport
3
+ include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
3
4
 
4
5
  MIN_VERSION_FOR_UPSERT = 90_500
5
6
 
@@ -1,5 +1,6 @@
1
1
  module ActiveRecord::Import::SQLite3Adapter
2
2
  include ActiveRecord::Import::ImportSupport
3
+ include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
3
4
 
4
5
  MIN_VERSION_FOR_IMPORT = "3.7.11".freeze
5
6
  MIN_VERSION_FOR_UPSERT = "3.24.0".freeze
@@ -11,6 +11,12 @@ module ActiveRecord::Import #:nodoc:
11
11
  end
12
12
  end
13
13
 
14
+ module OnDuplicateKeyUpdateSupport #:nodoc:
15
+ def supports_on_duplicate_key_update? #:nodoc:
16
+ true
17
+ end
18
+ end
19
+
14
20
  class MissingColumnError < StandardError
15
21
  def initialize(name, index)
16
22
  super "Missing column for value <#{name}> at index #{index}"
@@ -576,7 +582,7 @@ class ActiveRecord::Base
576
582
  if respond_to?(:timestamp_attributes_for_update, true)
577
583
  send(:timestamp_attributes_for_update).map(&:to_sym)
578
584
  else
579
- new.send(:timestamp_attributes_for_update_in_model)
585
+ allocate.send(:timestamp_attributes_for_update_in_model)
580
586
  end
581
587
  end
582
588
 
@@ -697,8 +703,7 @@ class ActiveRecord::Base
697
703
  # keep track of the instance and the position it is currently at. if this fails
698
704
  # validation we'll use the index to remove it from the array_of_attributes
699
705
  arr.each_with_index do |hsh, i|
700
- model = new
701
- hsh.each_pair { |k, v| model[k] = v }
706
+ model = new(hsh)
702
707
  next if validator.valid_model?(model)
703
708
  raise(ActiveRecord::RecordInvalid, model) if options[:raise_error]
704
709
  array_of_attributes[i] = nil
@@ -995,7 +1000,7 @@ class ActiveRecord::Base
995
1000
  timestamp_columns[:create] = timestamp_attributes_for_create_in_model
996
1001
  timestamp_columns[:update] = timestamp_attributes_for_update_in_model
997
1002
  else
998
- instance = new
1003
+ instance = allocate
999
1004
  timestamp_columns[:create] = instance.send(:timestamp_attributes_for_create_in_model)
1000
1005
  timestamp_columns[:update] = instance.send(:timestamp_attributes_for_update_in_model)
1001
1006
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Import
3
- VERSION = "1.0.5".freeze
3
+ VERSION = "1.0.6".freeze
4
4
  end
5
5
  end
@@ -900,4 +900,33 @@ describe "#import" do
900
900
  end
901
901
  end
902
902
  end
903
+ describe "importing model with after_initialize callback" do
904
+ let(:columns) { %w(name size) }
905
+ let(:valid_values) { [%w("Deer", "Small"), %w("Monkey", "Medium")] }
906
+ let(:invalid_values) do
907
+ [
908
+ { name: "giraffe", size: "Large" },
909
+ { size: "Medium" } # name is missing
910
+ ]
911
+ end
912
+ context "with validation checks turned off" do
913
+ it "should import valid data" do
914
+ Animal.import(columns, valid_values, validate: false)
915
+ assert_equal 2, Animal.count
916
+ end
917
+ it "should raise ArgumentError" do
918
+ assert_raise(ArgumentError) { Animal.import(invalid_values, validate: false) }
919
+ end
920
+ end
921
+
922
+ context "with validation checks turned on" do
923
+ it "should import valid data" do
924
+ Animal.import(columns, valid_values, validate: true)
925
+ assert_equal 2, Animal.count
926
+ end
927
+ it "should raise ArgumentError" do
928
+ assert_raise(ArgumentError) { Animal.import(invalid_values, validate: true) }
929
+ end
930
+ end
931
+ end
903
932
  end
@@ -0,0 +1,6 @@
1
+ class Animal < ActiveRecord::Base
2
+ after_initialize :validate_name_presence, if: :new_record?
3
+ def validate_name_presence
4
+ raise ArgumentError if name.nil?
5
+ end
6
+ end
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.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Dennis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-19 00:00:00.000000000 Z
11
+ date: 2020-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -121,6 +121,7 @@ files:
121
121
  - test/makara_postgis/import_test.rb
122
122
  - test/models/account.rb
123
123
  - test/models/alarm.rb
124
+ - test/models/animal.rb
124
125
  - test/models/bike_maker.rb
125
126
  - test/models/book.rb
126
127
  - test/models/car.rb
@@ -170,7 +171,7 @@ homepage: http://github.com/zdennis/activerecord-import
170
171
  licenses:
171
172
  - MIT
172
173
  metadata: {}
173
- post_install_message:
174
+ post_install_message:
174
175
  rdoc_options: []
175
176
  require_paths:
176
177
  - lib
@@ -186,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
187
  version: '0'
187
188
  requirements: []
188
189
  rubygems_version: 3.0.6
189
- signing_key:
190
+ signing_key:
190
191
  specification_version: 4
191
192
  summary: Bulk insert extension for ActiveRecord
192
193
  test_files:
@@ -211,6 +212,7 @@ test_files:
211
212
  - test/makara_postgis/import_test.rb
212
213
  - test/models/account.rb
213
214
  - test/models/alarm.rb
215
+ - test/models/animal.rb
214
216
  - test/models/bike_maker.rb
215
217
  - test/models/book.rb
216
218
  - test/models/car.rb