Module: ConnectionManager::Replication

Defined in:
lib/connection_manager/replication.rb

Instance Method Summary collapse

Instance Method Details

#fetch_replication_method(method_name) ⇒ Object

Get a connection class name from out replication_methods pool.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
# File 'lib/connection_manager/replication.rb', line 52

def fetch_replication_method(method_name)
  available_connections = @replication_methods[method_name]
  raise ArgumentError, "No connections found for #{method_name}." if available_connections.blank?
  available_connections.rotate!
  available_connections[0]
end

#replicant_classesObject



14
15
16
# File 'lib/connection_manager/replication.rb', line 14

def replicant_classes
  @replicant_classes ||= HashWithIndifferentAccess.new
end

#replicated(*connections) ⇒ Object

Builds a class method that returns an ActiveRecord::Relation for use with in ActiveRecord method chaining.

EX: class MyClass < ActiveRecord::Base

replicated :my_readonly_db, "FooConnection", :method_name => 'slaves'
end

end

Options:

  • :name - name of class method to call to access replication, default to slaves

  • :readonly - forces all results to readonly

  • :type - the type of replication; :slave or :master, defaults to :slave



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/connection_manager/replication.rb', line 36

def replicated(*connections)
    @replicated = true
    options = {:name => "slaves"}.merge!(connections.extract_options!)
    options[:type] ||= :slaves
    options[:build_replicants] = true if (options[:build_replicants].blank? && options[:type] == :masters)
    use_database(current_database_name, :table_name => table_name) # make sure the base class has current_database_name set
    connections = connection.replication_keys(options[:type]) if connections.blank?
    set_replications_to_method(connections,options[:name])
    build_repliciation_class_method(options)
    build_replication_association_class(options)
    build_query_method_alias_method(options[:name])
    build_repliciation_instance_method(options[:name])
    options[:name]
end

#replicated?Boolean

Is this class replicated

Returns:

  • (Boolean)


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

def replicated?
  (@replicated == true)
end

#replication_methodsObject

Replication methods (replication_method_name, which is the option for the #replication method) and all their associated connections. The key is the replication_method_name and the value is an array of all the replication_classes the replication_method has access to.

EX: replication_methods => [‘Slave1Connection’,Slave2Connection]



10
11
12
# File 'lib/connection_manager/replication.rb', line 10

def replication_methods
  @replication_methods ||= HashWithIndifferentAccess.new
end