switchman 2.1.0 → 2.1.4

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: a4ef7325514ffe64ab4fd53de062392b9edba2e7089ff35a8bbebfa475338a9d
4
- data.tar.gz: cb74519796fee752ec5dc5e5469e39db240fbf819b38ea8d1ff5ae75c939601a
3
+ metadata.gz: d96c8878af2c919433712e502ab527fd77e811eeb88a652357cc004fb5a40ac7
4
+ data.tar.gz: b8f9e1cb90e7dfbfb25ea02a8699a6c8e20d305f60a13f062a1d71d1da27816e
5
5
  SHA512:
6
- metadata.gz: 6583450d651aa95d06ad5d53a294bb2247ceee6fca42659a666da13378422fdaeeced89a772622de5671812fd7990531f470fd3d630870e6306d53e3e1ebc873
7
- data.tar.gz: d1c21d744f5b926620f32540ca5c5c68b86ba97355b1fb8548ea829ccb05702173228eb40bf25e729a773be210436cfeb369ac2918a8aacea4da63d59b294404
6
+ metadata.gz: bb9130947cbff85fef69fdb3f3ac54c29aa9cf7f95507faf92ac97673828addeebe58c9ff1fc478ff34daa834e2f42774a81a99b5bd42dde7e06e3fd63ff6dca
7
+ data.tar.gz: 1e9fd256868e5b35a0f8d34c5726af34bc7381d76f77f9988d623da84b510959a4ff8563956c797c4921961a8540dd1f8fe74882df9027b4946b93e087309284
@@ -38,11 +38,15 @@ module Switchman
38
38
  def define_method_global_attribute(attr_name)
39
39
  if sharded_column?(attr_name)
40
40
  generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
