active_record_host_pool 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Readme.md +1 -1
- data/lib/active_record_host_pool.rb +1 -0
- data/lib/active_record_host_pool/version.rb +1 -1
- data/test/database.yml +9 -0
- data/test/helper.rb +18 -0
- data/test/schema.rb +9 -1
- data/test/test_arhp.rb +45 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5eb359cbe2626874bf6836ee5b5b1826ccf02fef7b474f81f5eec273435232d0
|
4
|
+
data.tar.gz: 40383e9f54da681dd340b7dba75aac65613a34fd083862bf96a905f766c9f7ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84d8eaafd2a6ebd88a42e89c8a52113b1ae3654123bf6d6735256e4787563862024af288b9755c254d445151c43d35502f52608e468beed6daad309225fb4480
|
7
|
+
data.tar.gz: 9a6493969b78849cea22220f562371e25a8215015ee747c5bf8ca533dfca4db21a637336c127c7abc50e6eee7d31843dd75b14fcbe7f329e461e2a24dc0fbd35
|
data/Readme.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://github.com/zendesk/active_record_host_pool/actions?query=workflow%3ACI)
|
2
2
|
|
3
3
|
# ActiveRecord host pooling
|
4
4
|
|
@@ -4,6 +4,7 @@ require 'active_record'
|
|
4
4
|
require 'active_record/base'
|
5
5
|
require 'active_record/connection_adapters/abstract_adapter'
|
6
6
|
|
7
|
+
require 'active_record_host_pool/clear_query_cache_patch'
|
7
8
|
require 'active_record_host_pool/connection_proxy'
|
8
9
|
require 'active_record_host_pool/pool_proxy'
|
9
10
|
require 'active_record_host_pool/connection_adapter_mixin'
|
data/test/database.yml
CHANGED
@@ -66,3 +66,12 @@ test_host_1_db_not_there:
|
|
66
66
|
password: <%= mysql.password %>
|
67
67
|
host: <%= mysql.host %>
|
68
68
|
reconnect: true
|
69
|
+
|
70
|
+
test_host_1_db_shard:
|
71
|
+
adapter: mysql2
|
72
|
+
encoding: utf8
|
73
|
+
database: arhp_test_1_shard
|
74
|
+
username: <%= mysql.user %>
|
75
|
+
password: <%= mysql.password %>
|
76
|
+
host: <%= mysql.host %>
|
77
|
+
reconnect: true
|
data/test/helper.rb
CHANGED
@@ -25,6 +25,13 @@ module ARHPTestSetup
|
|
25
25
|
return if ARHPTestSetup.const_defined?('Test1')
|
26
26
|
|
27
27
|
eval <<-RUBY
|
28
|
+
# The placement of the Test1Shard class is important so that its
|
29
|
+
# connection will not be the most recent connection established
|
30
|
+
# for test_host_1.
|
31
|
+
class Test1Shard < ::ActiveRecord::Base
|
32
|
+
establish_connection(:test_host_1_db_shard)
|
33
|
+
end
|
34
|
+
|
28
35
|
class Test1 < ActiveRecord::Base
|
29
36
|
self.table_name = "tests"
|
30
37
|
establish_connection(:test_host_1_db_1)
|
@@ -60,4 +67,15 @@ module ARHPTestSetup
|
|
60
67
|
def current_database(klass)
|
61
68
|
klass.connection.select_value('select DATABASE()')
|
62
69
|
end
|
70
|
+
|
71
|
+
# Remove a method from a given module that fixes something.
|
72
|
+
# Execute the passed in block.
|
73
|
+
# Re-add the method back to the module.
|
74
|
+
def without_module_patch(mod, method_name)
|
75
|
+
method_body = mod.instance_method(method_name)
|
76
|
+
mod.remove_method(method_name)
|
77
|
+
yield if block_given?
|
78
|
+
ensure
|
79
|
+
mod.define_method(method_name, method_body)
|
80
|
+
end
|
63
81
|
end
|
data/test/schema.rb
CHANGED
@@ -2,7 +2,15 @@
|
|
2
2
|
|
3
3
|
require_relative 'helper'
|
4
4
|
ActiveRecord::Schema.define(version: 1) do
|
5
|
-
create_table 'tests' do |t|
|
5
|
+
create_table 'tests', force: true do |t|
|
6
6
|
t.string 'val'
|
7
7
|
end
|
8
|
+
|
9
|
+
# Add a table only the shard database will have. Conditional
|
10
|
+
# exists since Phenix loads the schema file for every database.
|
11
|
+
if ActiveRecord::Base.connection.current_database == 'arhp_test_1_shard'
|
12
|
+
create_table 'test1_shards' do |t|
|
13
|
+
t.string 'name'
|
14
|
+
end
|
15
|
+
end
|
8
16
|
end
|
data/test/test_arhp.rb
CHANGED
@@ -19,7 +19,7 @@ class ActiveRecordHostPoolTest < Minitest::Test
|
|
19
19
|
|
20
20
|
# Verify that when we fork, the process doesn't crash
|
21
21
|
pid = Process.fork do
|
22
|
-
if ActiveRecord
|
22
|
+
if ActiveRecord.version >= Gem::Version.new('5.2')
|
23
23
|
assert_equal(false, ActiveRecord::Base.connected?) # New to Rails 5.2
|
24
24
|
else
|
25
25
|
assert_equal(true, ActiveRecord::Base.connected?)
|
@@ -55,6 +55,38 @@ class ActiveRecordHostPoolTest < Minitest::Test
|
|
55
55
|
assert_action_uses_correct_database(:insert, "insert into tests values(NULL, 'foo')")
|
56
56
|
end
|
57
57
|
|
58
|
+
def test_models_with_matching_hosts_and_non_matching_databases_should_share_a_connection
|
59
|
+
simulate_rails_app_active_record_railties
|
60
|
+
assert_equal(Test1.connection.raw_connection, Test1Shard.connection.raw_connection)
|
61
|
+
end
|
62
|
+
|
63
|
+
if ActiveRecord.version >= Gem::Version.new('6.0')
|
64
|
+
def test_models_with_matching_hosts_and_non_matching_databases_issue_exists_without_arhp_patch
|
65
|
+
simulate_rails_app_active_record_railties
|
66
|
+
|
67
|
+
# Remove patch that fixes an issue in Rails 6+ to ensure it still
|
68
|
+
# exists. If this begins to fail then it may mean that Rails has fixed
|
69
|
+
# the issue so that it no longer occurs.
|
70
|
+
without_module_patch(ActiveRecordHostPool::ClearQueryCachePatch, :clear_query_caches_for_current_thread) do
|
71
|
+
exception = assert_raises(ActiveRecord::StatementInvalid) do
|
72
|
+
ActiveRecord::Base.cache { Test1Shard.create! }
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_equal("Mysql2::Error: Table 'arhp_test_2.test1_shards' doesn't exist", exception.message)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_models_with_matching_hosts_and_non_matching_databases_do_not_mix_up_underlying_database
|
80
|
+
simulate_rails_app_active_record_railties
|
81
|
+
|
82
|
+
# ActiveRecord 6.0 introduced a change that surfaced a problematic code
|
83
|
+
# path in active_record_host_pool when clearing caches across connection
|
84
|
+
# handlers which can cause the database to change.
|
85
|
+
# See ActiveRecordHostPool::ClearQueryCachePatch
|
86
|
+
ActiveRecord::Base.cache { Test1Shard.create! }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
58
90
|
def test_connection_returns_a_proxy
|
59
91
|
assert_kind_of ActiveRecordHostPool::ConnectionProxy, Test1.connection
|
60
92
|
end
|
@@ -180,4 +212,16 @@ class ActiveRecordHostPoolTest < Minitest::Test
|
|
180
212
|
assert_equal desired_db, current_database(klass)
|
181
213
|
end
|
182
214
|
end
|
215
|
+
|
216
|
+
def simulate_rails_app_active_record_railties
|
217
|
+
if ActiveRecord.version >= Gem::Version.new('6.0')
|
218
|
+
# Necessary for testing ActiveRecord 6.0 which uses the connection
|
219
|
+
# handlers when clearing query caches across all handlers when
|
220
|
+
# an operation that dirties the cache is involved (e.g. create/insert,
|
221
|
+
# update, delete/destroy, truncate, etc.)
|
222
|
+
ActiveRecord::Base.connection_handlers = {
|
223
|
+
ActiveRecord::Base.writing_role => ActiveRecord::Base.default_connection_handler
|
224
|
+
}
|
225
|
+
end
|
226
|
+
end
|
183
227
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_host_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Quorning
|
8
8
|
- Gabe Martin-Dempesy
|
9
9
|
- Pierre Schambacher
|
10
10
|
- Ben Osheroff
|
11
|
-
autorequire:
|
11
|
+
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2021-02-09 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|
@@ -157,7 +157,7 @@ homepage: https://github.com/zendesk/active_record_host_pool
|
|
157
157
|
licenses:
|
158
158
|
- MIT
|
159
159
|
metadata: {}
|
160
|
-
post_install_message:
|
160
|
+
post_install_message:
|
161
161
|
rdoc_options: []
|
162
162
|
require_paths:
|
163
163
|
- lib
|
@@ -172,8 +172,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
172
|
- !ruby/object:Gem::Version
|
173
173
|
version: '0'
|
174
174
|
requirements: []
|
175
|
-
rubygems_version: 3.
|
176
|
-
signing_key:
|
175
|
+
rubygems_version: 3.2.2
|
176
|
+
signing_key:
|
177
177
|
specification_version: 4
|
178
178
|
summary: Allow ActiveRecord to share a connection to multiple databases on the same
|
179
179
|
host
|