module Shackles

Constants

VERSION

Public Class Methods

activate(environment) { || ... } click to toggle source

switch environment for the duration of the block will keep the old connections around

# File lib/shackles.rb, line 58
def activate(environment)
  environment ||= :master
  return yield if environment == self.environment
  begin
    old_environment = activate!(environment)
    activated_environments << environment
    yield
  ensure
    Thread.current[:shackles_environment] = old_environment
    ActiveRecord::Base.connection_handler = ensure_handler unless Rails.env.test?
  end
end
activate!(environment) click to toggle source

for use from script/console ONLY

# File lib/shackles.rb, line 72
def activate!(environment)
  environment ||= :master
  save_handler
  old_environment = self.environment
  Thread.current[:shackles_environment] = environment
  ActiveRecord::Base.connection_handler = ensure_handler unless Rails.env.test?
  old_environment
end
activated_environments() click to toggle source
# File lib/shackles.rb, line 13
def activated_environments
  @activated_environments ||= Set.new()
end
apply_config!(hash) click to toggle source

for altering other pieces of config (i.e. username) will force a disconnect

# File lib/shackles.rb, line 41
def apply_config!(hash)
  global_config.merge!(hash)
  bump_sequence
end
bump_sequence() click to toggle source
# File lib/shackles.rb, line 33
def bump_sequence
  @global_config_sequence ||= 1
  @global_config_sequence += 1
  ActiveRecord::Base::connection_handler.clear_all_connections!
end
connection_handlers() click to toggle source
# File lib/shackles.rb, line 51
def connection_handlers
  save_handler
  @connection_handlers
end
environment() click to toggle source
# File lib/shackles.rb, line 5
def environment
  Thread.current[:shackles_environment] ||= :master
end
global_config() click to toggle source
# File lib/shackles.rb, line 9
def global_config
  @global_config ||= {}
end
global_config_sequence() click to toggle source
# File lib/shackles.rb, line 29
def global_config_sequence
  @global_config_sequence ||= 1
end
initialize!() click to toggle source

semi-private

# File lib/shackles.rb, line 18
def initialize!
  require 'shackles/connection_handler'
  require 'shackles/connection_specification'
  require 'shackles/helper_methods'

  activated_environments << Shackles.environment

  ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(ConnectionHandler)
  ActiveRecord::ConnectionAdapters::ConnectionSpecification.prepend(ConnectionSpecification)
end
remove_config!(key) click to toggle source
# File lib/shackles.rb, line 46
def remove_config!(key)
  global_config.delete(key)
  bump_sequence
end

Private Class Methods

ensure_handler() click to toggle source
# File lib/shackles.rb, line 87
def ensure_handler
  new_handler = @connection_handlers[environment]
  if !new_handler
    new_handler = @connection_handlers[environment] = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
    pools = ActiveRecord::Base.connection_handler.send(:owner_to_pool)
    pools.each_pair do |model, pool|
      if Rails.version < '5'
        model = model.constantize
        new_handler.establish_connection(model, pool.spec)
      elsif Rails.version < '5.1'
        new_handler.establish_connection(pool.spec)
      else
        new_handler.establish_connection(pool.spec.config)
      end
    end
  end
  new_handler
end
save_handler() click to toggle source
# File lib/shackles.rb, line 82
def save_handler
  @connection_handlers ||= {}
  @connection_handlers[environment] ||= ActiveRecord::Base.connection_handler
end