Module: ActiveRecord::Turntable::Base::ClassMethods

Defined in:
lib/active_record/turntable/base.rb

Instance Method Summary collapse

Instance Method Details

#clear_all_connections!Object



69
70
71
72
73
# File 'lib/active_record/turntable/base.rb', line 69

def clear_all_connections!
  turntable_connections.values.each do |pool|
    pool.disconnect!
  end
end

#current_last_shardObject



94
95
96
# File 'lib/active_record/turntable/base.rb', line 94

def current_last_shard
  turntable_cluster.select_shard(current_sequence) if sequencer_enabled?
end

#current_sequenceObject



90
91
92
# File 'lib/active_record/turntable/base.rb', line 90

def current_sequence
  connection.current_sequence_value(self.sequence_name) if sequencer_enabled?
end

#initialize_clusters!Object



53
54
55
56
57
# File 'lib/active_record/turntable/base.rb', line 53

def initialize_clusters!
  turntable_config[:clusters].each do |name, spec|
    self.turntable_clusters[name] ||= Cluster.new(spec, {})
  end
end

#sequencer(sequence_name, *args) ⇒ Object



75
76
77
78
79
80
# File 'lib/active_record/turntable/base.rb', line 75

def sequencer(sequence_name, *args)
  class_attribute :turntable_sequencer

  self.turntable_sequencer_enabled = true
  self.turntable_sequencer = ActiveRecord::Turntable::Sequencer.build(self, sequence_name, *args)
end

#sequencer_enabled?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/active_record/turntable/base.rb', line 86

def sequencer_enabled?
  turntable_sequencer_enabled
end

#spec_for(config) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/active_record/turntable/base.rb', line 59

def spec_for(config)
  begin
    require "active_record/connection_adapters/#{config['adapter']}_adapter"
  rescue LoadError => e
    raise "Please install the #{config['adapter']} adapter: `gem install activerecord-#{config['adapter']}-adapter` (#{e})"
  end
  adapter_method = "#{config['adapter']}_connection"
  ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config, adapter_method)
end

#turntable(cluster_name, shard_key_name, options = {}) ⇒ Object

Parameters:

  • cluster_name (Symbol)

    cluster name for this class

  • shard_key_name (Symbol)

    shard key attribute name

  • options (Hash) (defaults to: {})


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record/turntable/base.rb', line 29

def turntable(cluster_name, shard_key_name, options = {})
  class_attribute :turntable_shard_key,
                    :turntable_cluster, :turntable_cluster_name

  self.turntable_enabled = true
  self.turntable_cluster_name = cluster_name
  self.turntable_shard_key = shard_key_name
  self.turntable_cluster =
    self.turntable_clusters[cluster_name] ||= Cluster.new(
                                                turntable_config[:clusters][cluster_name],
                                                options
                                              )
  turntable_replace_connection_pool
end

#turntable_enabled?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/active_record/turntable/base.rb', line 82

def turntable_enabled?
  turntable_enabled
end

#turntable_replace_connection_poolObject



45
46
47
48
49
50
51
# File 'lib/active_record/turntable/base.rb', line 45

def turntable_replace_connection_pool
  ch = connection_handler
  cp = ConnectionProxy.new(self, turntable_cluster)
  pp = PoolProxy.new(cp)
  ch.class_to_pool.clear if defined?(ch.class_to_pool)
  ch.send(:class_to_pool)[name] = ch.send(:owner_to_pool)[name] = pp
end

#with_shard(any_shard) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_record/turntable/base.rb', line 98

def with_shard(any_shard)
  shard = case any_shard
          when Numeric
            turntable_cluster.shard_for(any_shard)
          when ActiveRecord::Base
            turntable_cluster.shard_for(any_shard.send(any_shard.turntable_shard_key))
          else
            shard_or_key
          end
  connection.with_shard(shard) { yield }
end