switchman 3.0.18 → 3.0.21

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: 21bdb8885af6932038bdf482531843f9d0157e0c51d806601937248e636ff98a
4
- data.tar.gz: 92f0ae609d94b8d7eeefa909927481964a09305a00ebcc792d8b8079625df226
3
+ metadata.gz: df4352d56024adc191b904c1786c43dff9a685be0c7e82e5813339a107ac6df3
4
+ data.tar.gz: 7f6ce97fdadcf3dff63b64065e94b2c4de5a481b234ec6f36429ff7eb796eccc
5
5
  SHA512:
6
- metadata.gz: 5d399c2f5de126812026515e4e5afd80380995cd8f098a561a3b17467c68caee64367ac60aa34c46c9a0f4550f9b246a459fd107d364e905db0098b665f32e9c
7
- data.tar.gz: '096f236515a0641c11805fc71f43e866301cedfced33d4662222f9a9df08e8dbdecf97dd315273c80d21252ebd75ddb447628d1b6dccff4c94d5f3f782d5d7af'
6
+ metadata.gz: 26b862b5b2d44c3aebf24d2ce469e332ff3af117b75e05559d1343b399a01d36db1a321f6fb2f48e535d3e0f99517c56c0dd549911d47b9ef024e8652b5f54b3
7
+ data.tar.gz: 229c8277f8df802f2f5f29221d66afbaba873ff70b0f97ca0ff9356a99b58d8b1dc08bd6fb07c75800275dfc4ef63ec8296451bdfed1204860fbfc1cd764d291
@@ -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
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'switchman/database_server'
4
+
3
5
  module Switchman
4
6
  module ActiveRecord
5
7
  module Base
@@ -64,6 +66,14 @@ module Switchman
64
66
  current_role != current_role(without_overrides: true)
65
67
  end
66
68
 
69
+ def connected_to_stack
70
+ return super if Thread.current.thread_variable?(:ar_connected_to_stack)
71
+
72
+ ret = super
73
+ DatabaseServer.guard_servers
74
+ ret
75
+ end
76
+
67
77
  # significant change: Allow per-shard roles
68
78
  def current_role(without_overrides: false)
69
79
  return super() if without_overrides
@@ -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] }
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.18'
4
+ VERSION = '3.0.21'
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.18
4
+ version: 3.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer