active_record_shards 3.15.2 → 3.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,278 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordShards
4
+ module DefaultReplicaPatches
5
+ def self.wrap_method_in_on_replica(class_method, base, method, force_on_replica: false)
6
+ base_methods =
7
+ if class_method
8
+ base.methods + base.private_methods
9
+ else
10
+ base.instance_methods + base.private_instance_methods
11
+ end
12
+
13
+ return unless base_methods.include?(method)
14
+
15
+ _, method, punctuation = method.to_s.match(/^(.*?)([\?\!]?)$/).to_a
16
+ # _ALWAYS_ on replica, or only for on `on_replica_by_default = true` models?
17
+ wrapper = force_on_replica ? 'force_on_replica' : 'on_replica_unless_tx'
18
+ base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
19
+ #{class_method ? 'class << self' : ''}
20
+ def #{method}_with_default_replica#{punctuation}(*args, &block)
21
+ #{wrapper} do
22
+ #{method}_without_default_replica#{punctuation}(*args, &block)
23
+ end
24
+ end
25
+
26
+ alias_method :#{method}_without_default_replica#{punctuation}, :#{method}#{punctuation}
27
+ alias_method :#{method}#{punctuation}, :#{method}_with_default_replica#{punctuation}
28
+ #{class_method ? 'end' : ''}
29
+ RUBY
30
+ end
31
+
32
+ def self.wrap_method_in_on_slave(*args)
33
+ ActiveRecordShards::Deprecation.deprecation_warning(
34
+ :'self.wrap_method_in_on_slave',
35
+ :'self.wrap_method_in_on_replica'
36
+ )
37
+ wrap_method_in_on_replica(*args)
38
+ end
39
+
40
+ def transaction_with_replica_off(*args, &block)
41
+ if on_replica_by_default?
42
+ begin
43
+ old_val = Thread.current[:_active_record_shards_in_tx]
44
+ Thread.current[:_active_record_shards_in_tx] = true
45
+ transaction_without_replica_off(*args, &block)
46
+ ensure
47
+ Thread.current[:_active_record_shards_in_tx] = old_val
48
+ end
49
+ else
50
+ transaction_without_replica_off(*args, &block)
51
+ end
52
+ end
53
+ alias_method :transaction_with_slave_off, :transaction_with_replica_off
54
+
55
+ module InstanceMethods
56
+ # fix ActiveRecord to do the right thing, and use our aliased quote_value
57
+ def quote_value(*args, &block)
58
+ self.class.quote_value(*args, &block)
59
+ end
60
+
61
+ def on_replica_unless_tx
62
+ self.class.on_replica_unless_tx { yield }
63
+ end
64
+ end
65
+
66
+ CLASS_REPLICA_METHODS = [
67
+ :calculate,
68
+ :count_by_sql,
69
+ :exists?,
70
+ :find,
71
+ :find_by,
72
+ :find_by_sql,
73
+ :find_every,
74
+ :find_one,
75
+ :find_some,
76
+ :get_primary_key
77
+ ].freeze
78
+
79
+ CLASS_FORCE_REPLICA_METHODS = [
80
+ :replace_bind_variable,
81
+ :replace_bind_variables,
82
+ :sanitize_sql_array,
83
+ :sanitize_sql_hash_for_assignment,
84
+ :table_exists?
85
+ ].freeze
86
+
87
+ CLASS_SLAVE_METHODS = CLASS_REPLICA_METHODS
88
+ CLASS_FORCE_SLAVE_METHODS = CLASS_FORCE_REPLICA_METHODS
89
+
90
+ def self.extended(base)
91
+ CLASS_REPLICA_METHODS.each { |m| ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, m) }
92
+ CLASS_FORCE_REPLICA_METHODS.each { |m| ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, m, force_on_replica: true) }
93
+
94
+ if ActiveRecord::VERSION::MAJOR >= 5
95
+ ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, :load_schema!, force_on_replica: true)
96
+ else
97
+ ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, :columns, force_on_replica: true)
98
+ end
99
+
100
+ ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(false, base, :reload)
101
+
102
+ base.class_eval do
103
+ include InstanceMethods
104
+
105
+ class << self
106
+ alias_method :transaction_without_replica_off, :transaction
107
+ alias_method :transaction, :transaction_with_replica_off
108
+ end
109
+ end
110
+ end
111
+
112
+ def on_replica_unless_tx(&block)
113
+ return yield if Thread.current[:_active_record_shards_in_migration]
114
+ return yield if Thread.current[:_active_record_shards_in_tx]
115
+
116
+ if on_replica_by_default?
117
+ on_replica(&block)
118
+ else
119
+ yield
120
+ end
121
+ end
122
+ alias_method :on_slave_unless_tx, :on_replica_unless_tx
123
+
124
+ def force_on_replica(&block)
125
+ return yield if Thread.current[:_active_record_shards_in_migration]
126
+
127
+ on_cx_switch_block(:replica, construct_ro_scope: false, force: true, &block)
128
+ end
129
+
130
+ module ActiveRelationPatches
131
+ def self.included(base)
132
+ [:calculate, :exists?, :pluck, :load].each do |m|
133
+ ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(false, base, m)
134
+ end
135
+
136
+ if ActiveRecord::VERSION::MAJOR == 4
137
+ # `where` and `having` clauses call `create_binds`, which will use the primary connection
138
+ ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(false, base, :create_binds, force_on_replica: true)
139
+ end
140
+
141
+ ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(false, base, :to_sql, force_on_replica: true)
142
+ end
143
+
144
+ def on_replica_unless_tx
145
+ @klass.on_replica_unless_tx { yield }
146
+ end
147
+ end
148
+
149
+ module Rails52RelationPatches
150
+ def connection
151
+ return super if Thread.current[:_active_record_shards_in_migration]
152
+ return super if Thread.current[:_active_record_shards_in_tx]
153
+
154
+ if @klass.on_replica_by_default?
155
+ @klass.on_replica.connection
156
+ else
157
+ super
158
+ end
159
+ end
160
+ end
161
+
162
+ # in rails 4.1+, they create a join class that's used to pull in records for HABTM.
163
+ # this simplifies the hell out of our existence, because all we have to do is inerit on-replica-by-default
164
+ # down from the parent now.
165
+ module Rails41HasAndBelongsToManyBuilderExtension
166
+ def self.included(base)
167
+ base.class_eval do
168
+ alias_method :through_model_without_inherit_default_replica_from_lhs, :through_model
169
+ alias_method :through_model, :through_model_with_inherit_default_replica_from_lhs
170
+ end
171
+ end
172
+
173
+ def through_model_with_inherit_default_replica_from_lhs
174
+ model = through_model_without_inherit_default_replica_from_lhs
175
+ def model.on_replica_by_default?
176
+ left_reflection.klass.on_replica_by_default?
177
+ end
178
+
179
+ # also transfer the sharded-ness of the left table to the join model
180
+ model.not_sharded unless model.left_reflection.klass.is_sharded?
181
+ model
182
+ end
183
+ end
184
+
185
+ module AssociationsAssociationAssociationScopePatch
186
+ def association_scope
187
+ if klass
188
+ on_replica_unless_tx { super }
189
+ else
190
+ super
191
+ end
192
+ end
193
+
194
+ def on_replica_unless_tx
195
+ klass.on_replica_unless_tx { yield }
196
+ end
197
+ end
198
+
199
+ module AssociationsAssociationFindTargetPatch
200
+ def find_target
201
+ if klass
202
+ on_replica_unless_tx { super }
203
+ else
204
+ super
205
+ end
206
+ end
207
+
208
+ def on_replica_unless_tx
209
+ klass.on_replica_unless_tx { yield }
210
+ end
211
+ end
212
+
213
+ module AssociationsAssociationGetRecordsPatch
214
+ def get_records # rubocop:disable Naming/AccessorMethodName
215
+ if klass
216
+ on_replica_unless_tx { super }
217
+ else
218
+ super
219
+ end
220
+ end
221
+
222
+ def on_replica_unless_tx
223
+ klass.on_replica_unless_tx { yield }
224
+ end
225
+ end
226
+
227
+ module AssociationsPreloaderAssociationAssociatedRecordsByOwnerPatch
228
+ def associated_records_by_owner(preloader)
229
+ if klass
230
+ on_replica_unless_tx { super }
231
+ else
232
+ super
233
+ end
234
+ end
235
+
236
+ def on_replica_unless_tx
237
+ klass.on_replica_unless_tx { yield }
238
+ end
239
+ end
240
+
241
+ module AssociationsPreloaderAssociationLoadRecordsPatch
242
+ def load_records
243
+ if klass
244
+ on_replica_unless_tx { super }
245
+ else
246
+ super
247
+ end
248
+ end
249
+
250
+ def on_replica_unless_tx
251
+ klass.on_replica_unless_tx { yield }
252
+ end
253
+ end
254
+
255
+ module TypeCasterConnectionConnectionPatch
256
+ def connection
257
+ return super if Thread.current[:_active_record_shards_in_migration]
258
+ return super if Thread.current[:_active_record_shards_in_tx]
259
+
260
+ if @klass.on_replica_by_default?
261
+ @klass.on_replica.connection
262
+ else
263
+ super
264
+ end
265
+ end
266
+ end
267
+
268
+ module SchemaDefinePatch
269
+ def define(info, &block)
270
+ old_val = Thread.current[:_active_record_shards_in_migration]
271
+ Thread.current[:_active_record_shards_in_migration] = true
272
+ super
273
+ ensure
274
+ Thread.current[:_active_record_shards_in_migration] = old_val
275
+ end
276
+ end
277
+ end
278
+ end
@@ -1,134 +1,5 @@
1
- # frozen_string_literal: true
2
- module ActiveRecordShards
3
- module DefaultSlavePatches
4
- def self.wrap_method_in_on_slave(class_method, base, method)
5
- base_methods =
6
- if class_method
7
- base.methods + base.private_methods
8
- else
9
- base.instance_methods + base.private_instance_methods
10
- end
11
-
12
- return unless base_methods.include?(method)
13
- _, method, punctuation = method.to_s.match(/^(.*?)([\?\!]?)$/).to_a
14
- base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
- #{class_method ? 'class << self' : ''}
16
- def #{method}_with_default_slave#{punctuation}(*args, &block)
17
- on_slave_unless_tx do
18
- #{method}_without_default_slave#{punctuation}(*args, &block)
19
- end
20
- end
21
-
22
- alias_method :#{method}_without_default_slave#{punctuation}, :#{method}#{punctuation}
23
- alias_method :#{method}#{punctuation}, :#{method}_with_default_slave#{punctuation}
24
- #{class_method ? 'end' : ''}
25
- RUBY
26
- end
27
-
28
- def columns_with_force_slave(*args, &block)
29
- on_cx_switch_block(:slave, construct_ro_scope: false, force: true) do
30
- columns_without_force_slave(*args, &block)
31
- end
32
- end
33
-
34
- def table_exists_with_force_slave?(*args, &block)
35
- on_cx_switch_block(:slave, construct_ro_scope: false, force: true) do
36
- table_exists_without_force_slave?(*args, &block)
37
- end
38
- end
39
-
40
- def transaction_with_slave_off(*args, &block)
41
- if on_slave_by_default?
42
- begin
43
- old_val = Thread.current[:_active_record_shards_slave_off]
44
- Thread.current[:_active_record_shards_slave_off] = true
45
- transaction_without_slave_off(*args, &block)
46
- ensure
47
- Thread.current[:_active_record_shards_slave_off] = old_val
48
- end
49
- else
50
- transaction_without_slave_off(*args, &block)
51
- end
52
- end
53
-
54
- module InstanceMethods
55
- # fix ActiveRecord to do the right thing, and use our aliased quote_value
56
- def quote_value(*args, &block)
57
- self.class.quote_value(*args, &block)
58
- end
59
- end
60
-
61
- CLASS_SLAVE_METHODS = [:find_by_sql, :count_by_sql, :calculate, :find_one, :find_some, :find_every, :exists?].freeze
62
-
63
- def self.extended(base)
64
- CLASS_SLAVE_METHODS.each { |m| ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(true, base, m) }
65
-
66
- base.class_eval do
67
- include InstanceMethods
1
+ ActiveRecordShards::Deprecation.warn('`DefaultSlavePatches` is deprecated, please use `DefaultReplicaPatches`.')
68
2
 
