activerecord-import 0.23.0 → 0.24.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: 25b9f077b6e370081a1c29b59ba3ce50be68659c
4
- data.tar.gz: adc5eff35697da10f5a6e3922980dfdd3ea849a6
3
+ metadata.gz: 7c157e7eabb443b03aa347b06bb39d8e50a685a2
4
+ data.tar.gz: ec11c5ca04b49a03aa98a19eac8c768ade01e7c0
5
5
  SHA512:
6
- metadata.gz: c8211cc8ed17997e92e944c1d36533ce501d18401da25df549e700fdd37e4223f5bcef2a342b3c8aa74af31d23d16c932a6e39b2445dba83dc8866575da16196
7
- data.tar.gz: 293a3eb933956824ec0fd0b9dfe760ba43d262b3154a5286f3cb33d677ae0c2949effd9c92006dbaa97f5748d97b289bd2483c5b68933ca8e2d8698c18d26111
6
+ metadata.gz: 3ac6e43987fe5b0309ca4a8346326d4f1c32cf5f30d8b342b260552d26caeb3bdc8bc0f8a89c9ac4671756f94797dc960cb70642eb70da1d8fec35b1a0eced95
7
+ data.tar.gz: ca600fdd1ec92ec39f34ab89216dee9137abfbf710dec6bd4678a6a7d130afab06a1a90631ab2aec1f3017842291e6e79177b8300d02a7b5498dd6ea42fb2dbf
@@ -1,11 +1,18 @@
1
+ ## Changes in 0.24.0
2
+
3
+ ### Fixes
4
+
5
+ * Use the association primary key when importing. Thanks to @dpogue via \#512.
6
+ * Allow association ids to be updated. Thanks to @Aristat via \#515.
7
+
1
8
  ## Changes in 0.23.0
2
9
 
3
10
  ### New Features
4
11
 
5
- * Rename `import` method to `bulk_import and alias to `import`. Thanks
12
+ * Rename `import` method to `bulk_import` and alias to `import`. Thanks
6
13
  to @itay-grudev, @jkowens via \#498.
7
14
  * Increment lock_version on duplicate key update. Thanks to @aimerald
8
- via \#400.
15
+ via \#500.
9
16
 
10
17
  ### Fixes
11
18
 
@@ -52,13 +52,13 @@ If `fake_name` adapter is needed by a gem (potentially called `activerecord-impo
52
52
  activerecord-import-fake_name/
53
53
  |-- activerecord-import-fake_name.gemspec
54
54
  |-- lib
55
+ | |-- activerecord-import-fake_name.rb
55
56
  | |-- activerecord-import-fake_name
56
57
  | | |-- version.rb
57
58
  | |-- activerecord-import
58
59
  | | |-- active_record
59
60
  | | | |-- adapters
60
61
  | | | |-- fake_name_adapter.rb
