switchman 2.1.2 → 2.1.6

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: 8af8855a0c01eb0bb4b35a8e4efc2e75985ba967e1eec343b8f3b31ccffc3aa2
4
- data.tar.gz: 18f7d47e4ebc386652a39e9d32459caeea44e7fffe2c1751dfebef8b319241e0
3
+ metadata.gz: 32a8870f7b32fb16b98edcd9cd0930c30359df0a71a73ecc63752b256b5ac616
4
+ data.tar.gz: 90a3f31b1b8c5951720c7a45a3418c8448a5e08160939de73d487bfd8c9a7dc3
5
5
  SHA512:
6
- metadata.gz: 9ba8fa917299342a8d4c48048d9bb5454d7d9768a788d32905addffd105f7fffe57b90f977084fcb15c20ff0cdf24adf515d695f839a1bd5c42cd481795d7c0c
7
- data.tar.gz: 3eea73f64b92f8bb826fa4f27b4048ec156b69c0c668386f261f51ee4ffa8b63a554be9d32180cbea3cdd3d066e2ca3801561570534f7a5c25511a8437ee6b93
6
+ metadata.gz: 017200b98b0c53d0004c2632322e63141284716609b50400bf5936bf776aa06a68a648e1ff11df0d2d715e1993e051ea0e103c0356d0658ff121283447a376e7
7
+ data.tar.gz: 50cb3e155dd07d2d5e9171f788abce42b29b39d1f488d75cb97ae75842eda94876c4a9b0af134f1b0e03094bc14358c4b9ccc0677a83ebff741d948b9b496d5e
@@ -129,6 +129,11 @@ module Switchman
129
129
  cached_shards[id]
130
130
  end
131
131
 
132
+ def preload_cache
133
+ cached_shards.reverse_merge!(active_shards.values.index_by(&:id))
134
+ cached_shards.reverse_merge!(all.index_by(&:id))
135
+ end
136
+
132
137
  def clear_cache
133
138
  cached_shards.clear
134
139
  end
@@ -103,8 +103,18 @@ module Switchman
103
103
  def __temp_relative_attribute__
104
104
  raw_value = original_#{attr_name}
105
105
  return nil if raw_value.nil?
106
+
107
+ abs_raw_value = raw_value.abs
106
108
  current_shard = Shard.current(#{shard_category_code_for_reflection(reflection)})
107
- return raw_value if shard == current_shard && raw_value < Shard::IDS_PER_SHARD
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
108
118
 
109
119
  Shard.relative_id_for(raw_value, shard, current_shard)
110
120
  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
@@ -158,7 +158,7 @@ module Switchman
158
158
  end
159
159
 
160
160
  def update_columns(*)
161
- db = Shard.current(self.class.shard_category).database_server
161
+ db = shard.database_server
162
162
  if ::GuardRail.environment != db.guard_rail_environment
163
163
  return db.unguard { super }
164
164
  else
@@ -13,6 +13,15 @@ module Switchman
13
13
  shard.activate(self.class.shard_category) { super }
14
14
  end
15
15
  end
16
+
17
+ def delete
18
+ db = shard.database_server
19
+ if ::GuardRail.environment != db.guard_rail_environment
20
+ return db.unguard { super }
21
+ else
22
+ super
23
+ end
24
+ end
16
25
  end
17
26
  end
18
- end
27
+ end
@@ -102,7 +102,7 @@ module Switchman
102
102
  WHERE i.relkind = 'i'
103
103
  AND d.indisprimary = 'f'
104
104
  AND t.relname = '#{table_name}'
105
- AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = '#{shard.name}' )
105
+ AND t.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = '#{shard.name}' )
106
106
  ORDER BY i.relname
107
107
  SQL
108
108
 
@@ -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.2"
4
+ VERSION = "2.1.6"
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.2
4
+ version: 2.1.6
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-29 00:00:00.000000000 Z
13
+ date: 2021-08-11 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.0.3
276
- signing_key:
275
+ rubygems_version: 3.2.24
276
+ signing_key:
277
277
  specification_version: 4
278
278
  summary: Rails sharding magic
279
279
  test_files: []