switchman 1.12.3 → 1.12.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
- SHA1:
3
- metadata.gz: 1ede324b26c105f71f0d071eee8afa73297430ae
4
- data.tar.gz: 6dc4b1fd3878baa64ab87827cdab9da20c7693fd
2
+ SHA256:
3
+ metadata.gz: 4aa0193a7496422746f84c2f7f9e97641ba11790a668dc34456719fbc61bec73
4
+ data.tar.gz: d932aa66cddb7c71934cb016d330dd23b77b9580ab7ca3f6fe3db4494e72085f
5
5
  SHA512:
6
- metadata.gz: 8655c6d6342901d739e858f5914aafc2a5b3a875c30a58802128df70630abcb2f09de364a52e43064ad5388368845d8d28c944617a7fd36eaaae7db45cf2e2ae
7
- data.tar.gz: 5631ec070476582189e1ebb53c1822d50c2c72a5ee0dab301d604a18166f166e143e1b13edfee3fc95df015bf5a2e315e4ebc067d77dd6e8e741ffbd1936290a
6
+ metadata.gz: 47ae6fae556db8c38e80989c5676bae68cd358bdbd664be065dbbba07230ca7ef58e30964bc776e18f98db3b889b68233a600700443573588aba4df4dd72a5b7
7
+ data.tar.gz: 6d49615fed9167e04e4a5db8a704e1810df401d69555e25d7477eaaa56e60051b918fe81dff0b870479999fbfcbfe996c5a5090803d2607c38cb8522b39ab2ef
@@ -56,7 +56,7 @@ module Switchman
56
56
 
57
57
  # Now find the actual record, if it exists; rescue the fake default if the table doesn't exist
58
58
  @default = begin
59
- Shard.where(default: true).first || default
59
+ find_cached("default_shard") { Shard.where(default: true).take } || default
60
60
  rescue
61
61
  default
62
62
  end
@@ -111,30 +111,7 @@ module Switchman
111
111
 
112
112
  unless cached_shards.has_key?(id)
113
113
  cached_shards[id] = Shard.default.activate do
114
- # can't simply cache the AR object since Shard has a custom serializer
115
- # that calls this method
116
- attributes = Switchman.cache.fetch(['shard', id].join('/')) do
117
- shard = find_by_id(id)
118
- if shard
119
- shard.attributes
120
- else
121
- :nil
122
- end
123
- end
124
- if attributes == :nil
125
- nil
126
- else
127
- shard = Shard.new
128
- attributes.each do |attr, value|
129
- shard.send(:"#{attr}=", value)
130
- end
131
- shard.clear_changes_information
132
- shard.instance_variable_set(:@new_record, false)
133
- # connection info doesn't exist in database.yml;
134
- # pretend the shard doesn't exist either
135
- shard = nil unless shard.database_server
136
- shard
137
- end
114
+ find_cached(['shard', id]) { find_by(id: id) }
138
115
  end
139
116
  end
140
117
  cached_shards[id]
@@ -540,6 +517,24 @@ module Switchman
540
517
  cached_shards.delete(shard.id)
541
518
  end
542
519
 
520
+ def find_cached(key)
521
+ # can't simply cache the AR object since Shard has a custom serializer
522
+ # that calls this method
523
+ attributes = Switchman.cache.fetch(key) { yield&.attributes }
524
+ return nil unless attributes
525
+
526
+ shard = Shard.new
527
+ attributes.each do |attr, value|
528
+ shard.send(:"#{attr}=", value)
529
+ end
530
+ shard.clear_changes_information
531
+ shard.instance_variable_set(:@new_record, false)
532
+ # connection info doesn't exist in database.yml;
533
+ # pretend the shard doesn't exist either
534
+ shard = nil unless shard.database_server
535
+ shard
536
+ end
537
+
543
538
  def active_shards
544
539
  Thread.current[:active_shards] ||= {}.compare_by_identity
545
540
  end
@@ -673,6 +668,7 @@ module Switchman
673
668
  def clear_cache
674
669
  Shard.default.activate do
675
670
  Switchman.cache.delete(['shard', id].join('/'))
671
+ Switchman.cache.delete("default_shard") if default?
676
672
  end
677
673
  end
678
674
 
@@ -0,0 +1,13 @@
1
+ class AddDefaultShardIndex < ActiveRecord::Migration[4.2]
2
+ def change
3
+ Switchman::Shard.where(default: nil).update_all(default: false)
4
+ change_column_default :switchman_shards, :default, false
5
+ change_column_null :switchman_shards, :default, false
6
+ options = if connection.adapter_name == 'PostgreSQL'
7
+ { unique: true, where: "\"default\"" }
8
+ else
9
+ {}
10
+ end
11
+ add_index :switchman_shards, :default, options
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ class AddTimestampsToShards < ActiveRecord::Migration[4.2]
2
+ def change
3
+ add_timestamps :switchman_shards, null: true
4
+ now = Time.now.utc
5
+ Switchman::Shard.update_all(updated_at: now, created_at: now)
6
+ change_column_null :switchman_shards, :updated_at, false
7
+ change_column_null :switchman_shards, :created_at, false
8
+ end
9
+ end
@@ -3,13 +3,14 @@ require 'switchman/connection_pool_proxy'
3
3
  module Switchman
4
4
  module ActiveRecord
5
5
  module ConnectionHandler
6
- def self.make_sharing_automagic(config)
6
+ def self.make_sharing_automagic(config, shard = Shard.current)
7
7
  key = config[:adapter] == 'postgresql' ? :schema_search_path : :database
8
8
 
9
9
  # only load the shard name from the db if we have to
10
- if config[key] || !config[:shard_name]
10
+ if !config[:shard_name]
11
11
  # we may not be able to connect to this shard yet, cause it might be an empty database server
12
- shard_name = Shard.current.name rescue nil
12
+ shard = shard.call if shard.is_a?(Proc)
13
+ shard_name = shard.name rescue nil
13
14
  return unless shard_name
14
15
 
15
16
  config[:shard_name] ||= shard_name
@@ -84,12 +85,15 @@ module Switchman
84
85
  if Shard.default.is_a?(Shard)
85
86
  DatabaseServer.all.each do |server|
86
87
  next if server == Shard.default.database_server
87
- shard = server.shards.where(:name => nil).first
88
- shard ||= Shard.new(:database_server => server)
89
- shard.activate do
90
- ConnectionHandler.make_sharing_automagic(server.config)
91
- ConnectionHandler.make_sharing_automagic(proxy.current_pool.spec.config)
88
+
89
+ shard = nil
90
+ shard_proc = -> do
91
+ shard ||= server.shards.where(:name => nil).first
92
+ shard ||= Shard.new(:database_server => server)
93
+ shard
92
94
  end
95
+ ConnectionHandler.make_sharing_automagic(server.config, shard_proc)
96
+ ConnectionHandler.make_sharing_automagic(proxy.current_pool.spec.config, shard_proc)
93
97
  end
94
98
  end
95
99
  # we may have established some connections above trying to infer the shard's name.
@@ -36,6 +36,7 @@ module Switchman
36
36
 
37
37
  puts "Setting up sharding for all specs..."
38
38
  Shard.delete_all
39
+ Switchman.cache.delete("default_shard")
39
40
 
40
41
  @@shard1, @@shard2 = TestHelper.recreate_persistent_test_shards
41
42
  @@default_shard = Shard.default
@@ -71,6 +72,7 @@ module Switchman
71
72
  # we'll re-persist in the group's `before :all`; we don't want them to exist
72
73
  # in the db before then
73
74
  Shard.delete_all
75
+ Switchman.cache.delete("default_shard")
74
76
  Shard.default(true)
75
77
  puts "Done!"
76
78
 
@@ -99,6 +101,7 @@ module Switchman
99
101
  dup = @@default_shard.dup
100
102
  dup.id = @@default_shard.id
101
103
  dup.save!
104
+ Switchman.cache.delete("default_shard")
102
105
  Shard.default(true)
103
106
  dup = @@shard1.dup
104
107
  dup.id = @@shard1.id
@@ -147,6 +150,7 @@ module Switchman
147
150
 
148
151
  klass.after(:all) do
149
152
  Shard.delete_all
153
+ Switchman.cache.delete("default_shard")
150
154
  Shard.default(true)
151
155
  end
152
156
  end
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.12.3"
2
+ VERSION = "1.12.4"
3
3
  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: 1.12.3
4
+ version: 1.12.4
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: 2018-08-09 00:00:00.000000000 Z
13
+ date: 2018-08-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -191,7 +191,8 @@ files:
191
191
  - db/migrate/20130328212039_create_switchman_shards.rb
192
192
  - db/migrate/20130328224244_create_default_shard.rb
193
193
  - db/migrate/20161206323434_add_back_default_string_limits_switchman.rb
194
- - db/shard_1708.sqlite3
194
+ - db/migrate/20180828183945_add_default_shard_index.rb
195
+ - db/migrate/20180828192111_add_timestamps_to_shards.rb
195
196
  - lib/switchman.rb
196
197
  - lib/switchman/action_controller/caching.rb
197
198
  - lib/switchman/active_record/abstract_adapter.rb
@@ -258,7 +259,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
258
259
  version: '0'
259
260
  requirements: []
260
261
  rubyforge_project:
261
- rubygems_version: 2.6.10
262
+ rubygems_version: 2.7.6
262
263
  signing_key:
263
264
  specification_version: 4
264
265
  summary: Rails 4 sharding magic
File without changes