switchman 2.0.5 → 2.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c544d2ed8498a6aecf33823f39b304393a65f213f6a68bf04f4d36610ebe3ac2
4
- data.tar.gz: be685b315997b103dd13376f5223196f1b66a5be490502aac6bc401408899abc
3
+ metadata.gz: 9dc22c0bf7a00cd1591602240e782813882155b744fa682a26ac20201a45c0e6
4
+ data.tar.gz: fe72352d11d0e81e6e216ba028578c5a4b0f8600fb603f2d5100f99dd4d54721
5
5
  SHA512:
6
- metadata.gz: fbdd769d90c831e1bbe6dd39def12775537989346daae7dbbab3103a330b89151e9d1c3b9fe216ab9ade75e3c01ee1b1c91062cc622707c3c2697f7dc3239ea8
7
- data.tar.gz: 27d32eefa842a76debb1a1fc7875407b4df1398a45a71419c7cc13045fd8705ff5e4af724478a9cb854d517433ae8d16420baa13fc8e7459ab1953e626c0c80a
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
- transposed, binds = transpose_single_predicate(predicate, source_shard, target_shard, remove_nonlocal_primary_keys,
250
- binds: binds, dup_binds_on_mutation: dup_binds_on_mutation)
251
- transposed
252
- end
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
- def transpose_single_predicate(predicate,
258
- source_shard,
259
- target_shard,
260
- remove_nonlocal_primary_keys = false,
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
- remove = true if type == :primary &&
300
- remove_nonlocal_primary_keys &&
301
- predicate.left.relation.model == klass &&
302
- predicate.is_a?(::Arel::Nodes::Equality)
303
-
304
- current_source_shard =
305
- if source_shard
306
- source_shard
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
- if ::Rails.version >= "5.2"
314
- new_right_value =
315
- case predicate.right
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
- predicate.right.map {|val| transpose_predicate_value(val, current_source_shard, target_shard, type, remove) }
318
- else
319
- transpose_predicate_value(predicate.right, current_source_shard, target_shard, type, remove)
320
- end
321
- else
322
- new_right_value = case predicate.right
323
- when Array
324
- local_ids = []
325
- predicate.right.each do |value|
326
- local_id = Shard.relative_id_for(value, current_source_shard, target_shard)
327
- next unless local_id
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
- if bind.value.is_a?(::ActiveRecord::StatementCache::Substitute)
350
- bind.value.sharded = true # mark for transposition later
351
- bind.value.primary = true if type == :primary
352
- else
353
- local_id = Shard.relative_id_for(bind.value, current_source_shard, target_shard)
354
- local_id = [] if remove && local_id > Shard::IDS_PER_SHARD
355
- bind.instance_variable_set(:@value, local_id)
356
- bind.instance_variable_set(:@value_for_database, nil)
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
- end
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, predicate.right.class.new(new_right_value, predicate.right.attribute))
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
- return out_predicate, binds
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = "2.0.5"
4
+ VERSION = "2.0.6"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer