chewy
chewy copied to clipboard
Avoid Early DB Connection when Using `index_scope` w/ ActiveRecord Relation
Is your feature request related to a problem? Please describe.
When using Index.index_scope
we would like to pass an ActiveRecord Relation, using ActiveRecord::Query#includes, like so:
class MyIndex < Chewy::Index
index_scope MyRecord.includes(:company, :owner, :parent, :children)
...
In our environments that eager load classes, like production, we find that this causes some very early database connections.
This also has the consequence of causing our rails app to fail to boot if the database is down. We would like to avoid this coupling in our application.
The stack trace where this is happening:
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.0.8.4/lib/active_record/connection_handling.rb:313:in `retrieve_connection'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.0.8.4/lib/active_record/connection_handling.rb:280:in `connection'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.0.8.4/lib/active_record/relation/delegation.rb:93:in `connection'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.0.8.4/lib/active_record/relation/query_methods.rb:1625:in `preprocess_order_args'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.0.8.4/lib/active_record/relation/query_methods.rb:498:in `reorder!'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.0.8.4/lib/active_record/relation/query_methods.rb:493:in `reorder'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/chewy-8.0.0.pre.beta/lib/chewy/index/adapter/active_record.rb:31:in `cleanup_default_scope!'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/chewy-8.0.0.pre.beta/lib/chewy/index/adapter/orm.rb:18:in `initialize'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/chewy-8.0.0.pre.beta/lib/chewy/index.rb:167:in `new'
from /Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/chewy-8.0.0.pre.beta/lib/chewy/index.rb:167:in `index_scope'
from /Users/john/Code/my_api/app/elastic_indexing/my_index.rb:4:in `<class:MyIndex>'
from /Users/john/Code/my_api/app/elastic_indexing/my_index.rb:1:in `<top (required)>'
from <internal:/Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
from <internal:/Users/john/.asdf/installs/ruby/3.3.5/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
Note that it's Chewy's cleanup_default_scope!
call to reorder
that triggers the establishment of a connection.
Describe the solution you'd like
I would like to be able to avoid establishing a connection when using this pattern, until the db connection is needed.
OR
I would like to be able to call index_scope
passing a lambda or proc, while also telling index_scope to use the ActiveRecord adapter.
Describe alternatives you've considered
-
Based on https://github.com/toptal/chewy/pull/925 I had hoped that setting
Chewy.import_scope_cleanup_behavior = :ignore
would avoid this, however it looks like reorder is called regardless of this being set. -
It also technically "works" to pass a lambda or proc to index_scope, but this method isn't able to leverage the activerecord adapter properly.
Additional context
We're currently using 8.0.0.pre.beta, but all the same code is present in master.