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

prototype wrapping of query execution for AR cursor #59

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

eljojo
Copy link
Member

@eljojo eljojo commented Nov 13, 2020

hey @kirs @byroot i'm doing a prototype here, no real intentions of merging, any comments welcome. I am doing this for a personal project.

here's the context: I have a vanilla rails app and I'm trying to use multiple connections within the same job. I am using rails' connected_to block mechanism.

I am running into a problem where I switch connections during each_iteration, which makes the job finish after the first batch is done. It seems that AR switches the default to connection to whatever connected_to block was called last.

In any case, this is an attempt to make it possible to pass a block around AR enumerator builder, so I can wrap the query execution in the appropriate connected_to block.

usage is kinda weird but not the end of the world:

  def build_enumerator(klass_name, cursor:)
    enumerator_builder.active_record_on_records(
      klass_name.safe_constantize.all,
      cursor: cursor,
      around_query: -> (&block) { Shards.legacy(&block) }
    )
  end

@kirs
Copy link
Contributor

kirs commented Nov 16, 2020

Hmm. I'd love to make it work nicely with AR's multi-database feature. Ideally with developers not having to pass around_query:.

I think we could scope this out to vanilla ActiveRecord gist like:

enum = Enumerator.new do |yielder|
  ActiveRecord::Base.connected_to(role: :foo) do
     Shop.find_each { |shop| yielder.yield(record) }
  end
end

enum.each do
  ActiveRecord::Base.connected_to(role: :bar) do
    # do something else
  end
end

Would that work? If not maybe it's an edge case of ActiveRecord that we could take care of upstream.

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

Successfully merging this pull request may close these issues.

2 participants