Skip to content

Commit

Permalink
Avoid fetching scope data to check if it is blank
Browse files Browse the repository at this point in the history
  • Loading branch information
Dalton committed Feb 12, 2021
1 parent bec5284 commit a5d2240
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Changes

* [#761](https://github.com/toptal/chewy/pull/761): Avoid fetching scope data to check if it is blank ([@dalthon][])

### Bugs Fixed

## 6.0.0 (2021-02-11)
Expand Down
11 changes: 10 additions & 1 deletion lib/chewy/type/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def compose(object, crutches = nil, fields: [])
private

def import_routine(*args)
return if args.first.blank? && !args.first.nil?
return if !args.first.nil? && empty_objects_or_scope?(args.first)

routine = Routine.new(self, **args.extract_options!)
routine.create_indexes!

Expand All @@ -138,6 +139,14 @@ def import_routine(*args)
end
end

def empty_objects_or_scope?(objects_or_scope)
if objects_or_scope.respond_to?(:empty?)
objects_or_scope.empty?
else
objects_or_scope.blank?
end
end

def import_linear(objects, routine)
ActiveSupport::Notifications.instrument 'import_objects.chewy', type: self do |payload|
adapter.import(*objects, routine.options) do |action_objects|
Expand Down
9 changes: 9 additions & 0 deletions spec/chewy/type/import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ def subscribe_notification
specify do
expect { import City.where(id: dummy_cities.first.id) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first).only
end

specify do
allow(CitiesIndex::City).to receive(:import_linear).and_return(double(present?: false))
allow(CitiesIndex::City).to receive(:import_parallel).and_return(double(present?: false))

expects_no_query(except: /SELECT 1 AS one FROM/) do
import City.where(id: dummy_cities.first.id)
end
end
end
end

Expand Down
7 changes: 5 additions & 2 deletions spec/support/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ def expects_db_queries(&block)
raise 'Expected some db queries, but none were made' unless have_queries
end

def expects_no_query(&block)
def expects_no_query(except: nil, &block)
queries = []
ActiveSupport::Notifications.subscribed(
->(*args) { queries << args[4][:sql] },
'sql.active_record',
&block
)
raise "Expected no DB queries, but the following ones were made: #{queries.join(', ')}" if queries.present?
ofending_queries = except ? queries.find_all { |query| !query.match(except) } : queries
if ofending_queries.present?
raise "Expected no DB queries, but the following ones were made: #{ofending_queries.join(', ')}"
end
end

def stub_model(name, superclass = nil, &block)
Expand Down

0 comments on commit a5d2240

Please sign in to comment.