Module: Knockoff

Defined in:
lib/knockoff.rb,
lib/knockoff/base.rb,
lib/knockoff/error.rb,
lib/knockoff/config.rb,
lib/knockoff/version.rb,
lib/knockoff/replica_connection_pool.rb

Defined Under Namespace

Classes: Base, Config, Error, ReplicaConnectionPool

Constant Summary collapse

VERSION =
'0.2.3'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

Class Method Details

.base_transaction_depthObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/knockoff.rb', line 48

def base_transaction_depth
  @base_transaction_depth ||= begin
    testcase = ActiveSupport::TestCase
    if defined?(testcase) &&
        testcase.respond_to?(:use_transactional_fixtures) &&
        testcase.try(:use_transactional_fixtures)
      1
    else
      0
    end
  end
end

.clear_all_active_connections!Object



27
28
29
# File 'lib/knockoff.rb', line 27

def clear_all_active_connections!
  replica_pool.clear_all_active_connections!
end

.configObject



44
45
46
# File 'lib/knockoff.rb', line 44

def config
  @config ||= Config.new
end

.disconnect_all!Object

Iterates through the replica pool and calls disconnect on each one’s connection.



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

def disconnect_all!
  replica_pool.disconnect_all_replicas!
end

.establish_new_connections!(new_config) ⇒ Object

Updates the config (both internal representation and the ActiveRecord::Base.configuration) with the new config, and then reconnects each replica connection in the replica pool.



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

def establish_new_connections!(new_config)
  config.update_replica_configs(new_config)
  replica_pool.reconnect_all_replicas!
end

.on_primary(&block) ⇒ Object



19
20
21
# File 'lib/knockoff.rb', line 19

def on_primary(&block)
  Base.new(:primary).run(&block)
end

.on_replica(&block) ⇒ Object



15
16
17
# File 'lib/knockoff.rb', line 15

def on_replica(&block)
  Base.new(:replica).run(&block)
end

.replica_poolObject



23
24
25
# File 'lib/knockoff.rb', line 23

def replica_pool
  @replica_pool ||= ReplicaConnectionPool.new(config.replica_database_keys)
end