Module: ActiveRecordShards::ConnectionSwitcher

Included in:
ActiveRecord::Base
Defined in:
lib/active_record_shards/connection_switcher.rb

Defined Under Namespace

Classes: MasterSlaveProxy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



5
6
7
8
# File 'lib/active_record_shards/connection_switcher.rb', line 5

def self.extended(klass)
  klass.singleton_class.alias_method_chain :columns, :default_shard
  klass.singleton_class.alias_method_chain :table_exists?, :default_shard
end

Instance Method Details

#connection_pool_nameObject

Name of the connection pool. Used by ConnectionHandler to retrieve the current connection pool.



112
113
114
115
116
117
118
119
120
# File 'lib/active_record_shards/connection_switcher.rb', line 112

def connection_pool_name # :nodoc:
  name = @connection_pool_name_override || current_shard_selection.shard_name(self)

  if configurations[name].nil? && on_slave?
    current_shard_selection.shard_name(self, false)
  else
    name
  end
end

#current_shard_idObject



139
140
141
# File 'lib/active_record_shards/connection_switcher.rb', line 139

def current_shard_id
  current_shard_selection.shard
end

#current_shard_selectionObject



135
136
137
# File 'lib/active_record_shards/connection_switcher.rb', line 135

def current_shard_selection
  Thread.current[:shard_selection] ||= ShardSelection.new
end

#default_shard=(new_default_shard) ⇒ Object



10
11
12
13
# File 'lib/active_record_shards/connection_switcher.rb', line 10

def default_shard=(new_default_shard)
  ActiveRecordShards::ShardSelection.default_shard = new_default_shard
  switch_connection(:shard => new_default_shard)
end

#establish_connection_override(connection_name) ⇒ Object



122
123
124
125
# File 'lib/active_record_shards/connection_switcher.rb', line 122

def establish_connection_override(connection_name)
  @connection_pool_name_override = connection_name
  establish_connection(connection_name)
end

#on_all_shards(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_record_shards/connection_switcher.rb', line 32

def on_all_shards(&block)
  old_options = current_shard_selection.options
  if supports_sharding?
    shard_names.map do |shard|
      switch_connection(:shard => shard)
      yield(shard)
    end
  else
    [yield]
  end
ensure
  switch_connection(old_options)
end

#on_cx_switch_block(which, options = {}, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_record_shards/connection_switcher.rb', line 92

def on_cx_switch_block(which, options = {}, &block)
  old_options = current_shard_selection.options
  switch_to_slave = (which == :slave && (@disallow_slave.nil? || @disallow_slave == 0))
  switch_connection(:slave => switch_to_slave)

  @disallow_slave = (@disallow_slave || 0) + 1 if which == :master

  # we avoid_readonly_scope to prevent some stack overflow problems, like when
  # .columns calls .with_scope which calls .columns and onward, endlessly.
  if self == ActiveRecord::Base || !switch_to_slave || options[:construct_ro_scope] == false
    yield
  else
    readonly.scoping(&block)
  end
ensure
  @disallow_slave -= 1 if which == :master
  switch_connection(old_options)
end

#on_first_shard(&block) ⇒ Object



23
24
25
26
# File 'lib/active_record_shards/connection_switcher.rb', line 23

def on_first_shard(&block)
  shard_name = shard_names.first
  on_shard(shard_name) { yield }
end

#on_master(&block) ⇒ Object



83
84
85
# File 'lib/active_record_shards/connection_switcher.rb', line 83

def on_master(&block)
  on_master_or_slave(:master, &block)
end

#on_master_if(condition, &block) ⇒ Object



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

def on_master_if(condition, &block)
  condition ? on_master(&block) : yield
end

#on_master_or_slave(which, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/active_record_shards/connection_switcher.rb', line 62

def on_master_or_slave(which, &block)
   if block_given?
     on_cx_switch_block(which, &block)
   else
     MasterSlaveProxy.new(self, which)
   end
end

#on_master_unless(condition, &block) ⇒ Object



58
59
60
# File 'lib/active_record_shards/connection_switcher.rb', line 58

def on_master_unless(condition, &block)
  on_master_if(!condition, &block)
end

#on_shard(shard, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/active_record_shards/connection_switcher.rb', line 15

def on_shard(shard, &block)
  old_options = current_shard_selection.options
  switch_connection(:shard => shard) if supports_sharding?
  yield
ensure
  switch_connection(old_options)
end

#on_slave(&block) ⇒ Object Also known as: with_slave

Executes queries using the slave database. Fails over to master if no slave is found. if you want to execute a block of code on the slave you can go:

Account.on_slave do
  Account.first
end

the first account will be found on the slave DB

For one-liners you can simply do

Account.on_slave.first


79
80
81
# File 'lib/active_record_shards/connection_switcher.rb', line 79

def on_slave(&block)
  on_master_or_slave(:slave, &block)
end

#on_slave?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/active_record_shards/connection_switcher.rb', line 131

def on_slave?
  current_shard_selection.on_slave?
end

#on_slave_if(condition, &block) ⇒ Object Also known as: with_slave_if



46
47
48
# File 'lib/active_record_shards/connection_switcher.rb', line 46

def on_slave_if(condition, &block)
  condition ? on_slave(&block) : yield
end

#on_slave_unless(condition, &block) ⇒ Object Also known as: with_slave_unless



50
51
52
# File 'lib/active_record_shards/connection_switcher.rb', line 50

def on_slave_unless(condition, &block)
  on_slave_if(!condition, &block)
end

#shard_namesObject



143
144
145
146
147
148
# File 'lib/active_record_shards/connection_switcher.rb', line 143

def shard_names
  unless config = configurations[shard_env]
    raise "Did not find #{shard_env} in configurations, did you forget to add it to your database.yml ? (configurations: #{configurations.inspect})"
  end
  config['shard_names'] || []
end

#shardsObject



28
29
30
# File 'lib/active_record_shards/connection_switcher.rb', line 28

def shards
  ShardSupport.new(self == ActiveRecord::Base ? nil : where(nil))
end

#supports_sharding?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/active_record_shards/connection_switcher.rb', line 127

def supports_sharding?
  shard_names.any?
end