active_record_host_pool 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/active_record_host_pool/connection_adapter_mixin.rb +5 -4
- data/lib/active_record_host_pool/pool_proxy.rb +36 -7
- data/test/database.yml +10 -0
- data/test/helper.rb +1 -0
- data/test/test_arhp.rb +25 -0
- metadata +122 -104
@@ -1,7 +1,8 @@
|
|
1
|
-
|
2
|
-
begin
|
3
|
-
|
4
|
-
rescue LoadError
|
1
|
+
['mysql_adapter', 'mysql2_adapter'].each do |adapter|
|
2
|
+
begin
|
3
|
+
require "active_record/connection_adapters/#{adapter}"
|
4
|
+
rescue LoadError
|
5
|
+
end
|
5
6
|
end
|
6
7
|
|
7
8
|
module ActiveRecordHostPool
|
@@ -27,10 +27,20 @@ module ActiveRecordHostPool
|
|
27
27
|
@config = spec.config.with_indifferent_access
|
28
28
|
end
|
29
29
|
|
30
|
+
def spec
|
31
|
+
@spec
|
32
|
+
end
|
30
33
|
|
31
34
|
def connection(*args)
|
32
|
-
|
33
|
-
|
35
|
+
begin
|
36
|
+
real_connection = _connection_pool.connection(*args)
|
37
|
+
_connection_proxy_for(real_connection, @config[:database])
|
38
|
+
rescue Exception => e
|
39
|
+
if rescuable_errors.any? { |r| e.is_a?(r) }
|
40
|
+
_connection_pools.delete(_pool_key)
|
41
|
+
end
|
42
|
+
raise(e)
|
43
|
+
end
|
34
44
|
end
|
35
45
|
|
36
46
|
# by the time we are patched into ActiveRecord, the current thread has already established
|
@@ -53,6 +63,8 @@ module ActiveRecordHostPool
|
|
53
63
|
end
|
54
64
|
|
55
65
|
def disconnect!
|
66
|
+
p = _connection_pool(false)
|
67
|
+
return unless p
|
56
68
|
_connection_pool.disconnect!
|
57
69
|
_connection_pool.automatic_reconnect = true if _connection_pool.respond_to?(:automatic_reconnect=)
|
58
70
|
_clear_connection_proxy_cache
|
@@ -64,17 +76,30 @@ module ActiveRecordHostPool
|
|
64
76
|
end
|
65
77
|
|
66
78
|
private
|
79
|
+
def rescuable_errors
|
80
|
+
@rescuable_errors ||= begin
|
81
|
+
e = []
|
82
|
+
if Object.const_defined?("Mysql")
|
83
|
+
e << Mysql::Error
|
84
|
+
end
|
85
|
+
if Object.const_defined?("Mysql2")
|
86
|
+
e << Mysql2::Error
|
87
|
+
end
|
88
|
+
e
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
67
92
|
def _connection_pools
|
68
|
-
@@
|
93
|
+
@@_connection_pools ||= {}
|
69
94
|
end
|
70
95
|
|
71
96
|
def _pool_key
|
72
|
-
[@config[:host], @config[:port], @config[:socket], @config[:username]]
|
97
|
+
[@config[:host], @config[:port], @config[:socket], @config[:username]].map(&:to_s).join("/")
|
73
98
|
end
|
74
99
|
|
75
|
-
def _connection_pool
|
100
|
+
def _connection_pool(auto_create=true)
|
76
101
|
pool = _connection_pools[_pool_key]
|
77
|
-
if pool.nil?
|
102
|
+
if pool.nil? && auto_create
|
78
103
|
pool = _connection_pools[_pool_key] = ActiveRecord::ConnectionAdapters::ConnectionPool.new(@spec)
|
79
104
|
end
|
80
105
|
pool
|
@@ -84,7 +109,11 @@ module ActiveRecordHostPool
|
|
84
109
|
@connection_proxy_cache ||= {}
|
85
110
|
key = [connection, database]
|
86
111
|
|
87
|
-
@connection_proxy_cache[key] ||=
|
112
|
+
@connection_proxy_cache[key] ||= begin
|
113
|
+
cx = ActiveRecordHostPool::ConnectionProxy.new(connection, database)
|
114
|
+
cx.execute('select 1')
|
115
|
+
cx
|
116
|
+
end
|
88
117
|
end
|
89
118
|
|
90
119
|
def _clear_connection_proxy_cache
|
data/test/database.yml
CHANGED
data/test/helper.rb
CHANGED
@@ -34,6 +34,7 @@ class ActiveSupport::TestCase
|
|
34
34
|
private
|
35
35
|
def arhp_create_databases
|
36
36
|
ActiveRecord::Base.configurations.each do |name, conf|
|
37
|
+
next if name =~ /not_there/
|
37
38
|
`echo "drop DATABASE IF EXISTS #{conf['database']}" | mysql --user=#{conf['username']}`
|
38
39
|
`echo "create DATABASE #{conf['database']}" | mysql --user=#{conf['username']}`
|
39
40
|
ActiveRecord::Base.establish_connection(name)
|
data/test/test_arhp.rb
CHANGED
@@ -95,6 +95,31 @@ class ActiveRecordHostPoolTest < ActiveSupport::TestCase
|
|
95
95
|
assert_equal expected_database, current_database(switch_to_klass)
|
96
96
|
end
|
97
97
|
|
98
|
+
# rake db:create uses a pattern where it tries to connect to a non-existant database.
|
99
|
+
# but then we had this left in the connection pool cache.
|
100
|
+
# It's also almost impossible to test at this level, so I'm stubbing out the test
|
101
|
+
def _test_connecting_to_wrong_db_first
|
102
|
+
pools = ActiveRecord::Base.connection_handler.connection_pools
|
103
|
+
if !pools.empty?
|
104
|
+
ActiveRecord::Base.connection_handler.connection_pools.values.first.send(:_connection_pools).clear
|
105
|
+
ActiveRecord::Base.connection_handler.connection_pools.clear
|
106
|
+
end
|
107
|
+
|
108
|
+
begin
|
109
|
+
eval <<-EOC
|
110
|
+
class TestNotThere < ActiveRecord::Base
|
111
|
+
establish_connection("test_host_1_db_not_there")
|
112
|
+
end
|
113
|
+
EOC
|
114
|
+
rescue Exception => e
|
115
|
+
assert e.message =~ /Unknown database/
|
116
|
+
end
|
117
|
+
|
118
|
+
Test1.establish_connection("test_host_1_db_1")
|
119
|
+
# assert it doesn't raise.
|
120
|
+
assert Test1.connection
|
121
|
+
end
|
122
|
+
|
98
123
|
def teardown
|
99
124
|
arhp_drop_databases
|
100
125
|
end
|
metadata
CHANGED
@@ -1,117 +1,137 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_host_pool
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.2
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 6
|
9
|
-
- 1
|
10
|
-
version: 0.6.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Ben Osheroff
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-10-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: activerecord
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
32
22
|
type: :runtime
|
33
|
-
|
34
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
35
31
|
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
36
39
|
prerelease: false
|
37
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
41
|
none: false
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: shoulda
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
46
54
|
type: :development
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: mysql
|
50
55
|
prerelease: false
|
51
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: mysql
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
52
65
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
version: "0"
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
60
70
|
type: :development
|
61
|
-
version_requirements: *id003
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: shoulda
|
64
71
|
prerelease: false
|
65
|
-
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: mysql2
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
66
81
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
segments:
|
72
|
-
- 0
|
73
|
-
version: "0"
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
74
86
|
type: :development
|
75
|
-
version_requirements: *id004
|
76
|
-
- !ruby/object:Gem::Dependency
|
77
|
-
name: mocha
|
78
87
|
prerelease: false
|
79
|
-
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
89
|
none: false
|
81
|
-
requirements:
|
82
|
-
- -
|
83
|
-
- !ruby/object:Gem::Version
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mocha
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
88
102
|
type: :development
|
89
|
-
version_requirements: *id005
|
90
|
-
- !ruby/object:Gem::Dependency
|
91
|
-
name: ruby-debug
|
92
103
|
prerelease: false
|
93
|
-
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: debugger
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
94
113
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
|
99
|
-
segments:
|
100
|
-
- 0
|
101
|
-
version: "0"
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
102
118
|
type: :development
|
103
|
-
|
104
|
-
|
105
|
-
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
description: ''
|
127
|
+
email:
|
106
128
|
- ben@gimbo.net
|
107
129
|
executables: []
|
108
|
-
|
109
130
|
extensions: []
|
110
|
-
|
111
|
-
extra_rdoc_files:
|
131
|
+
extra_rdoc_files:
|
112
132
|
- LICENSE
|
113
133
|
- README.md
|
114
|
-
files:
|
134
|
+
files:
|
115
135
|
- README.md
|
116
136
|
- lib/active_record_host_pool.rb
|
117
137
|
- lib/active_record_host_pool/connection_adapter_mixin.rb
|
@@ -124,38 +144,36 @@ files:
|
|
124
144
|
- test/test_arhp.rb
|
125
145
|
homepage: http://github.com/zendesk/active_record_host_pool
|
126
146
|
licenses: []
|
127
|
-
|
128
147
|
post_install_message:
|
129
148
|
rdoc_options: []
|
130
|
-
|
131
|
-
require_paths:
|
149
|
+
require_paths:
|
132
150
|
- lib
|
133
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
152
|
none: false
|
135
|
-
requirements:
|
136
|
-
- -
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
|
139
|
-
segments:
|
153
|
+
requirements:
|
154
|
+
- - ! '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
segments:
|
140
158
|
- 0
|
141
|
-
|
142
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
hash: 4283844498504978406
|
160
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
161
|
none: false
|
144
|
-
requirements:
|
145
|
-
- -
|
146
|
-
- !ruby/object:Gem::Version
|
147
|
-
|
148
|
-
segments:
|
162
|
+
requirements:
|
163
|
+
- - ! '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
segments:
|
149
167
|
- 0
|
150
|
-
|
168
|
+
hash: 4283844498504978406
|
151
169
|
requirements: []
|
152
|
-
|
153
170
|
rubyforge_project:
|
154
|
-
rubygems_version: 1.8.
|
171
|
+
rubygems_version: 1.8.21
|
155
172
|
signing_key:
|
156
173
|
specification_version: 3
|
157
|
-
summary: Allow ActiveRecord to share a connection to multiple databases on the same
|
158
|
-
|
174
|
+
summary: Allow ActiveRecord to share a connection to multiple databases on the same
|
175
|
+
host
|
176
|
+
test_files:
|
159
177
|
- test/database.yml
|
160
178
|
- test/helper.rb
|
161
179
|
- test/schema.rb
|