switchman 1.15.0 → 1.15.1

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: 6521b8f2a993bab5cb400bdc0fa741fd6e2391e7efb9801f058f21fe3c75169f
4
- data.tar.gz: e96468030c6d98b3c93730877ba6943d5c925255f59ed4a09c3ddfa0bce7388a
3
+ metadata.gz: 4bfb336093e3167cc3f353d50e6dcb60ec44da9d03fd8b30d1ec51adf570d702
4
+ data.tar.gz: ae40dd725814601f125ef10f7c0ae835abc8ae22c38259ec82abb9bfb4046b95
5
5
  SHA512:
6
- metadata.gz: 32b791ddb7c5198a5fcd96448ee0abe59d011f0c422ba9115df5b44e50a53c8a6f4ac1bea35e2a98db52cbfb5f46cb501dbf30dde123125bfef599f67c1ccd86
7
- data.tar.gz: ca11fa309c8f3c5bd7f9a9466327e95fddcff2342dc15d7732adaecab09911a2f9bfc0b2ed0d90cdd8899bf470d3a776945b86a470b8c84f5845878f9b46df0c
6
+ metadata.gz: a8e0dd6e60c8454eb99ad2035b6dfb95a5596c46dbcecf6b77dd422c7a988730ea63f32a78de5d5e742e9aa3caff0f8ff5576af0f3dac92b6b40677f5ca06205
7
+ data.tar.gz: 810631877453b90f8cbeda22eb180b50e575b3f49f2965006292e20c0f4f35085417f5ac0f4d0ead2c291abbec82c82943332b46c81669b9068e40a947a03c29
@@ -425,6 +425,19 @@ module Switchman
425
425
  end
426
426
  end
427
427
 
428
+ # it's tedious to hold onto this same
429
+ # kind of sign state and transform the
430
+ # result in multiple places, so
431
+ # here we can operate on the absolute value
432
+ # in a provided block and trust the sign will
433
+ # stay as provided. This assumes no consumer
434
+ # will return a nil value from the block.
435
+ def signed_id_operation(input_id)
436
+ sign = input_id < 0 ? -1 : 1
437
+ output = yield input_id.abs
438
+ output * sign
439
+ end
440
+
428
441
  # converts an AR object, integral id, string id, or string short-global-id to a
429
442
  # integral id. nil if it can't be interpreted
430
443
  def integral_id_for(any_id)
@@ -437,12 +450,13 @@ module Switchman
437
450
  case any_id
438
451
  when ::ActiveRecord::Base
439
452
  any_id.id
440
- when /^(\d+)~(\d+)$/
453
+ when /^(\d+)~(-?\d+)$/
441
454
  local_id = $2.to_i
442
- # doesn't make sense to have a double-global id
443
- return nil if local_id > IDS_PER_SHARD
444
- $1.to_i * IDS_PER_SHARD + local_id
445
- when Integer, /^\d+$/
455
+ signed_id_operation(local_id) do |id|
456
+ return nil if id > IDS_PER_SHARD
457
+ $1.to_i * IDS_PER_SHARD + id
458
+ end
459
+ when Integer, /^-?\d+$/
446
460
  any_id.to_i
447
461
  else
448
462
  nil
@@ -457,13 +471,17 @@ module Switchman
457
471
  def local_id_for(any_id)
458
472
  id = integral_id_for(any_id)
459
473
  return NIL_NIL_ID unless id
460
- if id < IDS_PER_SHARD
461
- [id, nil]
462
- elsif shard = lookup(id / IDS_PER_SHARD)
463
- [id % IDS_PER_SHARD, shard]
464
- else
465
- NIL_NIL_ID
474
+ return_shard = nil
475
+ local_id = signed_id_operation(id) do |abs_id|
476
+ if abs_id < IDS_PER_SHARD
477
+ abs_id
478
+ elsif return_shard = lookup(abs_id / IDS_PER_SHARD)
479
+ abs_id % IDS_PER_SHARD
480
+ else
481
+ return NIL_NIL_ID
482
+ end
466
483
  end
484
+ [local_id, return_shard]
467
485
  end
468
486
 
469
487
  # takes an id-ish, and returns an integral id relative to
@@ -494,11 +512,13 @@ module Switchman
494
512
  def global_id_for(any_id, source_shard = nil)
495
513
  id = integral_id_for(any_id)
496
514
  return any_id unless id
497
- if id >= IDS_PER_SHARD
498
- id
499
- else
500
- source_shard ||= Shard.current
501
- source_shard.global_id_for(id)
515
+ signed_id_operation(id) do |abs_id|
516
+ if abs_id >= IDS_PER_SHARD
517
+ abs_id
518
+ else
519
+ source_shard ||= Shard.current
520
+ source_shard.global_id_for(abs_id)
521
+ end
502
522
  end
503
523
  end
504
524
 
@@ -671,7 +691,9 @@ module Switchman
671
691
  # takes an id local to this shard, and returns a global id
672
692
  def global_id_for(local_id)
673
693
  return nil unless local_id
674
- local_id + self.id * IDS_PER_SHARD
694
+ self.class.signed_id_operation(local_id) do |abs_id|
695
+ abs_id + self.id * IDS_PER_SHARD
696
+ end
675
697
  end
676
698
 
677
699
  # skip global_id.hash
@@ -33,5 +33,14 @@ module Switchman
33
33
  ::ActiveRecord::Migrator::MIGRATOR_SALT * shard_name_hash
34
34
  end
35
35
  end
36
+
37
+ module MigrationContext
38
+ def migrations
39
+ return @migrations if instance_variable_defined?(:@migrations)
40
+ migrations_cache = Thread.current[:migrations_cache] ||= {}
41
+ key = Digest::MD5.hexdigest(migration_files.sort.join(','))
42
+ @migrations = migrations_cache[key] ||= super
43
+ end
44
+ end
36
45
  end
37
46
  end
@@ -122,6 +122,7 @@ module Switchman
122
122
  ::ActiveRecord::LogSubscriber.prepend(ActiveRecord::LogSubscriber)
123
123
  ::ActiveRecord::Migration.prepend(ActiveRecord::Migration)
124
124
  ::ActiveRecord::Migration::Compatibility::V5_0.prepend(ActiveRecord::Migration::Compatibility::V5_0)
125
+ ::ActiveRecord::MigrationContext.prepend(ActiveRecord::MigrationContext) if ::Rails.version >= '5.2'
125
126
  ::ActiveRecord::Migrator.prepend(ActiveRecord::Migrator)
126
127
 
127
128
  ::ActiveRecord::Reflection::AbstractReflection.include(ActiveRecord::Reflection::AbstractReflection)
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.15.0"
2
+ VERSION = "1.15.1"
3
3
  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: 1.15.0
4
+ version: 1.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-05-18 00:00:00.000000000 Z
13
+ date: 2020-06-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties