switchman 1.9.14 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/switchman/shard_internal.rb +1 -1
- data/db/migrate/20130328212039_create_switchman_shards.rb +1 -1
- data/db/migrate/20130328224244_create_default_shard.rb +1 -1
- data/db/migrate/20161206323434_add_back_default_string_limits_switchman.rb +1 -1
- data/lib/switchman/active_record/abstract_adapter.rb +8 -0
- data/lib/switchman/active_record/association.rb +10 -7
- data/lib/switchman/active_record/base.rb +2 -2
- data/lib/switchman/active_record/connection_handler.rb +12 -6
- data/lib/switchman/active_record/log_subscriber.rb +0 -2
- data/lib/switchman/active_record/migration.rb +17 -0
- data/lib/switchman/active_record/postgresql_adapter.rb +5 -3
- data/lib/switchman/active_record/query_cache.rb +39 -15
- data/lib/switchman/active_record/statement_cache.rb +15 -5
- data/lib/switchman/engine.rb +3 -0
- data/lib/switchman/r_spec_helper.rb +4 -2
- data/lib/switchman/version.rb +1 -1
- metadata +13 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9bf082be6cc36e99b6be46bdf90c47102c45962
|
4
|
+
data.tar.gz: 105fd5544f11577cd939de766a0d2950f727759e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a69a13d8922e15c57a972c4521833f0d1658d7c13198f766e1cc2374a3cc8781765984194bb1b57950b32bc2c52221b3aeb8d1e6474e30bde3a4aa30a6d8cd3
|
7
|
+
data.tar.gz: a22298dbbc075b8a69af90bd87c97a74ad598729551f268a7bd3bb9eae08aa28eec6ca1654bf35b2b7863527acd2aab63458a8ad439453d4d86fb5514c0d7447
|
@@ -192,7 +192,7 @@ module Switchman
|
|
192
192
|
max_procs = determine_max_procs(options.delete(:max_procs), parallel)
|
193
193
|
if ::ActiveRecord::Relation === scope
|
194
194
|
# still need a post-uniq, cause the default database server could be NULL or Rails.env in the db
|
195
|
-
database_servers = scope.reorder('database_server_id').select(:database_server_id).
|
195
|
+
database_servers = scope.reorder('database_server_id').select(:database_server_id).distinct.
|
196
196
|
map(&:database_server).compact.uniq
|
197
197
|
parallel = [(max_procs.to_f / database_servers.count).ceil, parallel].min if max_procs
|
198
198
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class AddBackDefaultStringLimitsSwitchman < ActiveRecord::Migration
|
1
|
+
class AddBackDefaultStringLimitsSwitchman < ActiveRecord::Migration[4.2]
|
2
2
|
def up
|
3
3
|
add_string_limit_if_missing :switchman_shards, :name
|
4
4
|
add_string_limit_if_missing :switchman_shards, :database_server_id
|
@@ -38,6 +38,14 @@ module Switchman
|
|
38
38
|
ensure
|
39
39
|
@last_query_at = Time.now
|
40
40
|
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def id_value_for_database(value)
|
45
|
+
return super unless value.class.sharded_primary_key?
|
46
|
+
# do this the Rails 4.2 way, so that if Shard.current != self.shard, the id gets transposed
|
47
|
+
quote(value.id)
|
48
|
+
end
|
41
49
|
end
|
42
50
|
end
|
43
51
|
end
|
@@ -36,14 +36,17 @@ module Switchman
|
|
36
36
|
end
|
37
37
|
|
38
38
|
module CollectionAssociation
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
39
|
+
method = ::Rails.version < '5.1' ? :get_records : :find_target
|
40
|
+
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
41
|
+
def #{method}
|
42
|
+
shards = reflection.options[:multishard] && owner.respond_to?(:associated_shards) ? owner.associated_shards : [shard]
|
43
|
+
# activate both the owner and the target's shard category, so that Reflection#join_id_for,
|
44
|
+
# when called for the owner, will be returned relative to shard the query will execute on
|
45
|
+
Shard.with_each_shard(shards, [klass.shard_category, owner.class.shard_category].uniq) do
|
46
|
+
super
|
47
|
+
end
|
45
48
|
end
|
46
|
-
|
49
|
+
RUBY
|
47
50
|
end
|
48
51
|
|
49
52
|
module BelongsToAssociation
|
@@ -138,7 +138,7 @@ module Switchman
|
|
138
138
|
end
|
139
139
|
|
140
140
|
def hash
|
141
|
-
self.class.sharded_primary_key? ? Shard.global_id_for(id).hash : super
|
141
|
+
self.class.sharded_primary_key? ? self.class.hash ^ Shard.global_id_for(id).hash : super
|
142
142
|
end
|
143
143
|
|
144
144
|
def to_param
|
@@ -156,7 +156,7 @@ module Switchman
|
|
156
156
|
def quoted_id
|
157
157
|
return super unless self.class.sharded_primary_key?
|
158
158
|
# do this the Rails 4.2 way, so that if Shard.current != self.shard, the id gets transposed
|
159
|
-
self.class.connection.quote(id
|
159
|
+
self.class.connection.quote(id)
|
160
160
|
end
|
161
161
|
end
|
162
162
|
end
|
@@ -40,10 +40,15 @@ module Switchman
|
|
40
40
|
Shard.default
|
41
41
|
|
42
42
|
# automatically change config to allow for sharing connections with simple config
|
43
|
-
|
43
|
+
config = ::Rails.version < '5.1' ? spec.config : pool.spec.config
|
44
|
+
ConnectionHandler.make_sharing_automagic(config)
|
44
45
|
ConnectionHandler.make_sharing_automagic(Shard.default.database_server.config)
|
45
46
|
|
46
|
-
::
|
47
|
+
if ::Rails.version < '5.1'
|
48
|
+
::ActiveRecord::Base.configurations[::Rails.env] = spec.instance_variable_get(:@config).stringify_keys
|
49
|
+
else
|
50
|
+
::ActiveRecord::Base.configurations[::Rails.env] = config.stringify_keys
|
51
|
+
end
|
47
52
|
end
|
48
53
|
|
49
54
|
@shard_connection_pools ||= { [:master, Shard.default.database_server.shareable? ? ::Rails.env : Shard.default] => pool}
|
@@ -162,12 +167,13 @@ module Switchman
|
|
162
167
|
# A connection was established in an ancestor process that must have
|
163
168
|
# subsequently forked. We can't reuse the connection, but we can copy
|
164
169
|
# the specification and establish a new connection with it.
|
165
|
-
|
166
|
-
|
167
|
-
pool = establish_connection ancestor_pool.default_pool.spec
|
170
|
+
spec = if ancestor_pool.is_a?(ConnectionPoolProxy)
|
171
|
+
ancestor_pool.default_pool.spec
|
168
172
|
else
|
169
|
-
|
173
|
+
ancestor_pool.spec
|
170
174
|
end
|
175
|
+
spec = spec.to_hash if ::Rails.version >= '5.1'
|
176
|
+
pool = establish_connection spec
|
171
177
|
pool.instance_variable_set(:@schema_cache, ancestor_pool.schema_cache) if ancestor_pool.schema_cache
|
172
178
|
pool
|
173
179
|
elsif spec_name != "primary"
|
@@ -21,8 +21,6 @@ module Switchman
|
|
21
21
|
binds = " " + payload[:binds].map { |col,v|
|
22
22
|
render_bind(col, v)
|
23
23
|
}.inspect
|
24
|
-
elsif ::Rails.version < '5.0.3'
|
25
|
-
binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
|
26
24
|
else
|
27
25
|
casted_params = type_casted_binds(payload[:binds], payload[:type_casted_binds])
|
28
26
|
binds = " " + payload[:binds].zip(casted_params).map { |attr, value|
|
@@ -1,6 +1,23 @@
|
|
1
1
|
module Switchman
|
2
2
|
module ActiveRecord
|
3
3
|
module Migration
|
4
|
+
module Compatibility
|
5
|
+
module V5_0
|
6
|
+
def create_table(*args, **options)
|
7
|
+
unless options.key?(:id)
|
8
|
+
options[:id] = :bigserial
|
9
|
+
end
|
10
|
+
if block_given?
|
11
|
+
super do |td|
|
12
|
+
yield td
|
13
|
+
end
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
4
21
|
def connection
|
5
22
|
conn = super
|
6
23
|
if conn.shard != ::ActiveRecord::Base.connection_pool.current_pool.shard
|
@@ -1,8 +1,10 @@
|
|
1
1
|
module Switchman
|
2
2
|
module ActiveRecord
|
3
3
|
module PostgreSQLAdapter
|
4
|
-
|
5
|
-
|
4
|
+
if ::Rails.version < '5'
|
5
|
+
def self.prepended(klass)
|
6
|
+
klass::NATIVE_DATABASE_TYPES[:primary_key] = "bigserial primary key".freeze
|
7
|
+
end
|
6
8
|
end
|
7
9
|
|
8
10
|
# copy/paste; use quote_local_table_name
|
@@ -137,7 +139,7 @@ module Switchman
|
|
137
139
|
end.compact
|
138
140
|
end
|
139
141
|
|
140
|
-
def index_name_exists?(table_name, index_name,
|
142
|
+
def index_name_exists?(table_name, index_name, _default = nil)
|
141
143
|
schema = shard.name if use_qualified_names?
|
142
144
|
|
143
145
|
exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
|
@@ -80,22 +80,46 @@ module Switchman
|
|
80
80
|
|
81
81
|
private
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
83
|
+
if ::Rails.version < '5.1'
|
84
|
+
def cache_sql(sql, binds)
|
85
|
+
# have to include the shard id in the cache key because of switching dbs on the same connection
|
86
|
+
sql = "#{self.shard.id}::#{sql}"
|
87
|
+
result =
|
88
|
+
if query_cache[sql].key?(binds)
|
89
|
+
::ActiveSupport::Notifications.instrument("sql.active_record",
|
90
|
+
:sql => sql, :binds => binds, :name => "CACHE", :connection_id => object_id)
|
91
|
+
query_cache[sql][binds]
|
92
|
+
else
|
93
|
+
query_cache[sql][binds] = yield
|
94
|
+
end
|
94
95
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
96
|
+
if ::ActiveRecord::Result === result
|
97
|
+
result.dup
|
98
|
+
else
|
99
|
+
result.collect { |row| row.dup }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
else
|
103
|
+
def cache_sql(sql, name, binds)
|
104
|
+
# have to include the shard id in the cache key because of switching dbs on the same connection
|
105
|
+
sql = "#{self.shard.id}::#{sql}"
|
106
|
+
@lock.synchronize do
|
107
|
+
result =
|
108
|
+
if query_cache[sql].key?(binds)
|
109
|
+
::ActiveSupport::Notifications.instrument(
|
110
|
+
"sql.active_record",
|
111
|
+
sql: sql,
|
112
|
+
binds: binds,
|
113
|
+
name: name,
|
114
|
+
connection_id: object_id,
|
115
|
+
cached: true,
|
116
|
+
)
|
117
|
+
query_cache[sql][binds]
|
118
|
+
else
|
119
|
+
query_cache[sql][binds] = yield
|
120
|
+
end
|
121
|
+
result.dup
|
122
|
+
end
|
99
123
|
end
|
100
124
|
end
|
101
125
|
end
|
@@ -44,12 +44,22 @@ module Switchman
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
if ::Rails.version < '5.1'
|
48
|
+
def generic_query_builder(connection)
|
49
|
+
@query_builder ||= connection.cacheable_query(@arel)
|
50
|
+
end
|
51
|
+
|
52
|
+
def qualified_query_builder(shard, klass)
|
53
|
+
@qualified_query_builders[shard.id] ||= klass.connection.cacheable_query(@arel)
|
54
|
+
end
|
55
|
+
else
|
56
|
+
def generic_query_builder(connection)
|
57
|
+
@query_builder ||= connection.cacheable_query(self.class, @arel)
|
58
|
+
end
|
50
59
|
|
51
|
-
|
52
|
-
|
60
|
+
def qualified_query_builder(shard, klass)
|
61
|
+
@qualified_query_builders[shard.id] ||= klass.connection.cacheable_query(self.class, @arel)
|
62
|
+
end
|
53
63
|
end
|
54
64
|
|
55
65
|
module BindMap
|
data/lib/switchman/engine.rb
CHANGED
@@ -129,6 +129,9 @@ module Switchman
|
|
129
129
|
|
130
130
|
::ActiveRecord::LogSubscriber.prepend(ActiveRecord::LogSubscriber)
|
131
131
|
::ActiveRecord::Migration.prepend(ActiveRecord::Migration)
|
132
|
+
if ::Rails.version >= '5'
|
133
|
+
::ActiveRecord::Migration::Compatibility::V5_0.prepend(ActiveRecord::Migration::Compatibility::V5_0)
|
134
|
+
end
|
132
135
|
::ActiveRecord::Migrator.prepend(ActiveRecord::Migrator)
|
133
136
|
|
134
137
|
::ActiveRecord::Reflection::AbstractReflection.include(ActiveRecord::Reflection::AbstractReflection)
|
@@ -118,7 +118,8 @@ module Switchman
|
|
118
118
|
klass.before do
|
119
119
|
raise "Sharding did not set up correctly" if @@sharding_failed
|
120
120
|
Shard.clear_cache
|
121
|
-
if
|
121
|
+
if ::Rails.version >= '5.1' ? use_transactional_tests :
|
122
|
+
(::Rails.version >= '5' && use_transactional_tests) || use_transactional_fixtures
|
122
123
|
Shard.default(true)
|
123
124
|
@shard1 = Shard.find(@shard1.id)
|
124
125
|
@shard2 = Shard.find(@shard2.id)
|
@@ -134,7 +135,8 @@ module Switchman
|
|
134
135
|
|
135
136
|
klass.after do
|
136
137
|
next if @@sharding_failed
|
137
|
-
if
|
138
|
+
if ::Rails.version >= '5.1' ? use_transactional_tests :
|
139
|
+
(::Rails.version >= '5' && use_transactional_tests) || use_transactional_fixtures
|
138
140
|
shards = [@shard2]
|
139
141
|
shards << @shard1 unless @shard1.database_server == Shard.default.database_server
|
140
142
|
shards.each do |shard|
|
data/lib/switchman/version.rb
CHANGED
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.
|
4
|
+
version: 1.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody Cutrer
|
@@ -19,9 +19,9 @@ dependencies:
|
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '4.2'
|
22
|
-
- - "
|
22
|
+
- - "<"
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version: '5.
|
24
|
+
version: '5.2'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
27
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,9 +29,9 @@ dependencies:
|
|
29
29
|
- - ">="
|
30
30
|
- !ruby/object:Gem::Version
|
31
31
|
version: '4.2'
|
32
|
-
- - "
|
32
|
+
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '5.
|
34
|
+
version: '5.2'
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: activerecord
|
37
37
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,9 +39,9 @@ dependencies:
|
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '4.2'
|
42
|
-
- - "
|
42
|
+
- - "<"
|
43
43
|
- !ruby/object:Gem::Version
|
44
|
-
version: '5.
|
44
|
+
version: '5.2'
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
47
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -49,23 +49,23 @@ dependencies:
|
|
49
49
|
- - ">="
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: '4.2'
|
52
|
-
- - "
|
52
|
+
- - "<"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '5.
|
54
|
+
version: '5.2'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: shackles
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1.3
|
61
|
+
version: '1.3'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 1.3
|
68
|
+
version: '1.3'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: open4
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 2.1
|
89
|
+
version: '2.1'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 2.1
|
96
|
+
version: '2.1'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: byebug
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|