69
- class << self
70
- alias_method :columns_without_force_slave, :columns
71
- alias_method :columns, :columns_with_force_slave
72
-
73
- alias_method :table_exists_without_force_slave?, :table_exists?
74
- alias_method :table_exists?, :table_exists_with_force_slave?
75
-
76
- alias_method :transaction_without_slave_off, :transaction
77
- alias_method :transaction, :transaction_with_slave_off
78
- end
79
- end
80
- if ActiveRecord::Associations.const_defined?(:HasAndBelongsToManyAssociation)
81
- ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_sql)
82
- ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_find_options!)
83
- end
84
- end
85
-
86
- def on_slave_unless_tx
87
- if on_slave_by_default? && !Thread.current[:_active_record_shards_slave_off]
88
- on_slave { yield }
89
- else
90
- yield
91
- end
92
- end
93
-
94
- module ActiveRelationPatches
95
- def self.included(base)
96
- [:calculate, :exists?, :pluck, :load].each do |m|
97
- ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, m)
98
- end
99
-
100
- if ActiveRecord::VERSION::MAJOR == 4
101
- # `where` and `having` clauses call `create_binds`, which will use the master connection
102
- ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :create_binds)
103
- end
104
- end
105
-
106
- def on_slave_unless_tx
107
- @klass.on_slave_unless_tx { yield }
108
- end
109
- end
110
-
111
- # in rails 4.1+, they create a join class that's used to pull in records for HABTM.
112
- # this simplifies the hell out of our existence, because all we have to do is inerit on-slave-by-default
113
- # down from the parent now.
114
- module Rails41HasAndBelongsToManyBuilderExtension
115
- def self.included(base)
116
- base.class_eval do
117
- alias_method :through_model_without_inherit_default_slave_from_lhs, :through_model
118
- alias_method :through_model, :through_model_with_inherit_default_slave_from_lhs
119
- end
120
- end
121
-
122
- def through_model_with_inherit_default_slave_from_lhs
123
- model = through_model_without_inherit_default_slave_from_lhs
124
- def model.on_slave_by_default?
125
- left_reflection.klass.on_slave_by_default?
126
- end
127
-
128
- # also transfer the sharded-ness of the left table to the join model
129
- model.not_sharded unless model.left_reflection.klass.is_sharded?
130
- model
131
- end
132
- end
133
- end
3
+ module ActiveRecordShards
4
+ DefaultSlavePatches = DefaultReplicaPatches
134
5
  end
@@ -0,0 +1,12 @@
1
+ module ActiveRecordShards
2
+ class Deprecation < ActiveSupport::Deprecation
3
+ # This allows us to define separate deprecation behavior for ActiveRecordShards, but defaults to
4
+ # the same behavior globally configured with ActiveSupport.
5
+ #
6
+ # For example, this allows us to silence our own deprecation warnings in test while still being
7
+ # able to fail tests for upstream deprecation warnings.
8
+ def behavior
9
+ @behavior ||= ActiveSupport::Deprecation.behavior
10
+ end
11
+ end
12
+ end
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ActiveRecord
3
4
  class Migrator
4
5
  def self.shards_migration_context
5
- if ActiveRecord::VERSION::STRING >= '5.2.0'
6
- ActiveRecord::MigrationContext.new(['db/migrate'])
6
+ if ActiveRecord::VERSION::MAJOR >= 6
7
+ ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::SchemaMigration)
8
+ elsif ActiveRecord::VERSION::STRING >= '5.2.0'
9
+ ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths)
7
10
  else
8
11
  self
9
12
  end
@@ -6,6 +6,7 @@ module ActiveRecordShards
6
6
  if self != ActiveRecord::Base && self != base_class
7
7
  raise "You should only call not_sharded on direct descendants of ActiveRecord::Base"
