Module: ActiveRecordShards::ConnectionSwitcher

Included in:
ActiveRecord::Base
Defined in:
lib/active_record_shards/connection_switcher.rb,
lib/active_record_shards/connection_switcher-4-0.rb,
lib/active_record_shards/connection_switcher-5-0.rb,
lib/active_record_shards/connection_switcher-5-1.rb

Defined Under Namespace

Classes: MasterSlaveProxy

Constant Summary collapse

SHARD_NAMES_CONFIG_KEY =
'shard_names'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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

def self.extended(base)
  base.singleton_class.send(:alias_method, :columns_without_default_shard, :columns)
  base.singleton_class.send(:alias_method, :columns, :columns_with_default_shard)

  base.singleton_class.send(:alias_method, :table_exists_without_default_shard?, :table_exists?)
  base.singleton_class.send(:alias_method, :table_exists?, :table_exists_with_default_shard?)
end

Instance Method Details

#connection_pool_nameObject

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



4
5
6
7
8
9
10
11
12
# File 'lib/active_record_shards/connection_switcher-4-0.rb', line 4

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

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

#connection_specification_nameObject



3
4
5
6
7
8
9
10
11
# File 'lib/active_record_shards/connection_switcher-5-0.rb', line 3

def connection_specification_name
  name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)

  unless configurations[name] || name == "primary"
    raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in database.yml"
  end

  name
end

#current_shard_idObject



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

def current_shard_id
  current_shard_selection.shard
end

#current_shard_selectionObject



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

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

#default_shard=(new_default_shard) ⇒ Object



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

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

#on_all_shardsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_record_shards/connection_switcher.rb', line 38

def on_all_shards
  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, force: false, construct_ro_scope: nil, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_record_shards/connection_switcher.rb', line 98

def on_cx_switch_block(which, force: false, construct_ro_scope: nil, &block)
  @disallow_slave ||= 0
  @disallow_slave += 1 if which == :master

  switch_to_slave = force || @disallow_slave.zero?
  old_options = current_shard_selection.options

  switch_connection(slave: switch_to_slave)

  # 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 || construct_ro_scope == false
    yield
  else
    readonly.scoping(&block)
  end
ensure
  @disallow_slave -= 1 if which == :master
  switch_connection(old_options) if old_options
end

#on_first_shardObject



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

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

#on_master(&block) ⇒ Object



89
90
91
# File 'lib/active_record_shards/connection_switcher.rb', line 89

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

#on_master_if(condition, &block) ⇒ Object



60
61
62
# File 'lib/active_record_shards/connection_switcher.rb', line 60

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

#on_master_or_slave(which, &block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/active_record_shards/connection_switcher.rb', line 68

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



64
65
66
# File 'lib/active_record_shards/connection_switcher.rb', line 64

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

#on_shard(shard) ⇒ Object



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

def on_shard(shard)
  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


85
86
87
# File 'lib/active_record_shards/connection_switcher.rb', line 85

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

#on_slave?Boolean

Returns:

  • (Boolean)


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

def on_slave?
  current_shard_selection.on_slave?
end

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



52
53
54
# File 'lib/active_record_shards/connection_switcher.rb', line 52

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

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



56
57
58
# File 'lib/active_record_shards/connection_switcher.rb', line 56

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

#shard_namesObject



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

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_CONFIG_KEY] || []
end

#shardsObject



34
35
36
# File 'lib/active_record_shards/connection_switcher.rb', line 34

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

#supports_sharding?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/active_record_shards/connection_switcher.rb', line 119

def supports_sharding?
  shard_names.any?
end