switchman 3.0.17 → 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: 213adc61b124f8ba48e52259c47702cb601fdc9e0ef71c452c1eeee23dc94e46
4
- data.tar.gz: f7982e2e7cb3efce13291382b7fa49f9cb0da87cfd5939581368ac5735ed3e00
3
+ metadata.gz: 1e131a4d625dcfe3ff7854591e717574e3fddf49e29cb9a5aa2e1cc9df8c2f10
4
+ data.tar.gz: 385c067530063b6d4bcdc3f7f0463a57a54082c14be4e00ddf1f520ea5fabb26
5
5
  SHA512:
6
- metadata.gz: e3651c9824e8258c8854e0281ecbc081b42f3fa97524d90eb41ac701f9a5eb0bef53ea77a700c682ffa5913ea71aa7816044cfa408b051f20f4b96b86a4e5307
7
- data.tar.gz: 8fac15d77c092790b6410bf15aaccef4c90243308970eddd29e2c9145281cde67e4d6572439b97966ecc2c5924557c7916aca8665e722530c2ead0d43dbcdab2
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
@@ -71,7 +82,7 @@ module Switchman
71
82
  sharded_role = nil
72
83
  connected_to_stack.reverse_each do |hash|
73
84
  shard_role = hash.dig(:shard_roles, current_shard)
74
- if shard_role && (hash[:klasses].include?(Base) || hash[:klasses].include?(connection_classes))
85
+ if shard_role && (hash[:klasses].include?(::ActiveRecord::Base) || hash[:klasses].include?(connection_classes))
75
86
  sharded_role = shard_role
76
87
  break
77
88
  end
@@ -27,7 +27,9 @@ module Switchman
27
27
  return configs if configs.is_a?(Array)
28
28
 
29
29
  db_configs = configs.flat_map do |env_name, config|
30
- roles = config.keys.select { |k| config[k].is_a?(Hash) }
30
+ # It would be nice to do the auto-fallback that we want here, but we haven't
31
+ # actually done that for years (or maybe ever) and it will be a big lift to get working
32
+ roles = config.keys.select { |k| config[k].is_a?(Hash) || (config[k].is_a?(Array) && config[k].all? { |ck| ck.is_a?(Hash) }) }
31
33
  base_config = config.except(*roles)
32
34
 
33
35
  name = "#{env_name}/primary"
@@ -35,7 +37,7 @@ module Switchman
35
37
  base_db = build_db_config_from_raw_config(env_name, name, base_config)
36
38
  [base_db] + roles.map do |role|
37
39
  build_db_config_from_raw_config(env_name, "#{env_name}/#{role}",
38
- base_config.merge(config[role]))
40
+ base_config.merge(config[role].is_a?(Array) ? config[role].first : config[role]))
39
41
  end
40
42
  end
41
43
 
@@ -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)
@@ -71,7 +75,7 @@ module Switchman
71
75
  if role == 'primary'
72
76
  @database_servers[name] = DatabaseServer.new(config.env_name, config.configuration_hash)
73
77
  else
74
- @database_servers[name].roles << role
78
+ @database_servers[name].roles << role.to_sym
75
79
  end
76
80
  end
77
81
  end
@@ -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.17'
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.17
4
+ version: 3.0.20
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: 2022-04-05 00:00:00.000000000 Z
13
+ date: 2022-04-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord