Module: DbCharmer::ActiveRecord::MultiDbProxy::ClassMethods

Defined in:
lib/db_charmer/active_record/multi_db_proxy.rb

Instance Method Summary collapse

Instance Method Details

#on_db(con, proxy_target = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/db_charmer/active_record/multi_db_proxy.rb', line 29

def on_db(con, proxy_target = nil)
  proxy_target ||= self

  # This is for the block_schema_map method.
  # Setting the table_name_prefix is done in switch_connection_to
  # but we don't go through switch_connection_to when the connection is set
  # outside of shard_for.
  if self.respond_to? :set_schema_table_name_prefix
    self.set_schema_table_name_prefix(con)
  end
  
  # Chain call
  #Rails.logger.debug "MultiDbProxy#on_db: con=#{con} proxy_target=#{proxy_target}"
  return OnDbProxy.new(proxy_target, con) unless block_given?

  # Block call
  begin
    self.db_charmer_connection_level += 1
    old_proxy = db_charmer_connection_proxy
    switch_connection_to(con, DbCharmer.connections_should_exist?)
    yield(proxy_target)
  ensure
    switch_connection_to(old_proxy)
    self.db_charmer_connection_level -= 1
  end
end