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

Avoid Early DB Connection when Using index_scope w/ ActiveRecord Relation #970

Open
jpterry opened this issue Oct 16, 2024 · 0 comments
Open

Comments

@jpterry
Copy link

jpterry commented Oct 16, 2024

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

Additional context

We're currently using 8.0.0.pre.beta, but all the same code is present in master.

@jpterry jpterry changed the title Allow Index.index_scope ActiveRecord relation to avoid an early DB Connection. Avoid Early DB Connection when Using index_scope w/ ActiveRecord Relation Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant