Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toptal/chewy v7.0.1 #5

Merged
merged 16 commits into from
Nov 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid fetching scope data to check if it is blank
Dalton committed Feb 12, 2021
commit b2c4d0c231e7ed0531be5808309f56f3ac2232fc
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 10 additions & 1 deletion lib/chewy/type/import.rb
Original file line number Diff line number Diff line change
@@ -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!

@@ -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|
9 changes: 9 additions & 0 deletions spec/chewy/type/import_spec.rb
Original file line number Diff line number Diff line change
@@ -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\s+1\s+AS\s+one\s+FROM/) do
import City.where(id: dummy_cities.first.id)
end
end
end
end

5 changes: 3 additions & 2 deletions spec/support/active_record.rb
Original file line number Diff line number Diff line change
@@ -39,14 +39,15 @@ 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
raise "Expected no DB queries, but the following ones were made: #{ofending_queries.join(', ')}" if ofending_queries.present?
end

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