switchman 3.0.19 → 3.0.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eea092610f18550e6ab0d24baa9a2746e8452341116c0b5e3a94c15b1bcb9be0
4
- data.tar.gz: 53e0c2f7735838283311e57017de6bf020aae61f38dd394821a5bab5fdda869e
3
+ metadata.gz: 1e131a4d625dcfe3ff7854591e717574e3fddf49e29cb9a5aa2e1cc9df8c2f10
4
+ data.tar.gz: 385c067530063b6d4bcdc3f7f0463a57a54082c14be4e00ddf1f520ea5fabb26
5
5
  SHA512:
6
- metadata.gz: da4096ca84676fecda1bee5e99a9e7e34c1d4cc2233e4a32f391d68ccd9d93a8d641b5d4a5ecd330e5df340daca8611bad0226709ba9f9fc6653282821216e0a
7
- data.tar.gz: d8a6f2245ac58e5ab64398245f7b14188cdb4675dea430fc2dd256b5aea73f68a81a30f5bcd3cd626634c492932bf85f3becab5dfa0a6e773abc6639615ab247
6
+ metadata.gz: e6c9d4e232befbbf02b1da406d9bae6fd67731b0942ea3c46d29b87b40f2e457e0f0a03edc14547dd3269d619d0f019beab415150229e8d4a2f395cb3a4e51ea
7
+ data.tar.gz: f6e536576fa2b1f33c2f641ba0dbf0fdb3e487706558efb2bb924af63b8bc939ed45ee491676d5887313d428e64d42cfc13604fc291e561127cd829ced728471
@@ -374,12 +374,12 @@ module Switchman
374
374
  shard || source_shard || Shard.current
375
375
  end
376
376
 
377
- private
378
-
379
377
  def sharding_initialized
380
378
  @sharding_initialized ||= false
381
379
  end
382
380
 
381
+ private
382
+
383
383
  def add_sharded_model(klass)
384
384
  @sharded_models = (sharded_models + [klass]).freeze
385
385
  initialize_sharding
@@ -407,9 +407,13 @@ module Switchman
407
407
 
408
408
  klass.connects_to shards: connects_to_hash
409
409
  end
410
- DatabaseServer.all.each { |db| db.guard! if db.config[:prefer_secondary] } unless @sharding_initialized
411
410
 
411
+ return if @sharding_initialized
412
+
413
+ # If we hadn't initialized sharding yet, the servers won't be guarded
414
+ # The order matters here or guard_servers will be a noop
412
415
  @sharding_initialized = true
416
+ DatabaseServer.guard_servers
413
417
  end
414
418
 
415
419
  # in-process caching
@@ -64,6 +64,17 @@ module Switchman
64
64
  current_role != current_role(without_overrides: true)
65
65
  end
66
66
 
67
+ def connected_to_stack
68
+ ret = super
69
+ # Really early in boot, DatabaseServer may not be loaded yet,
70
+ # which is fine since we will guard when we initialize sharding in that case
71
+ return ret unless const_defined?(:DatabaseServer)
72
+ return ret if Thread.current.thread_variable?(:ar_connected_to_stack)
73
+
74
+ DatabaseServer.guard_servers
75
+ ret
76
+ end
77
+
67
78
  # significant change: Allow per-shard roles
68
79
  def current_role(without_overrides: false)
69
80
  return super() if without_overrides
@@ -49,6 +49,10 @@ module Switchman
49
49
  servers[rand(servers.length)]
50
50
  end
51
51
 
52
+ def guard_servers
53
+ all.each { |db| db.guard! if db.config[:prefer_secondary] } if Shard.sharding_initialized
54
+ end
55
+
52
56
  private
53
57
 
54
58
  def reference_role(role)
@@ -133,6 +137,7 @@ module Switchman
133
137
  # when doing writes (then it falls back to the current
134
138
  # value of GuardRail.environment)
135
139
  def guard!(environment = :secondary)
140
+ DatabaseServer.send(:reference_role, environment)
136
141
  ::ActiveRecord::Base.connected_to_stack << { shard_roles: { id.to_sym => environment }, klasses: [::ActiveRecord::Base] }
137
142
  end
138
143
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = '3.0.19'
4
+ VERSION = '3.0.20'
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: 3.0.19
4
+ version: 3.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer