Module: ActiveRecordShards::ConnectionSwitcher
- Defined in:
- lib/active_record_shards/connection_switcher.rb,
lib/active_record_shards/connection_switcher-5-1.rb,
lib/active_record_shards/connection_switcher-6-0.rb,
lib/active_record_shards/connection_switcher-6-1.rb,
lib/active_record_shards/connection_switcher-7-0.rb
Defined Under Namespace
Classes: IsolationLevelError, LegacyConnectionHandlingError, PrimaryReplicaProxy
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.extended(base) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/active_record_shards/connection_switcher.rb', line 17
def self.extended(base)
base.singleton_class.send(:alias_method, :load_schema_without_default_shard!, :load_schema!)
base.singleton_class.send(:alias_method, :load_schema!, :load_schema_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_specification_name ⇒ Object
3
4
5
6
7
8
9
10
11
|
# File 'lib/active_record_shards/connection_switcher-5-1.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 your database config. (configurations: #{configurations.to_h.keys.inspect})"
end
name
end
|
#current_shard_id ⇒ Object
142
143
144
|
# File 'lib/active_record_shards/connection_switcher.rb', line 142
def current_shard_id
current_shard_selection.shard
end
|
#current_shard_selection ⇒ Object
137
138
139
140
|
# File 'lib/active_record_shards/connection_switcher.rb', line 137
def current_shard_selection
cs = Thread.current.thread_variable_get(:shard_selection) || ShardSelection.new
Thread.current.thread_variable_set(:shard_selection, cs)
end
|
#disallow_replica ⇒ Object
125
126
127
|
# File 'lib/active_record_shards/connection_switcher.rb', line 125
def disallow_replica
Thread.current[:__active_record_shards__disallow_replica_by_thread] ||= 0
end
|
#disallow_replica=(value) ⇒ Object
121
122
123
|
# File 'lib/active_record_shards/connection_switcher.rb', line 121
def disallow_replica=(value)
Thread.current[:__active_record_shards__disallow_replica_by_thread] = value
end
|
#on_all_shards ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/active_record_shards/connection_switcher.rb', line 46
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/active_record_shards/connection_switcher.rb', line 101
def on_cx_switch_block(which, force: false, construct_ro_scope: nil, &block)
self.disallow_replica += 1 if which == :primary
switch_to_replica = force || disallow_replica.zero?
old_options = current_shard_selection.options
switch_connection(replica: switch_to_replica)
if self == ActiveRecord::Base || !switch_to_replica || construct_ro_scope == false || ActiveRecordShards.disable_replica_readonly_records == true
yield
else
readonly.scoping(&block)
end
ensure
self.disallow_replica -= 1 if which == :primary
switch_connection(old_options) if old_options
end
|
#on_first_shard(&block) ⇒ Object
37
38
39
40
|
# File 'lib/active_record_shards/connection_switcher.rb', line 37
def on_first_shard(&block)
shard_name = shard_names.first
on_shard(shard_name, &block)
end
|
#on_primary(&block) ⇒ Object
97
98
99
|
# File 'lib/active_record_shards/connection_switcher.rb', line 97
def on_primary(&block)
on_primary_or_replica(:primary, &block)
end
|
#on_primary_db(&block) ⇒ Object
25
26
27
|
# File 'lib/active_record_shards/connection_switcher.rb', line 25
def on_primary_db(&block)
on_shard(nil, &block)
end
|
#on_primary_if(condition, &block) ⇒ Object
68
69
70
|
# File 'lib/active_record_shards/connection_switcher.rb', line 68
def on_primary_if(condition, &block)
condition ? on_primary(&block) : yield
end
|
#on_primary_or_replica(which, &block) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/active_record_shards/connection_switcher.rb', line 76
def on_primary_or_replica(which, &block)
if block_given?
on_cx_switch_block(which, &block)
else
PrimaryReplicaProxy.new(self, which)
end
end
|
#on_primary_unless(condition, &block) ⇒ Object
72
73
74
|
# File 'lib/active_record_shards/connection_switcher.rb', line 72
def on_primary_unless(condition, &block)
on_primary_if(!condition, &block)
end
|
#on_replica(&block) ⇒ Object
Executes queries using the replica database. Fails over to primary if no replica is found. if you want to execute a block of code on the replica you can go:
Account.on_replica do
Account.first
end
the first account will be found on the replica DB
For one-liners you can simply do
Account.on_replica.first
93
94
95
|
# File 'lib/active_record_shards/connection_switcher.rb', line 93
def on_replica(&block)
on_primary_or_replica(:replica, &block)
end
|
#on_replica? ⇒ Boolean
133
134
135
|
# File 'lib/active_record_shards/connection_switcher.rb', line 133
def on_replica?
current_shard_selection.on_replica?
end
|
#on_replica_if(condition, &block) ⇒ Object
60
61
62
|
# File 'lib/active_record_shards/connection_switcher.rb', line 60
def on_replica_if(condition, &block)
condition ? on_replica(&block) : yield
end
|
#on_replica_unless(condition, &block) ⇒ Object
64
65
66
|
# File 'lib/active_record_shards/connection_switcher.rb', line 64
def on_replica_unless(condition, &block)
on_replica_if(!condition, &block)
end
|
#on_shard(shard) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/active_record_shards/connection_switcher.rb', line 29
def on_shard(shard)
old_options = current_shard_selection.options
switch_connection(shard: shard) if supports_sharding?
yield
ensure
switch_connection(old_options)
end
|
#shard_names ⇒ Object
146
147
148
|
# File 'lib/active_record_shards/connection_switcher.rb', line 146
def shard_names
config_for_env[SHARD_NAMES_CONFIG_KEY] || []
end
|
#shards ⇒ Object
42
43
44
|
# File 'lib/active_record_shards/connection_switcher.rb', line 42
def shards
ShardSupport.new(self == ActiveRecord::Base ? nil : where(nil))
end
|
#supports_sharding? ⇒ Boolean
129
130
131
|
# File 'lib/active_record_shards/connection_switcher.rb', line 129
def supports_sharding?
shard_names.any?
end
|