Skip to content

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

@jpterry

Description

@jpterry

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions