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



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

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

.current_connection_pool_config=(config) ⇒ Object



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

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

.default_configObject



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

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
27
# File 'lib/activerecord_partitioning.rb', line 18

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