activerecord-import 1.2.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yaml +51 -11
  3. data/.rubocop.yml +74 -8
  4. data/.rubocop_todo.yml +6 -16
  5. data/Brewfile +3 -1
  6. data/CHANGELOG.md +37 -0
  7. data/Gemfile +8 -10
  8. data/README.markdown +14 -11
  9. data/Rakefile +2 -0
  10. data/activerecord-import.gemspec +4 -3
  11. data/benchmarks/benchmark.rb +10 -4
  12. data/benchmarks/lib/base.rb +4 -2
  13. data/benchmarks/lib/cli_parser.rb +4 -2
  14. data/benchmarks/lib/float.rb +2 -0
  15. data/benchmarks/lib/mysql2_benchmark.rb +2 -0
  16. data/benchmarks/lib/output_to_csv.rb +2 -0
  17. data/benchmarks/lib/output_to_html.rb +4 -2
  18. data/benchmarks/models/test_innodb.rb +2 -0
  19. data/benchmarks/models/test_memory.rb +2 -0
  20. data/benchmarks/models/test_myisam.rb +2 -0
  21. data/benchmarks/schema/mysql2_schema.rb +2 -0
  22. data/gemfiles/4.2.gemfile +2 -0
  23. data/gemfiles/5.0.gemfile +2 -0
  24. data/gemfiles/5.1.gemfile +2 -0
  25. data/gemfiles/5.2.gemfile +2 -0
  26. data/gemfiles/6.0.gemfile +2 -0
  27. data/gemfiles/6.1.gemfile +3 -0
  28. data/gemfiles/7.0.gemfile +4 -0
  29. data/lib/activerecord-import/active_record/adapters/abstract_adapter.rb +2 -0
  30. data/lib/activerecord-import/active_record/adapters/jdbcmysql_adapter.rb +2 -0
  31. data/lib/activerecord-import/active_record/adapters/jdbcpostgresql_adapter.rb +2 -0
  32. data/lib/activerecord-import/active_record/adapters/jdbcsqlite3_adapter.rb +2 -0
  33. data/lib/activerecord-import/active_record/adapters/mysql2_adapter.rb +2 -0
  34. data/lib/activerecord-import/active_record/adapters/postgresql_adapter.rb +2 -0
  35. data/lib/activerecord-import/active_record/adapters/seamless_database_pool_adapter.rb +2 -0
  36. data/lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb +2 -0
  37. data/lib/activerecord-import/adapters/abstract_adapter.rb +8 -5
  38. data/lib/activerecord-import/adapters/em_mysql2_adapter.rb +2 -0
  39. data/lib/activerecord-import/adapters/mysql2_adapter.rb +2 -0
  40. data/lib/activerecord-import/adapters/mysql_adapter.rb +27 -19
  41. data/lib/activerecord-import/adapters/postgresql_adapter.rb +66 -47
  42. data/lib/activerecord-import/adapters/sqlite3_adapter.rb +36 -30
  43. data/lib/activerecord-import/base.rb +3 -1
  44. data/lib/activerecord-import/import.rb +85 -44
  45. data/lib/activerecord-import/mysql2.rb +2 -0
  46. data/lib/activerecord-import/postgresql.rb +2 -0
  47. data/lib/activerecord-import/sqlite3.rb +2 -0
  48. data/lib/activerecord-import/synchronize.rb +3 -1
  49. data/lib/activerecord-import/value_sets_parser.rb +3 -0
  50. data/lib/activerecord-import/version.rb +3 -1
  51. data/lib/activerecord-import.rb +3 -1
  52. data/test/adapters/jdbcmysql.rb +2 -0
  53. data/test/adapters/jdbcpostgresql.rb +2 -0
  54. data/test/adapters/jdbcsqlite3.rb +2 -0
  55. data/test/adapters/makara_postgis.rb +2 -0
  56. data/test/adapters/mysql2.rb +2 -0
  57. data/test/adapters/mysql2_makara.rb +2 -0
  58. data/test/adapters/mysql2spatial.rb +2 -0
  59. data/test/adapters/postgis.rb +2 -0
  60. data/test/adapters/postgresql.rb +2 -0
  61. data/test/adapters/postgresql_makara.rb +2 -0
  62. data/test/adapters/seamless_database_pool.rb +2 -0
  63. data/test/adapters/spatialite.rb +2 -0
  64. data/test/adapters/sqlite3.rb +2 -0
  65. data/test/import_test.rb +26 -1
  66. data/test/jdbcmysql/import_test.rb +5 -3
  67. data/test/jdbcpostgresql/import_test.rb +4 -2
  68. data/test/jdbcsqlite3/import_test.rb +4 -2
  69. data/test/makara_postgis/import_test.rb +4 -2
  70. data/test/models/account.rb +2 -0
  71. data/test/models/alarm.rb +2 -0
  72. data/test/models/animal.rb +2 -0
  73. data/test/models/bike_maker.rb +3 -0
  74. data/test/models/book.rb +2 -0
  75. data/test/models/car.rb +2 -0
  76. data/test/models/card.rb +2 -0
  77. data/test/models/chapter.rb +2 -0
  78. data/test/models/customer.rb +8 -0
  79. data/test/models/deck.rb +2 -0
  80. data/test/models/dictionary.rb +2 -0
  81. data/test/models/discount.rb +2 -0
  82. data/test/models/end_note.rb +2 -0
  83. data/test/models/group.rb +2 -0
  84. data/test/models/order.rb +8 -0
  85. data/test/models/playing_card.rb +2 -0
  86. data/test/models/promotion.rb +2 -0
  87. data/test/models/question.rb +2 -0
  88. data/test/models/rule.rb +2 -0
  89. data/test/models/tag.rb +3 -0
  90. data/test/models/tag_alias.rb +5 -0
  91. data/test/models/topic.rb +7 -0
  92. data/test/models/user.rb +2 -0
  93. data/test/models/user_token.rb +2 -0
  94. data/test/models/vendor.rb +2 -0
  95. data/test/models/widget.rb +2 -0
  96. data/test/mysql2/import_test.rb +5 -3
  97. data/test/mysql2_makara/import_test.rb +5 -3
  98. data/test/mysqlspatial2/import_test.rb +5 -3
  99. data/test/postgis/import_test.rb +4 -2
  100. data/test/postgresql/import_test.rb +4 -2
  101. data/test/schema/generic_schema.rb +20 -0
  102. data/test/schema/jdbcpostgresql_schema.rb +3 -1
  103. data/test/schema/mysql2_schema.rb +2 -0
  104. data/test/schema/postgis_schema.rb +3 -1
  105. data/test/schema/postgresql_schema.rb +2 -0
  106. data/test/schema/sqlite3_schema.rb +2 -0
  107. data/test/schema/version.rb +2 -0
  108. data/test/sqlite3/import_test.rb +4 -2
  109. data/test/support/active_support/test_case_extensions.rb +2 -0
  110. data/test/support/assertions.rb +2 -0
  111. data/test/support/factories.rb +2 -0
  112. data/test/support/generate.rb +4 -2
  113. data/test/support/mysql/import_examples.rb +2 -1
  114. data/test/support/postgresql/import_examples.rb +65 -2
  115. data/test/support/shared_examples/on_duplicate_key_ignore.rb +2 -0
  116. data/test/support/shared_examples/on_duplicate_key_update.rb +41 -10
  117. data/test/support/shared_examples/recursive_import.rb +23 -1
  118. data/test/support/sqlite3/import_examples.rb +2 -1
  119. data/test/synchronize_test.rb +2 -0
  120. data/test/test_helper.rb +21 -5
  121. data/test/value_sets_bytes_parser_test.rb +3 -1
  122. data/test/value_sets_records_parser_test.rb +3 -1
  123. metadata +16 -12
  124. data/gemfiles/3.2.gemfile +0 -2
  125. data/gemfiles/4.0.gemfile +0 -2
  126. data/gemfiles/4.1.gemfile +0 -2
data/test/models/rule.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Rule < ActiveRecord::Base
2
4
  belongs_to :question
3
5
  end
data/test/models/tag.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tag < ActiveRecord::Base
2
4
  self.primary_keys = :tag_id, :publisher_id unless ENV["SKIP_COMPOSITE_PK"]
3
5
  has_many :books, inverse_of: :tag
6
+ has_many :tag_aliases, inverse_of: :tag
4
7
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TagAlias < ActiveRecord::Base
4
+ belongs_to :tag, foreign_key: [:tag_id, :parent_id], required: true
5
+ end
data/test/models/topic.rb CHANGED
@@ -1,4 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Topic < ActiveRecord::Base
4
+ if ENV['AR_VERSION'].to_i >= 6.0
5
+ self.ignored_columns = [:priority]
6
+ end
7
+ alias_attribute :name, :title
8
+
2
9
  validates_presence_of :author_name
3
10
  validates :title, numericality: { only_integer: true }, on: :context_test
4
11
  validates :title, uniqueness: true
data/test/models/user.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class User < ActiveRecord::Base
2
4
  has_many :user_tokens, primary_key: :name, foreign_key: :user_name
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class UserToken < ActiveRecord::Base
2
4
  belongs_to :user, primary_key: :name, foreign_key: :user_name
3
5
  validates :user, presence: true
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Vendor < ActiveRecord::Base
2
4
  store :preferences, accessors: [:color], coder: JSON
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CustomCoder
2
4
  def load(value)
3
5
  if value.nil?
@@ -1,5 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/../support/assertions')
3
- require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/import_examples')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/../test_helper")
4
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/assertions")
5
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/mysql/import_examples")
4
6
 
5
7
  should_support_mysql_import_functionality
@@ -1,6 +1,8 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
1
+ # frozen_string_literal: true
2
2
 
3
- require File.expand_path(File.dirname(__FILE__) + '/../support/assertions')
4
- require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/import_examples')
3
+ require File.expand_path("#{File.dirname(__FILE__)}/../test_helper")
4
+
5
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/assertions")
6
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/mysql/import_examples")
5
7
 
6
8
  should_support_mysql_import_functionality
@@ -1,6 +1,8 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
1
+ # frozen_string_literal: true
2
2
 
3
- require File.expand_path(File.dirname(__FILE__) + '/../support/assertions')
4
- require File.expand_path(File.dirname(__FILE__) + '/../support/mysql/import_examples')
3
+ require File.expand_path("#{File.dirname(__FILE__)}/../test_helper")
4
+
5
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/assertions")
6
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/mysql/import_examples")
5
7
 
6
8
  should_support_mysql_import_functionality
@@ -1,5 +1,7 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/../test_helper")
4
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/postgresql/import_examples")
3
5
 
4
6
  should_support_postgresql_import_functionality
5
7
 
@@ -1,4 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/../support/postgresql/import_examples')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/../test_helper")
4
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/postgresql/import_examples")
3
5
 
4
6
  should_support_postgresql_import_functionality
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  create_table :schema_info, force: :cascade do |t|
3
5
  t.integer :version, unique: true
@@ -20,6 +22,7 @@ ActiveRecord::Schema.define do
20
22
  t.boolean :approved, default: '1'
21
23
  t.integer :replies_count
22
24
  t.integer :parent_id
25
+ t.integer :priority, default: 0
23
26
  t.string :type
24
27
  t.datetime :created_at
25
28
  t.datetime :created_on
@@ -204,5 +207,22 @@ ActiveRecord::Schema.define do
204
207
  PRIMARY KEY (tag_id, publisher_id)
205
208
  );
206
209
  ).split.join(' ').strip
210
+
211
+ create_table :tag_aliases, force: :cascade do |t|
212
+ t.integer :tag_id, null: false
213
+ t.integer :parent_id, null: false
214
+ t.string :alias, null: false
215
+ end
216
+ end
217
+
218
+ create_table :customers, force: :cascade do |t|
219
+ t.integer :account_id
220
+ t.string :name
221
+ end
222
+
223
+ create_table :orders, force: :cascade do |t|
224
+ t.integer :account_id
225
+ t.integer :customer_id
226
+ t.integer :amount
207
227
  end
208
228
  end
@@ -1 +1,3 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/postgresql_schema')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/postgresql_schema")
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  create_table :books, force: :cascade do |t|
3
5
  t.string :title, null: false
@@ -1 +1,3 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/postgresql_schema')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/postgresql_schema")
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  execute('CREATE extension IF NOT EXISTS "hstore";')
3
5
  execute('CREATE extension IF NOT EXISTS "pgcrypto";')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ActiveRecord::Schema.define do
2
4
  create_table :alarms, force: true do |t|
3
5
  t.column :device_id, :integer, null: false
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class SchemaInfo < ActiveRecord::Base
2
4
  if respond_to?(:table_name=)
3
5
  self.table_name = 'schema_info'
@@ -1,4 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
- require File.expand_path(File.dirname(__FILE__) + '/../support/sqlite3/import_examples')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/../test_helper")
4
+ require File.expand_path("#{File.dirname(__FILE__)}/../support/sqlite3/import_examples")
3
5
 
4
6
  should_support_sqlite3_import_functionality
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ActiveSupport::TestCase
2
4
  include ActiveRecord::TestFixtures
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ActiveSupport::TestCase
2
4
  module ImportAssertions
3
5
  def self.extended(klass)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  sequence(:book_title) { |n| "Book #{n}" }
3
5
  sequence(:chapter_title) { |n| "Chapter #{n}" }
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ActiveSupport::TestCase
2
- def Build(*args) # rubocop:disable Style/MethodName
4
+ def Build(*args) # rubocop:disable Naming/MethodName
3
5
  n = args.shift if args.first.is_a?(Numeric)
4
6
  factory = args.shift
5
7
  factory_bot_args = args.shift || {}
@@ -13,7 +15,7 @@ class ActiveSupport::TestCase
13
15
  end
14
16
  end
15
17
 
16
- def Generate(*args) # rubocop:disable Style/MethodName
18
+ def Generate(*args) # rubocop:disable Naming/MethodName
17
19
  n = args.shift if args.first.is_a?(Numeric)
18
20
  factory = args.shift
19
21
  factory_bot_args = args.shift || {}
@@ -1,4 +1,5 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  def should_support_mysql_import_functionality
3
4
  # Forcefully disable strict mode for this session.
4
5
  ActiveRecord::Base.connection.execute "set sql_mode='STRICT_ALL_TABLES'"
@@ -1,4 +1,5 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  def should_support_postgresql_import_functionality
3
4
  should_support_recursive_import
4
5
 
@@ -102,6 +103,8 @@ def should_support_postgresql_import_functionality
102
103
  books.first.id.to_s
103
104
  end
104
105
  end
106
+ let(:true_returning_value) { ENV['AR_VERSION'].to_f >= 5.0 ? true : 't' }
107
+ let(:false_returning_value) { ENV['AR_VERSION'].to_f >= 5.0 ? false : 'f' }
105
108
 
106
109
  it "creates records" do
107
110
  assert_difference("Book.count", +1) { result }
@@ -150,6 +153,34 @@ def should_support_postgresql_import_functionality
150
153
  end
151
154
  end
152
155
 
156
+ context "when returning is raw sql" do
157
+ let(:result) { Book.import(books, returning: "title, (xmax = '0') AS inserted") }
158
+
159
+ setup { result }
160
+
161
+ it "returns ids" do
162
+ assert_equal [book_id], result.ids
163
+ end
164
+
165
+ it "returns specified columns" do
166
+ assert_equal [['It', true_returning_value]], result.results
167
+ end
168
+ end
169
+
170
+ context "when returning contains raw sql" do
171
+ let(:result) { Book.import(books, returning: [:title, "id, (xmax = '0') AS inserted"]) }
172
+
173
+ setup { result }
174
+
175
+ it "returns ids" do
176
+ assert_equal [book_id], result.ids
177
+ end
178
+
179
+ it "returns specified columns" do
180
+ assert_equal [['It', book_id, true_returning_value]], result.results
181
+ end
182
+ end
183
+
153
184
  context "setting model attributes" do
154
185
  let(:code) { 'abc' }
155
186
  let(:discount) { 0.10 }
@@ -179,6 +210,14 @@ def should_support_postgresql_import_functionality
179
210
  assert_equal updated_promotion.discount, discount
180
211
  end
181
212
  end
213
+
214
+ context 'returning raw sql' do
215
+ let(:returning_columns) { [:discount, "(xmax = '0') AS inserted"] }
216
+
217
+ it "sets custom model attributes" do
218
+ assert_equal updated_promotion.inserted, false_returning_value
219
+ end
220
+ end
182
221
  end
183
222
  end
184
223
  end
@@ -281,7 +320,7 @@ def should_support_postgresql_import_functionality
281
320
  end
282
321
 
283
322
  describe "with binary field" do
284
- let(:binary_value) { "\xE0'c\xB2\xB0\xB3Bh\\\xC2M\xB1m\\I\xC4r".force_encoding('ASCII-8BIT') }
323
+ let(:binary_value) { "\xE0'c\xB2\xB0\xB3Bh\\\xC2M\xB1m\\I\xC4r".dup.force_encoding('ASCII-8BIT') }
285
324
  it "imports the correct values for binary fields" do
286
325
  alarms = [Alarm.new(device_id: 1, alarm_type: 1, status: 1, secret_key: binary_value)]
287
326
  assert_difference "Alarm.count", +1 do
@@ -290,6 +329,30 @@ def should_support_postgresql_import_functionality
290
329
  assert_equal(binary_value, Alarm.first.secret_key)
291
330
  end
292
331
  end
332
+
333
+ unless ENV["SKIP_COMPOSITE_PK"]
334
+ describe "with composite foreign keys" do
335
+ let(:account_id) { 555 }
336
+ let(:customer) { Customer.new(account_id: account_id, name: "foo") }
337
+ let(:order) { Order.new(account_id: account_id, amount: 100, customer: customer) }
338
+
339
+ it "imports and correctly maps foreign keys" do
340
+ assert_difference "Customer.count", +1 do
341
+ Customer.import [customer]
342
+ end
343
+
344
+ assert_difference "Order.count", +1 do
345
+ Order.import [order]
346
+ end
347
+
348
+ db_customer = Customer.last
349
+ db_order = Order.last
350
+
351
+ assert_equal db_customer.orders.last, db_order
352
+ assert_not_equal db_order.customer_id, nil
353
+ end
354
+ end
355
+ end
293
356
  end
294
357
 
295
358
  def should_support_postgresql_upsert_functionality
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  def should_support_on_duplicate_key_ignore
2
4
  describe "#import" do
3
5
  extend ActiveSupport::TestCase::ImportAssertions
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  def should_support_basic_on_duplicate_key_update
2
4
  describe "#import" do
3
5
  extend ActiveSupport::TestCase::ImportAssertions
@@ -24,7 +26,7 @@ def should_support_basic_on_duplicate_key_update
24
26
  User.import(updated_users, on_duplicate_key_update: [:name])
25
27
  assert User.count == updated_users.length
26
28
  User.all.each_with_index do |user, i|
27
- assert_equal user.name, users[i].name + ' Rothschild'
29
+ assert_equal user.name, "#{users[i].name} Rothschild"
28
30
  assert_equal 1, user.lock_version
29
31
  end
30
32
  end
@@ -48,7 +50,7 @@ def should_support_basic_on_duplicate_key_update
48
50
  User.import(columns, updated_values, on_duplicate_key_update: [:name])
49
51
  assert User.count == updated_values.length
50
52
  User.all.each_with_index do |user, i|
51
- assert_equal user.name, users[i].name + ' Rothschild'
53
+ assert_equal user.name, "#{users[i].name} Rothschild"
52
54
  assert_equal 1, user.lock_version
53
55
  end
54
56
  end
@@ -70,7 +72,7 @@ def should_support_basic_on_duplicate_key_update
70
72
  User.import(updated_values, on_duplicate_key_update: [:name])
71
73
  assert User.count == updated_values.length
72
74
  User.all.each_with_index do |user, i|
73
- assert_equal user.name, users[i].name + ' Rothschild'
75
+ assert_equal user.name, "#{users[i].name} Rothschild"
74
76
  assert_equal 1, user.lock_version
75
77
  end
76
78
  updated_values2 = User.all.map do |user|
@@ -80,7 +82,7 @@ def should_support_basic_on_duplicate_key_update
80
82
  User.import(updated_values2, on_duplicate_key_update: [:name])
81
83
  assert User.count == updated_values2.length
82
84
  User.all.each_with_index do |user, i|
83
- assert_equal user.name, users[i].name + ' Rothschild jr.'
85
+ assert_equal user.name, "#{users[i].name} Rothschild jr."
84
86
  assert_equal 2, user.lock_version
85
87
  end
86
88
  end
@@ -102,7 +104,7 @@ def should_support_basic_on_duplicate_key_update
102
104
  Account.import(updated_accounts, on_duplicate_key_update: [:id, :name])
103
105
  assert Account.count == updated_accounts.length
104
106
  Account.all.each_with_index do |user, i|
105
- assert_equal user.name, accounts[i].name + ' Rothschild'
107
+ assert_equal user.name, "#{accounts[i].name} Rothschild"
106
108
  assert_equal 1, user.lock
107
109
  end
108
110
  end
@@ -126,7 +128,7 @@ def should_support_basic_on_duplicate_key_update
126
128
  Account.import(columns, updated_values, on_duplicate_key_update: [:name])
127
129
  assert Account.count == updated_values.length
128
130
  Account.all.each_with_index do |user, i|
129
- assert_equal user.name, accounts[i].name + ' Rothschild'
131
+ assert_equal user.name, "#{accounts[i].name} Rothschild"
130
132
  assert_equal 1, user.lock
131
133
  end
132
134
  end
@@ -148,7 +150,7 @@ def should_support_basic_on_duplicate_key_update
148
150
  Account.import(updated_values, on_duplicate_key_update: [:name])
149
151
  assert Account.count == updated_values.length
150
152
  Account.all.each_with_index do |user, i|
151
- assert_equal user.name, accounts[i].name + ' Rothschild'
153
+ assert_equal user.name, "#{accounts[i].name} Rothschild"
152
154
  assert_equal 1, user.lock
153
155
  end
154
156
  end
@@ -170,10 +172,11 @@ def should_support_basic_on_duplicate_key_update
170
172
  Bike::Maker.import(updated_makers, on_duplicate_key_update: [:name])
171
173
  assert Bike::Maker.count == updated_makers.length
172
174
  Bike::Maker.all.each_with_index do |maker, i|
173
- assert_equal maker.name, makers[i].name + ' bikes'
175
+ assert_equal maker.name, "#{makers[i].name} bikes"
174
176
  assert_equal 1, maker.lock_version
175
177
  end
176
178
  end
179
+
177
180
  it 'update the lock_version of models separated by namespaces by array' do
178
181
  makers = [
179
182
  Bike::Maker.new(name: 'Yamaha'),
@@ -193,7 +196,7 @@ def should_support_basic_on_duplicate_key_update
193
196
  Bike::Maker.import(columns, updated_values, on_duplicate_key_update: [:name])
194
197
  assert Bike::Maker.count == updated_values.length
195
198
  Bike::Maker.all.each_with_index do |maker, i|
196
- assert_equal maker.name, makers[i].name + ' bikes'
199
+ assert_equal maker.name, "#{makers[i].name} bikes"
197
200
  assert_equal 1, maker.lock_version
198
201
  end
199
202
  end
@@ -215,7 +218,7 @@ def should_support_basic_on_duplicate_key_update
215
218
  Bike::Maker.import(updated_values, on_duplicate_key_update: [:name])
216
219
  assert Bike::Maker.count == updated_values.length
217
220
  Bike::Maker.all.each_with_index do |maker, i|
218
- assert_equal maker.name, makers[i].name + ' bikes'
221
+ assert_equal maker.name, "#{makers[i].name} bikes"
219
222
  assert_equal 1, maker.lock_version
220
223
  end
221
224
  end
@@ -314,6 +317,34 @@ def should_support_basic_on_duplicate_key_update
314
317
  should_support_on_duplicate_key_update
315
318
  should_update_fields_mentioned
316
319
  end
320
+
321
+ context "using column aliases" do
322
+ let(:columns) { %w( id title author_name author_email_address parent_id ) }
323
+ let(:update_columns) { %w(title author_email_address parent_id) }
324
+
325
+ context "with column aliases in column list" do
326
+ let(:columns) { %w( id name author_name author_email_address parent_id ) }
327
+ should_support_on_duplicate_key_update
328
+ should_update_fields_mentioned
329
+ end
330
+
331
+ context "with column aliases in update columns list" do
332
+ let(:update_columns) { %w(name author_email_address parent_id) }
333
+ should_support_on_duplicate_key_update
334
+ should_update_fields_mentioned
335
+ end
336
+ end
337
+
338
+ if ENV['AR_VERSION'].to_i >= 6.0
339
+ context "using ignored columns" do
340
+ let(:columns) { %w( id title author_name author_email_address parent_id priority ) }
341
+ let(:values) { [[99, "Book", "John Doe", "john@doe.com", 17, 1]] }
342
+ let(:update_columns) { %w(name author_email_address parent_id priority) }
343
+ let(:updated_values) { [[99, "Book - 2nd Edition", "Author Should Not Change", "johndoe@example.com", 57, 2]] }
344
+ should_support_on_duplicate_key_update
345
+ should_update_fields_mentioned
346
+ end
347
+ end
317
348
  end
318
349
 
319
350
  context "with a table that has a non-standard primary key" do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  def should_support_recursive_import
2
4
  describe "importing objects with associations" do
3
5
  let(:new_topics) { Build(num_topics, :topic_with_book) }
@@ -176,7 +178,7 @@ def should_support_recursive_import
176
178
  end
177
179
  end
178
180
 
179
- # If adapter supports on_duplicate_key_update, it is only applied to top level models so that SQL with invalid
181
+ # If adapter supports on_duplicate_key_update and specific columns are specified, it is only applied to top level models so that SQL with invalid
180
182
  # columns, keys, etc isn't generated for child associations when doing recursive import
181
183
  if ActiveRecord::Base.connection.supports_on_duplicate_key_update?
182
184
  describe "on_duplicate_key_update" do
@@ -190,6 +192,26 @@ def should_support_recursive_import
190
192
  end
191
193
  end
192
194
  end
195
+
196
+ context "when :all fields are updated" do
197
+ setup do
198
+ Topic.import new_topics, recursive: true
199
+ end
200
+
201
+ it "updates associated objects" do
202
+ new_author_name = 'Richard Bachman'
203
+ topic = new_topics.first
204
+ topic.books.each do |book|
205
+ book.author_name = new_author_name
206
+ end
207
+ assert_nothing_raised do
208
+ Topic.import new_topics, recursive: true, on_duplicate_key_update: :all
209
+ end
210
+ Topic.find(topic.id).books.each do |book|
211
+ assert_equal new_author_name, book.author_name
212
+ end
213
+ end
214
+ end
193
215
  end
194
216
  end
195
217
 
@@ -1,4 +1,5 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  def should_support_sqlite3_import_functionality
3
4
  if ActiveRecord::Base.connection.supports_on_duplicate_key_update?
4
5
  should_support_sqlite_upsert_functionality
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('../test_helper', __FILE__)
2
4
 
3
5
  describe ".synchronize" do
data/test/test_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pathname'
2
4
  require 'rake'
3
5
  test_dir = Pathname.new File.dirname(__FILE__)
@@ -22,7 +24,7 @@ if ActiveSupport::VERSION::STRING < "4.0"
22
24
  require 'mocha/test_unit'
23
25
  else
24
26
  require 'active_support/testing/autorun'
25
- require "mocha/mini_test"
27
+ require "mocha/minitest"
26
28
  end
27
29
 
28
30
  require 'timecop'
@@ -50,15 +52,29 @@ FileUtils.mkdir_p 'log'
50
52
  ActiveRecord::Base.logger = Logger.new("log/test.log")
51
53
  ActiveRecord::Base.logger.level = Logger::DEBUG
52
54
 
55
+ if ActiveRecord.respond_to?(:use_yaml_unsafe_load)
56
+ ActiveRecord.use_yaml_unsafe_load = true
57
+ elsif ActiveRecord::Base.respond_to?(:use_yaml_unsafe_load)
58
+ ActiveRecord::Base.use_yaml_unsafe_load = true
59
+ end
60
+
53
61
  if ENV['AR_VERSION'].to_f >= 6.0
54
- yaml_config = YAML.load_file(test_dir.join("database.yml"))[adapter]
62
+ yaml_config = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.2.1')
63
+ YAML.safe_load_file(test_dir.join("database.yml"), aliases: true)[adapter]
64
+ else
65
+ YAML.load_file(test_dir.join("database.yml"))[adapter]
66
+ end
55
67
  config = ActiveRecord::DatabaseConfigurations::HashConfig.new("test", adapter, yaml_config)
56
68
  ActiveRecord::Base.configurations.configurations << config
57
69
  else
58
70
  ActiveRecord::Base.configurations["test"] = YAML.load_file(test_dir.join("database.yml"))[adapter]
59
71
  end
60
72
 
61
- ActiveRecord::Base.default_timezone = :utc
73
+ if ActiveRecord.respond_to?(:default_timezone)
74
+ ActiveRecord.default_timezone = :utc
75
+ else
76
+ ActiveRecord::Base.default_timezone = :utc
77
+ end
62
78
 
63
79
  require "activerecord-import"
64
80
  ActiveRecord::Base.establish_connection :test
@@ -68,7 +84,7 @@ ActiveSupport::Notifications.subscribe(/active_record.sql/) do |_, _, _, _, hsh|
68
84
  end
69
85
 
70
86
  require "factory_bot"
71
- Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |file| require file }
87
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |file| require file }
72
88
 
73
89
  # Load base/generic schema
74
90
  require test_dir.join("schema/version")
@@ -76,7 +92,7 @@ require test_dir.join("schema/generic_schema")
76
92
  adapter_schema = test_dir.join("schema/#{adapter}_schema.rb")
77
93
  require adapter_schema if File.exist?(adapter_schema)
78
94
 
79
- Dir[File.dirname(__FILE__) + "/models/*.rb"].each { |file| require file }
95
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].sort.each { |file| require file }
80
96
 
81
97
  # Prevent this deprecation warning from breaking the tests.
82
98
  Rake::FileList.send(:remove_method, :import)
@@ -1,4 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
2
4
 
3
5
  require 'activerecord-import/value_sets_parser'
4
6
 
@@ -1,4 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("#{File.dirname(__FILE__)}/test_helper")
2
4
 
3
5
  require 'activerecord-import/value_sets_parser'
4
6