Module: ActiveRecordSlave

Defined in:
lib/active_record_slave/railtie.rb,
lib/active_record_slave/slave.rb,
lib/active_record_slave/version.rb,
lib/active_record_slave/instance_methods.rb,
lib/active_record_slave/active_record_slave.rb

Overview

ActiveRecord read from a slave

Defined Under Namespace

Modules: InstanceMethods Classes: Railtie, Slave

Constant Summary collapse

VERSION =
'1.3.0'
SELECT_METHODS =

Select Methods

[:select, :select_all, :select_one, :select_rows, :select_value, :select_values]

Class Method Summary collapse

Class Method Details

.ignore_transactions=(ignore_transactions) ⇒ Object

Set whether slave reads should ignore transactions



69
70
71
# File 'lib/active_record_slave/active_record_slave.rb', line 69

def self.ignore_transactions=(ignore_transactions)
  @ignore_transactions = ignore_transactions
end

.ignore_transactions?Boolean

Returns whether slave reads are ignoring transactions

Returns:

  • (Boolean)


64
65
66
# File 'lib/active_record_slave/active_record_slave.rb', line 64

def self.ignore_transactions?
  @ignore_transactions
end

.install!(adapter_class = nil, environment = nil) ⇒ Object

Install ActiveRecord::Slave into ActiveRecord to redirect reads to the slave Parameters:

adapter_class:
  By default, only the default Database adapter (ActiveRecord::Base.connection.class)
  is extended with slave read capabilities

environment:
  In a non-Rails environment, supply the environment such as
  'development', 'production'


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record_slave/active_record_slave.rb', line 15

def self.install!(adapter_class = nil, environment = nil)
  slave_config =
    if ActiveRecord::Base.connection.respond_to?(:config)
      ActiveRecord::Base.connection.config[:slave]
    else
      ActiveRecord::Base.configurations[environment || Rails.env]['slave']
    end
  if slave_config
    ActiveRecord::Base.logger.info "ActiveRecordSlave.install! v#{ActiveRecordSlave::VERSION} Establishing connection to slave database"
    Slave.establish_connection(slave_config)

    # Inject a new #select method into the ActiveRecord Database adapter
    base = adapter_class || ActiveRecord::Base.connection.class
    base.send(:include, InstanceMethods)
  else
    ActiveRecord::Base.logger.info "ActiveRecordSlave not installed since no slave database defined"
  end
end

.read_from_masterObject

Force reads for the supplied block to read from the master database Only applies to calls made within the current thread



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_record_slave/active_record_slave.rb', line 36

def self.read_from_master
  return yield if read_from_master?
  begin
    # Set :master indicator in thread local storage so that it is visible
    # during the select call
    read_from_master!
    yield
  ensure
    read_from_slave!
  end
end

.read_from_master!Object

Force all subsequent reads on this thread and any fibers called by this thread to go the master



54
55
56
# File 'lib/active_record_slave/active_record_slave.rb', line 54

def self.read_from_master!
  thread_variable_set(:active_record_slave, :master)
end

.read_from_master?Boolean

Whether this thread is currently forcing all reads to go against the master database

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_record_slave/active_record_slave.rb', line 49

def self.read_from_master?
  thread_variable_get(:active_record_slave) == :master
end

.read_from_slave!Object

Subsequent reads on this thread and any fibers called by this thread can go to a slave



59
60
61
# File 'lib/active_record_slave/active_record_slave.rb', line 59

def self.read_from_slave!
  thread_variable_set(:active_record_slave, nil)
end