61
- |--activerecord-import-fake_name.rb
62
62
  ```
63
63
 
64
64
  When rubygems pushes the `lib` folder onto the load path a `require` will now find `activerecord-import/active_record/adapters/fake_name_adapter` as it runs through the lookup process for a ruby file under that path in `$LOAD_PATH`
@@ -97,7 +97,7 @@ class ActiveRecord::Associations::CollectionAssociation
97
97
  symbolized_foreign_key = reflection.foreign_key.to_sym
98
98
  symbolized_column_names = model_klass.column_names.map(&:to_sym)
99
99
 
100
- owner_primary_key = owner.class.primary_key
100
+ owner_primary_key = reflection.active_record_primary_key.to_sym
101
101
  owner_primary_key_value = owner.send(owner_primary_key)
102
102
 
103
103
  # assume array of model objects
@@ -276,6 +276,9 @@ class ActiveRecord::Base
276
276
  # == Options
277
277
  # * +validate+ - true|false, tells import whether or not to use
278
278
  # ActiveRecord validations. Validations are enforced by default.
279
+ # It skips the uniqueness validation for performance reasons.
280
+ # You can find more details here:
281
+ # https://github.com/zdennis/activerecord-import/issues/228
279
282
  # * +ignore+ - true|false, an alias for on_duplicate_key_ignore.
280
283
  # * +on_duplicate_key_ignore+ - true|false, tells import to discard
281
284
  # records that contain duplicate keys. For Postgres 9.5+ it adds
@@ -785,13 +788,14 @@ class ActiveRecord::Base
785
788
  def load_association_ids(model)
786
789
  association_reflections = model.class.reflect_on_all_associations(:belongs_to)
787
790
  association_reflections.each do |association_reflection|
791
+ column_name = association_reflection.foreign_key
788
792
  next if association_reflection.options[:polymorphic]
789
793
  association = model.association(association_reflection.name)
790
794
  association = association.target
791
- if association
792
- association_primary_key = association_reflection.association_primary_key
793
- model.public_send("#{association_reflection.foreign_key}=", association.send(association_primary_key))
794
- end
795
+ next if association.blank? || model.public_send(column_name).present?
796
+
797
+ association_primary_key = association_reflection.association_primary_key
798
+ model.public_send("#{column_name}=", association.send(association_primary_key))
795
799
  end
796
800
  end
797
801
 
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Import
3
- VERSION = "0.23.0".freeze
3
+ VERSION = "0.24.0".freeze
4
4
  end
5
5
  end
@@ -596,6 +596,14 @@ describe "#import" do
596
596
  assert_equal [val1, val2], scope.map(&column).sort
597
597
  end
598
598
  end
599
+
600
+ it "works with a non-standard association primary key" do
601
+ user = User.create(id: 1, name: 'Solomon')
602
+ user.user_tokens.import [:id, :token], [[5, '12345abcdef67890']]
603
+
604
+ token = UserToken.find(5)
605
+ assert_equal 'Solomon', token.user_name
606
+ end
599
607
  end
600
608
  end
601
609
 
@@ -1 +1,3 @@
1
- class User < ActiveRecord::Base; end
1
+ class User < ActiveRecord::Base
2
+ has_many :user_tokens, primary_key: :name, foreign_key: :user_name
3
+ end
@@ -0,0 +1,3 @@
1
+ class UserToken < ActiveRecord::Base
2
+ belongs_to :user, primary_key: :name, foreign_key: :user_name
3
+ end
@@ -164,6 +164,11 @@ ActiveRecord::Schema.define do
164
164
  t.integer :lock_version, null: false, default: 0
165
165
  end
166
166
 
167
+ create_table :user_tokens, force: :cascade do |t|
168
+ t.string :user_name, null: false
169
+ t.string :token, null: false
170
+ end
171
+
167
172
  create_table :accounts, force: :cascade do |t|
168
173
  t.string :name, null: false
169
174
  t.integer :lock, null: false, default: 0
@@ -116,7 +116,10 @@ def should_support_recursive_import
116
116
  end
117
117
 
118
118
  it "imports an imported belongs_to association id" do
119
- books = new_topics[0].books.to_a
119
+ first_new_topic = new_topics[0]
120
+ second_new_topic = new_topics[1]
121
+
122
+ books = first_new_topic.books.to_a
120
123
  Topic.import new_topics, validate: false
121
124
 
122
125
  assert_difference "Book.count", books.size do
@@ -124,7 +127,16 @@ def should_support_recursive_import
124
127
  end
125
128
 
126
129
  books.each do |book|
127
- assert_not_nil book.topic_id
130
+ assert_equal book.topic_id, first_new_topic.id
131
+ end
132
+
133
+ books.each { |book| book.topic_id = second_new_topic.id }
134
+ assert_no_difference "Book.count", books.size do
135
+ Book.import books, validate: false, on_duplicate_key_update: [:topic_id]
136
+ end
137
+
138
+ books.each do |book|
139
+ assert_equal book.topic_id, second_new_topic.id
128
140
  end
129
141
  end
130
142
 
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: 0.23.0
4
+ version: 0.24.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: 2018-05-04 00:00:00.000000000 Z
11
+ date: 2018-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -131,6 +131,7 @@ files:
131
131
  - test/models/tag.rb
132
132
  - test/models/topic.rb
133
133
  - test/models/user.rb
134
+ - test/models/user_token.rb
134
135
  - test/models/vendor.rb
135
136
  - test/models/widget.rb
136
137
  - test/mysql2/import_test.rb
@@ -218,6 +219,7 @@ test_files:
218
219
  - test/models/tag.rb
219
220
  - test/models/topic.rb
220
221
  - test/models/user.rb
222
+ - test/models/user_token.rb
221
223
  - test/models/vendor.rb
222
224
  - test/models/widget.rb
223
225
  - test/mysql2/import_test.rb