Module: ActiveRecordPartitioning

Defined in:
lib/activerecord_partitioning.rb,
lib/activerecord_partitioning/connection_pools.rb

Defined Under Namespace

Classes: ConnectionPools, NoActiveConnectionPoolError

Class Method Summary collapse

Class Method Details

.current_connection_pool_configObject



32
33
34
# File 'lib/activerecord_partitioning.rb', line 32

def current_connection_pool_config
  Thread.current[:current_connection_pool_config] || self.default_config
end

.current_connection_pool_config=(config) ⇒ Object



36
37
38
# File 'lib/activerecord_partitioning.rb', line 36

def current_connection_pool_config=(config)
  Thread.current[:current_connection_pool_config] = config
end

.default_configObject



28
29
30
# File 'lib/activerecord_partitioning.rb', line 28

def default_config
  @default_config
end

.reset_connection_handlerObject



13
14
15
16
# File 'lib/activerecord_partitioning.rb', line 13

def reset_connection_handler
  @default_config = nil
  ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
end

.setup(key_name, default_config = nil, store = {}) ⇒ Object



6
7
8
9
10
11
# File 'lib/activerecord_partitioning.rb', line 6

def setup(key_name, default_config = nil, store = {})
  @default_config = default_config.try(:symbolize_keys)
  new_pools = ConnectionPools.new(key_name, store)
  new_pools.merge!(ActiveRecord::Base.connection_handler.connection_pools)
  ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new(new_pools)
end

.with_connection_pool(config, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/activerecord_partitioning.rb', line 18

def with_connection_pool(config, &block)
  self.current_connection_pool_config = config = config.symbolize_keys
  if ActiveRecord::Base.connection_pool.nil?
    ActiveRecord::Base.establish_connection((self.default_config || {}).merge(config))
  end
  yield if block_given?
ensure
  self.current_connection_pool_config = nil
end