module ActiveReplica
Implementation courtesy of db-charmer. Via Octopus
Constants
- VERSION
Public Class Methods
active_replica()
click to toggle source
# File lib/active_replica.rb, line 25 def self.active_replica ActiveReplica::RuntimeRegistry.active_replica end
add_replica(name, config)
click to toggle source
# File lib/active_replica.rb, line 16 def self.add_replica(name, config) handler = handler_for_config(config) self.connection_handler.add_shard(name, handler) end
handler_for_config(config)
click to toggle source
# File lib/active_replica.rb, line 39 def self.handler_for_config(config) spec = spec_for_config(config) ActiveRecord::ConnectionAdapters::ConnectionHandler.new.tap do |handler| handler.establish_connection(ActiveRecord::Base, spec) end end
replicas()
click to toggle source
# File lib/active_replica.rb, line 21 def self.replicas self.connection_handler.shards end
setup(active_record)
click to toggle source
# File lib/active_replica.rb, line 9 def self.setup(active_record) default_handler = active_record.default_connection_handler active_handler = ActiveReplica::ConnectionHandler.new(default_handler) self.connection_handler = active_handler ActiveRecord::Base.default_connection_handler = active_handler end
spec_for_config(config)
click to toggle source
# File lib/active_replica.rb, line 46 def self.spec_for_config(config) ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new("replica" => config).spec(:replica) end
with_replica(name) { || ... }
click to toggle source
# File lib/active_replica.rb, line 29 def self.with_replica(name) before = ActiveReplica::RuntimeRegistry.active_replica ActiveReplica::RuntimeRegistry.active_replica = name self.connection_handler.with_shard(name) do yield end ensure ActiveReplica::RuntimeRegistry.active_replica = before end