41
- def __temp__
41
+ def __temp_global_attribute__
42
+ raw_value = original_#{attr_name}
43
+ return nil if raw_value.nil?
44
+ return raw_value if raw_value > Shard::IDS_PER_SHARD
45
+
42
46
  Shard.global_id_for(original_#{attr_name}, shard)
43
47
  end
44
- alias_method 'global_#{attr_name}', :__temp__
45
- undef_method :__temp__
48
+ alias_method 'global_#{attr_name}', :__temp_global_attribute__
49
+ undef_method :__temp_global_attribute__
46
50
  RUBY
47
51
  else
48
52
  define_method_unsharded_column(attr_name, 'global')
@@ -52,11 +56,13 @@ module Switchman
52
56
  def define_method_local_attribute(attr_name)
53
57
  if sharded_column?(attr_name)
54
58
  generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
55
- def __temp__
56
- Shard.local_id_for(original_#{attr_name}).first
59
+ def __temp_local_attribute__
60
+ raw_value = original_#{attr_name}
61
+ return nil if raw_value.nil?
62
+ return raw_value % Shard::IDS_PER_SHARD
57
63
  end
58
- alias_method 'local_#{attr_name}', :__temp__
59
- undef_method :__temp__
64
+ alias_method 'local_#{attr_name}', :__temp_local_attribute__
65
+ undef_method :__temp_local_attribute__
60
66
  RUBY
61
67
  else
62
68
  define_method_unsharded_column(attr_name, 'local')
@@ -94,18 +100,33 @@ module Switchman
94
100
  # rename the original method to original_
95
101
  alias_method 'original_#{attr_name}', '#{attr_name}'
96
102
  # and replace with one that transposes the id
97
- def __temp__
98
- Shard.relative_id_for(original_#{attr_name}, shard, Shard.current(#{shard_category_code_for_reflection(reflection)}))
103
+ def __temp_relative_attribute__
104
+ raw_value = original_#{attr_name}
105
+ return nil if raw_value.nil?
106
+
107
+ abs_raw_value = raw_value.abs
108
+ current_shard = Shard.current(#{shard_category_code_for_reflection(reflection)})
109
+ same_shard = shard == current_shard
110
+ return raw_value if same_shard && abs_raw_value < Shard::IDS_PER_SHARD
111
+
112
+ value_shard_id = abs_raw_value / Shard::IDS_PER_SHARD
113
+ # this is a stupid case when someone stuffed a global id for the current shard in instead
114
+ # of a local id
115
+ return raw_value % Shard::IDS_PER_SHARD if value_shard_id == current_shard.id
116
+ return raw_value if !same_shard && abs_raw_value > Shard::IDS_PER_SHARD
117
+ return shard.global_id_for(raw_value) if !same_shard && abs_raw_value < Shard::IDS_PER_SHARD
118
+
119
+ Shard.relative_id_for(raw_value, shard, current_shard)
99
120
  end
100
- alias_method '#{attr_name}', :__temp__
101
- undef_method :__temp__
121
+ alias_method '#{attr_name}', :__temp_relative_attribute__
122
+ undef_method :__temp_relative_attribute__
102
123
 
103
124
  alias_method 'original_#{attr_name}=', '#{attr_name}='
104
- def __temp__(new_value)
125
+ def __temp_relative_attribute_assignment__(new_value)
105
126
  self.original_#{attr_name} = Shard.relative_id_for(new_value, Shard.current(#{shard_category_code_for_reflection(reflection)}), shard)
106
127
  end
107
- alias_method '#{attr_name}=', :__temp__
108
- undef_method :__temp__
128
+ alias_method '#{attr_name}=', :__temp_relative_attribute_assignment__
129
+ undef_method :__temp_relative_attribute_assignment__
109
130
  RUBY
110
131
  else
111
132
  define_method_unsharded_column(attr_name, 'global')
@@ -115,11 +136,11 @@ module Switchman
115
136
  def define_method_unsharded_column(attr_name, prefix)
116
137
  return if columns_hash["#{prefix}_#{attr_name}"]
117
138
  generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
118
- def __temp__
139
+ def __temp_unsharded_attribute__
119
140
  raise NoMethodError, "undefined method `#{prefix}_#{attr_name}'; are you missing an association?"
120
141
  end
121
- alias_method '#{prefix}_#{attr_name}', :__temp__
122
- undef_method :__temp__
142
+ alias_method '#{prefix}_#{attr_name}', :__temp_unsharded_attribute__
143
+ undef_method :__temp_unsharded_attribute__
123
144
  RUBY
124
145
  end
125
146
  end
@@ -80,17 +80,17 @@ module Switchman
80
80
  end
81
81
  end
82
82
 
83
- def self.included(klass)
84
- klass.extend(ClassMethods)
85
- klass.set_callback(:initialize, :before) do
86
- unless @shard
87
- if self.class.sharded_primary_key?
88
- @shard = Shard.shard_for(self[self.class.primary_key], Shard.current(self.class.shard_category))
89
- else
90
- @shard = Shard.current(self.class.shard_category)
91
- end
92
- end
83
+ def self.prepended(klass)
84
+ klass.singleton_class.prepend(ClassMethods)
85
+ end
86
+
87
+ def _run_initialize_callbacks
88
+ @shard ||= if self.class.sharded_primary_key?
89
+ Shard.shard_for(self[self.class.primary_key], Shard.current(self.class.shard_category))
90
+ else
91
+ Shard.current(self.class.shard_category)
93
92
  end
93
+ super
94
94
  end
95
95
 
96
96
  def shard
@@ -118,7 +118,7 @@ module Switchman
118
118
  next if k == spec_name
119
119
 
120
120
  v = owner_to_pool[k]
121
- owner_to_pool.delete(k) if v.is_a?(ConnectionPoolProxy) && v.spec.name == spec_name
121
+ owner_to_pool.delete(k) if v.is_a?(ConnectionPoolProxy) && v.default_pool.spec.name == spec_name
122
122
  end
123
123
 
124
124
  # unwrap the pool from inside a ConnectionPoolProxy
@@ -154,7 +154,7 @@ module Switchman
154
154
  end
155
155
  end
156
156
  spec = ::ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(
157
- category,
157
+ default_pool.spec.name,
158
158
  config,
159
159
  "#{config[:adapter]}_connection"
160
160
  )
@@ -95,7 +95,7 @@ module Switchman
95
95
 
96
96
  ::StandardError.include(StandardError)
97
97
 
98
- include ActiveRecord::Base
98
+ prepend ActiveRecord::Base
99
99
  include ActiveRecord::AttributeMethods
100
100
  include ActiveRecord::Persistence
101
101
  singleton_class.prepend ActiveRecord::ModelSchema::ClassMethods
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = "2.1.0"
4
+ VERSION = "2.1.4"
5
5
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  - James Williams
9
9
  - Jacob Fugal
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-07-12 00:00:00.000000000 Z
13
+ date: 2021-07-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -257,7 +257,7 @@ homepage: http://www.instructure.com/
257
257
  licenses:
258
258
  - MIT
259
259
  metadata: {}
260
- post_install_message:
260
+ post_install_message:
261
261
  rdoc_options: []
262
262
  require_paths:
263
263
  - lib
@@ -272,8 +272,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
272
272
  - !ruby/object:Gem::Version
273
273
  version: '0'
274
274
  requirements: []
275
- rubygems_version: 3.2.15
276
- signing_key:
275
+ rubygems_version: 3.0.3
276
+ signing_key:
277
277
  specification_version: 4
278
278
  summary: Rails sharding magic
279
279
  test_files: []