Skip to content

Commit

Permalink
Merge pull request #822 from dombesz/fix-use-locking-enabled
Browse files Browse the repository at this point in the history
Use ActiveRecord::Base.locking_enabled? to update the lock field
  • Loading branch information
jkowens committed Nov 22, 2023
2 parents 14a88ba + 8bce353 commit 22e077b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def import_helper( *args )
options.merge!( args.pop ) if args.last.is_a? Hash
# making sure that current model's primary key is used
options[:primary_key] = primary_key
options[:locking_column] = locking_column if attribute_names.include?(locking_column)
options[:locking_column] = locking_column if locking_enabled?

is_validating = options[:validate_with_context].present? ? true : options[:validate]
validator = ActiveRecord::Import::Validator.new(self, options)
Expand Down
28 changes: 28 additions & 0 deletions test/support/shared_examples/on_duplicate_key_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ def should_support_basic_on_duplicate_key_update
end
end
end

context 'with locking disabled' do
it 'does not update the lock_version' do
users = [
User.new(name: 'Salomon'),
User.new(name: 'Nathan')
]
User.import(users)
assert User.count == users.length
User.all.each do |user|
assert_equal 0, user.lock_version
end
updated_users = User.all.map do |user|
user.name += ' Rothschild'
user
end

ActiveRecord::Base.lock_optimistically = false # Disable locking
User.import(updated_users, on_duplicate_key_update: [:name])
ActiveRecord::Base.lock_optimistically = true # Enable locking

assert User.count == updated_users.length
User.all.each_with_index do |user, i|
assert_equal user.name, "#{users[i].name} Rothschild"
assert_equal 0, user.lock_version
end
end
end
end

context "with :on_duplicate_key_update" do
Expand Down

0 comments on commit 22e077b

Please sign in to comment.