switchman 2.0.5 → 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/switchman/active_record/query_methods.rb +72 -119
- data/lib/switchman/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9dc22c0bf7a00cd1591602240e782813882155b744fa682a26ac20201a45c0e6
|
4
|
+
data.tar.gz: fe72352d11d0e81e6e216ba028578c5a4b0f8600fb603f2d5100f99dd4d54721
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2311f1ad0547735b6ffa51b18790564b67db99009b1ca4d0080eec7e881dd5e735e745dc6c78c0a5f26b8ca2ef0bc9afeaa5c9fe6cfac0aa882f5c3cb8c7d82c
|
7
|
+
data.tar.gz: 6b2f5e2cab99c42d5358c464e22a0dd267a2b66108e3e62f252970735d1482618460b37fc730ec4be2bdeb791e33bb6549ebc939ec9714f4fad21a48bcb01108
|
@@ -78,10 +78,6 @@ module Switchman
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
-
def or(other)
|
82
|
-
super(other.shard(self.primary_shard))
|
83
|
-
end
|
84
|
-
|
85
81
|
private
|
86
82
|
|
87
83
|
if ::Rails.version >= '5.2'
|
@@ -246,135 +242,92 @@ module Switchman
|
|
246
242
|
binds: nil,
|
247
243
|
dup_binds_on_mutation: false)
|
248
244
|
result = predicates.map do |predicate|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
result = [result, binds]
|
254
|
-
result
|
255
|
-
end
|
245
|
+
next predicate unless predicate.is_a?(::Arel::Nodes::Binary)
|
246
|
+
next predicate unless predicate.left.is_a?(::Arel::Attributes::Attribute)
|
247
|
+
relation, column = relation_and_column(predicate.left)
|
248
|
+
next predicate unless (type = transposable_attribute_type(relation, column))
|
256
249
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
binds: nil,
|
262
|
-
dup_binds_on_mutation: false)
|
263
|
-
if predicate.is_a?(::Arel::Nodes::Grouping)
|
264
|
-
return predicate, binds unless predicate.expr.is_a?(::Arel::Nodes::Or)
|
265
|
-
# Dang, we have an OR. OK, that means we have other epxressions below this
|
266
|
-
# level, perhaps many, that may need transposition.
|
267
|
-
# the left side and right side must each be treated as predicate lists and
|
268
|
-
# transformed in kind, if neither of them changes we can just return the grouping as is.
|
269
|
-
# hold on, it's about to get recursive...
|
270
|
-
#
|
271
|
-
# TODO: "binds" is getting passed up and down
|
272
|
-
# this stack purely because of the necessary handling for rails <5.2
|
273
|
-
# Dropping support for 5.2 means we can remove the "binds" argument from
|
274
|
-
# all of this and yank the conditional below where we monkey with their instance state.
|
275
|
-
or_expr = predicate.expr
|
276
|
-
left_node = or_expr.left
|
277
|
-
right_node = or_expr.right
|
278
|
-
left_predicates = left_node.children
|
279
|
-
right_predicates = right_node.children
|
280
|
-
new_left_predicates, binds = transpose_predicates(left_predicates, source_shard,
|
281
|
-
target_shard, remove_nonlocal_primary_keys,
|
282
|
-
binds: binds, dup_binds_on_mutation: dup_binds_on_mutation)
|
283
|
-
new_right_predicates, binds = transpose_predicates(right_predicates, source_shard,
|
284
|
-
target_shard, remove_nonlocal_primary_keys,
|
285
|
-
binds: binds, dup_binds_on_mutation: dup_binds_on_mutation)
|
286
|
-
if new_left_predicates != left_predicates
|
287
|
-
left_node.instance_variable_set(:@children, new_left_predicates)
|
288
|
-
end
|
289
|
-
if new_right_predicates != right_predicates
|
290
|
-
right_node.instance_variable_set(:@children, new_right_predicates)
|
291
|
-
end
|
292
|
-
return predicate, binds
|
293
|
-
end
|
294
|
-
return predicate, binds unless predicate.is_a?(::Arel::Nodes::Binary)
|
295
|
-
return predicate, binds unless predicate.left.is_a?(::Arel::Attributes::Attribute)
|
296
|
-
relation, column = relation_and_column(predicate.left)
|
297
|
-
return predicate, binds unless (type = transposable_attribute_type(relation, column))
|
250
|
+
remove = true if type == :primary &&
|
251
|
+
remove_nonlocal_primary_keys &&
|
252
|
+
predicate.left.relation.model == klass &&
|
253
|
+
predicate.is_a?(::Arel::Nodes::Equality)
|
298
254
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
elsif type == :primary
|
308
|
-
Shard.current(klass.shard_category)
|
309
|
-
elsif type == :foreign
|
310
|
-
source_shard_for_foreign_key(relation, column)
|
311
|
-
end
|
255
|
+
current_source_shard =
|
256
|
+
if source_shard
|
257
|
+
source_shard
|
258
|
+
elsif type == :primary
|
259
|
+
Shard.current(klass.shard_category)
|
260
|
+
elsif type == :foreign
|
261
|
+
source_shard_for_foreign_key(relation, column)
|
262
|
+
end
|
312
263
|
|
313
|
-
|
314
|
-
|
315
|
-
|
264
|
+
if ::Rails.version >= "5.2"
|
265
|
+
new_right_value =
|
266
|
+
case predicate.right
|
267
|
+
when Array
|
268
|
+
predicate.right.map {|val| transpose_predicate_value(val, current_source_shard, target_shard, type, remove) }
|
269
|
+
else
|
270
|
+
transpose_predicate_value(predicate.right, current_source_shard, target_shard, type, remove)
|
271
|
+
end
|
272
|
+
else
|
273
|
+
new_right_value = case predicate.right
|
316
274
|
when Array
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
unless remove && local_id > Shard::IDS_PER_SHARD
|
329
|
-
if value.is_a?(::Arel::Nodes::Casted)
|
330
|
-
if local_id == value.val
|
331
|
-
local_id = value
|
332
|
-
elsif local_id != value
|
333
|
-
local_id = value.class.new(local_id, value.attribute)
|
275
|
+
local_ids = []
|
276
|
+
predicate.right.each do |value|
|
277
|
+
local_id = Shard.relative_id_for(value, current_source_shard, target_shard)
|
278
|
+
next unless local_id
|
279
|
+
unless remove && local_id > Shard::IDS_PER_SHARD
|
280
|
+
if value.is_a?(::Arel::Nodes::Casted)
|
281
|
+
if local_id == value.val
|
282
|
+
local_id = value
|
283
|
+
elsif local_id != value
|
284
|
+
local_id = value.class.new(local_id, value.attribute)
|
285
|
+
end
|
334
286
|
end
|
287
|
+
local_ids << local_id
|
335
288
|
end
|
336
|
-
local_ids << local_id
|
337
|
-
end
|
338
|
-
end
|
339
|
-
local_ids
|
340
|
-
when ::Arel::Nodes::BindParam
|
341
|
-
# look for a bind param with a matching column name
|
342
|
-
if binds && bind = binds.detect{|b| b&.name.to_s == predicate.left.name.to_s}
|
343
|
-
# before we mutate, dup
|
344
|
-
if dup_binds_on_mutation
|
345
|
-
binds = binds.map(&:dup)
|
346
|
-
dup_binds_on_mutation = false
|
347
|
-
bind = binds.find { |b| b&.name.to_s == predicate.left.name.to_s }
|
348
289
|
end
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
290
|
+
local_ids
|
291
|
+
when ::Arel::Nodes::BindParam
|
292
|
+
# look for a bind param with a matching column name
|
293
|
+
if binds && bind = binds.detect{|b| b&.name.to_s == predicate.left.name.to_s}
|
294
|
+
# before we mutate, dup
|
295
|
+
if dup_binds_on_mutation
|
296
|
+
binds = binds.map(&:dup)
|
297
|
+
dup_binds_on_mutation = false
|
298
|
+
bind = binds.find { |b| b&.name.to_s == predicate.left.name.to_s }
|
299
|
+
end
|
300
|
+
if bind.value.is_a?(::ActiveRecord::StatementCache::Substitute)
|
301
|
+
bind.value.sharded = true # mark for transposition later
|
302
|
+
bind.value.primary = true if type == :primary
|
303
|
+
else
|
304
|
+
local_id = Shard.relative_id_for(bind.value, current_source_shard, target_shard)
|
305
|
+
local_id = [] if remove && local_id > Shard::IDS_PER_SHARD
|
306
|
+
bind.instance_variable_set(:@value, local_id)
|
307
|
+
bind.instance_variable_set(:@value_for_database, nil)
|
308
|
+
end
|
357
309
|
end
|
310
|
+
predicate.right
|
311
|
+
else
|
312
|
+
local_id = Shard.relative_id_for(predicate.right, current_source_shard, target_shard) || predicate.right
|
313
|
+
local_id = [] if remove && local_id.is_a?(Integer) && local_id > Shard::IDS_PER_SHARD
|
314
|
+
local_id
|
358
315
|
end
|
359
|
-
predicate.right
|
360
|
-
else
|
361
|
-
local_id = Shard.relative_id_for(predicate.right, current_source_shard, target_shard) || predicate.right
|
362
|
-
local_id = [] if remove && local_id.is_a?(Integer) && local_id > Shard::IDS_PER_SHARD
|
363
|
-
local_id
|
364
316
|
end
|
365
|
-
|
366
|
-
out_predicate = if new_right_value == predicate.right
|
367
|
-
predicate
|
368
|
-
elsif predicate.right.is_a?(::Arel::Nodes::Casted)
|
369
|
-
if new_right_value == predicate.right.val
|
317
|
+
if new_right_value == predicate.right
|
370
318
|
predicate
|
319
|
+
elsif predicate.right.is_a?(::Arel::Nodes::Casted)
|
320
|
+
if new_right_value == predicate.right.val
|
321
|
+
predicate
|
322
|
+
else
|
323
|
+
predicate.class.new(predicate.left, predicate.right.class.new(new_right_value, predicate.right.attribute))
|
324
|
+
end
|
371
325
|
else
|
372
|
-
predicate.class.new(predicate.left,
|
326
|
+
predicate.class.new(predicate.left, new_right_value)
|
373
327
|
end
|
374
|
-
else
|
375
|
-
predicate.class.new(predicate.left, new_right_value)
|
376
328
|
end
|
377
|
-
|
329
|
+
result = [result, binds]
|
330
|
+
result
|
378
331
|
end
|
379
332
|
|
380
333
|
def transpose_predicate_value(value, current_shard, target_shard, attribute_type, remove_non_local_ids)
|
data/lib/switchman/version.rb
CHANGED