8
8
  end
9
+
9
10
  self.sharded = false
10
11
  end
11
12
 
@@ -23,34 +24,38 @@ module ActiveRecordShards
23
24
  end
24
25
  end
25
26
 
26
- def on_slave_by_default?
27
+ def on_replica_by_default?
27
28
  if self == ActiveRecord::Base
28
29
  false
29
30
  else
30
31
  base = base_class
31
- if base.instance_variable_defined?(:@on_slave_by_default)
32
- base.instance_variable_get(:@on_slave_by_default)
32
+ if base.instance_variable_defined?(:@on_replica_by_default)
33
+ base.instance_variable_get(:@on_replica_by_default)
33
34
  end
34
35
  end
35
36
  end
37
+ alias_method :on_slave_by_default?, :on_replica_by_default?
36
38
 
37
- def on_slave_by_default=(value)
39
+ def on_replica_by_default=(value)
38
40
  if self == ActiveRecord::Base
39
- raise ArgumentError, "Cannot set on_slave_by_default on ActiveRecord::Base"
41
+ raise ArgumentError, "Cannot set on_replica_by_default on ActiveRecord::Base"
40
42
  else
41
- base_class.instance_variable_set(:@on_slave_by_default, value)
43
+ base_class.instance_variable_set(:@on_replica_by_default, value)
42
44
  end
43
45
  end
46
+ alias_method :on_slave_by_default=, :on_replica_by_default=
44
47
 
45
48
  module InstanceMethods
46
- def initialize_shard_and_slave
47
- @from_slave = !!self.class.current_shard_selection.options[:slave]
49
+ def initialize_shard_and_replica
50
+ @from_replica = !!self.class.current_shard_selection.options[:replica]
48
51
  @from_shard = self.class.current_shard_selection.options[:shard]
49
52
  end
53
+ alias_method :initialize_shard_and_slave, :initialize_shard_and_replica
50
54
 
51
- def from_slave?
52
- @from_slave
55
+ def from_replica?
56
+ @from_replica
53
57
  end
58
+ alias_method :from_slave?, :from_replica?
54
59
 
55
60
  def from_shard
56
61
  @from_shard
@@ -59,7 +64,7 @@ module ActiveRecordShards
59
64
 
60
65
  def self.extended(base)
61
66
  base.send(:include, InstanceMethods)
62
- base.after_initialize :initialize_shard_and_slave
67
+ base.after_initialize :initialize_shard_and_replica
63
68
  end
64
69
 
65
70
  private