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 =
'1.2.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_targetObject

Returns the value of attribute default_target.



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

def default_target
  @default_target
end

.enabledObject

Returns the value of attribute enabled.



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

def enabled
  @enabled
end

Class Method Details

.base_transaction_depthObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/knockoff.rb', line 52

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



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

def clear_all_active_connections!
  replica_pool.clear_all_active_connections!
end

.configObject



48
49
50
# File 'lib/knockoff.rb', line 48

def config
  @config ||= Config.new
end

.disconnect_all!Object

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



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

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.



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

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(check_transaction: true, &block) ⇒ Object



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

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

.replica_poolObject



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

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