Module: ReadFromSlave::ClassMethods

Defined in:
lib/read_from_slave.rb

Constant Summary collapse

@@slave_models =
{}

Instance Method Summary collapse

Instance Method Details

#connection_with_read_from_slaveObject


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/read_from_slave.rb', line 147

def connection_with_read_from_slave
  normal_connection = connection_without_read_from_slave
  if Thread.current[:read_from_slave] && normal_connection.open_transactions == 0
    slaves.each do |slave_name, slave_config|
      if Thread.current[:"with_#{slave_name}_count"].to_i > 0
        Thread.current[:read_from_slave_uses] = slave_name.to_sym  # for testing use
        return slave_connection(slave_config)
      end
    end
    # If we're not in a with_slave block, default to the primary slave
    Thread.current[:read_from_slave_uses] = primary_slave_name  # for testing use
    return slave_connection(primary_slave_config)
  else
    Thread.current[:read_from_slave_uses] = :master
    return normal_connection
  end
end

#connection_with_slave_db_scopeObject


138
139
140
141
142
143
144
145
# File 'lib/read_from_slave.rb', line 138

def connection_with_slave_db_scope
  slaves.each_key do |slave_name|
    if Thread.current[:"with_#{slave_name}_count"].to_i > 0
      return connection_without_slave_db_scope
    end
  end
  connection_without_read_from_slave
end

#establish_slave_connection_for(slave_config) ⇒ Object

Establishes a connection to the slave database that is configured for the database name provided


225
226
227
228
# File 'lib/read_from_slave.rb', line 225

def establish_slave_connection_for(slave_config)
  conn_spec = slave_config_for(slave_config)
  establish_connection(conn_spec) if conn_spec
end

#establish_slave_connectionsObject

Re-establishes connections to all the slave databases that have been used so far. Use this in your PhusionPassenger.on_event(:starting_worker_process) block if required.


234
235
236
237
238
# File 'lib/read_from_slave.rb', line 234

def establish_slave_connections
  @@slave_models.each do |slave_config, model|
    model.establish_slave_connection_for(slave_config)
  end
end

#find_by_sql_with_read_from_slave(*find_args) ⇒ Object


131
132
133
134
135
136
# File 'lib/read_from_slave.rb', line 131

def find_by_sql_with_read_from_slave(*find_args)
  Thread.current[:read_from_slave] = (Thread.current[:read_from_slave] != :reload)
  find_by_sql_without_read_from_slave(*find_args)
ensure
  Thread.current[:read_from_slave] = false
end

#primary_slave_configObject

Returns the first slave defined in database.yml which will be used by default for reads


206
207
208
# File 'lib/read_from_slave.rb', line 206

def primary_slave_config
  slaves.symbolize_keys[primary_slave_name]
end

#primary_slave_nameObject

Returns the first slave defined in database.yml which will be used by default for reads


211
212
213
# File 'lib/read_from_slave.rb', line 211

def primary_slave_name
  :primary_slave
end

#slave_config_for(slave_config) ⇒ Object

Returns the config for the associated slave database for this master, as given in the database.yml file


218
219
220
# File 'lib/read_from_slave.rb', line 218

def slave_config_for(slave_config)
  configurations[slave_config]
end

#slave_connection(slave_config) ⇒ Object

Returns a connection to the slave database, or to the regular database if no slave is configured


168
169
170
171
# File 'lib/read_from_slave.rb', line 168

def slave_connection(slave_config)
  @slave_model ||= {}
  (@slave_model[slave_config] || slave_model(slave_config)).connection_without_read_from_slave
end

#slave_model(slave_config) ⇒ Object

Returns an AR model class that has a connection to the appropriate slave db


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/read_from_slave.rb', line 176

def slave_model(slave_config)
  if slave_config_for(slave_config)
    unless @@slave_models[slave_config]
      slave_model_name = "ReadFromSlaveFor_#{slave_config}"
      @@slave_models[slave_config] = eval %{
        class #{slave_model_name} < ActiveRecord::Base
          self.abstract_class = true
          establish_slave_connection_for('#{slave_config}')
        end
        #{slave_model_name}
      }
    end
    @slave_model[slave_config] = @@slave_models[slave_config]
  else
    @slave_model[slave_config] = self
  end
end

#slavesObject

Returns a hash of the slave databases, as given in the database.yml file


201
202
203
# File 'lib/read_from_slave.rb', line 201

def slaves
  connection_without_read_from_slave.instance_variable_get(:@config)[:slaves] || {}
end