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



40
41
42
# File 'lib/activerecord_partitioning.rb', line 40

def current_connection_pool_config
  Thread.current[:current_connection_pool_config]
end

.default_configObject



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

def default_config
  @default_config
end

.default_config=(config) ⇒ Object



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

def default_config=(config)
  @default_config = config
end

.resetObject



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

def reset
  self.default_config = nil
  ActiveRecord::Base.clear_active_connections!
  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 = {})
  self.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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/activerecord_partitioning.rb', line 19

def with_connection_pool(config, &block)
  config.try(:symbolize_keys!)
  origin = Thread.current[:current_connection_pool_config]
  Thread.current[: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
  ActiveRecord::Base.clear_active_connections!
  Thread.current[:current_connection_pool_config] = origin
end