active_record_host_pool 2.2.0 → 3.1.0
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b012c288bdd269561992eb0a5a53310f7424f18102dae61655f97bc654bf146b
|
4
|
+
data.tar.gz: 9f3bdbda2e96929770dd6f9ad33293182d460e68a961a0a89370b3e2ca30a77d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5540b9126209ea0a458ae8a5d3d96267cb449193a53d8798f34257b0e90dc8178faa2ef14e10477f4131f3619d7cf4406d1083f45b87ebe178b5b3c2ae1f72d
|
7
|
+
data.tar.gz: d314aca9726a5393a51921c9727df1caf688a0350abc6db0341cbc1b5d4dec5d5cec222384bd71ddb4f64fddd81f6da108fc6e2071cd016da2a48c104fd1b354
|
data/Changelog.md
CHANGED
@@ -6,7 +6,21 @@ and as of v1.0.0 this project adheres to [Semantic Versioning](https://semver.or
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [3.1.0]
|
10
|
+
|
11
|
+
### Added
|
12
|
+
- Calls `#clean!` on the connection after switching databases.
|
13
|
+
|
14
|
+
## [3.0.0]
|
15
|
+
|
16
|
+
### Added
|
17
|
+
- Support and testing for Rails 7.2 & Rails main.
|
18
|
+
|
19
|
+
### Removed
|
20
|
+
- Support for ActiveRecord's legacy connection handling.
|
21
|
+
|
9
22
|
## [2.2.0]
|
23
|
+
|
10
24
|
### Removed
|
11
25
|
- Support for Ruby 3.0.
|
12
26
|
|
@@ -8,7 +8,7 @@ when :trilogy
|
|
8
8
|
when "6.1", "7.0"
|
9
9
|
require "trilogy_adapter/connection"
|
10
10
|
ActiveRecord::Base.extend(TrilogyAdapter::Connection)
|
11
|
-
when "7.1"
|
11
|
+
when "7.1", "7.2", "8.0"
|
12
12
|
require "active_record/connection_adapters/trilogy_adapter"
|
13
13
|
else
|
14
14
|
raise "Unsupported version of Rails (v#{ActiveRecord::VERSION::STRING})"
|
@@ -17,29 +17,29 @@ end
|
|
17
17
|
|
18
18
|
module ActiveRecordHostPool
|
19
19
|
module DatabaseSwitch
|
20
|
-
attr_reader :
|
20
|
+
attr_reader :_host_pool_desired_database
|
21
21
|
def initialize(*)
|
22
22
|
@_cached_current_database = nil
|
23
23
|
super
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
@
|
28
|
-
@config[:database] =
|
26
|
+
def _host_pool_desired_database=(database)
|
27
|
+
@_host_pool_desired_database = database
|
28
|
+
@config[:database] = _host_pool_desired_database
|
29
29
|
end
|
30
30
|
|
31
31
|
if ActiveRecord.version >= Gem::Version.new("7.1") || ActiveRecordHostPool.loaded_db_adapter == :trilogy
|
32
32
|
# Patch `raw_execute` instead of `execute` since this commit:
|
33
33
|
# https://github.com/rails/rails/commit/f69bbcbc0752ca5d5af327d55922614a26f5c7e9
|
34
34
|
def raw_execute(...)
|
35
|
-
if
|
35
|
+
if _host_pool_desired_database && !_no_switch
|
36
36
|
_switch_connection
|
37
37
|
end
|
38
38
|
super
|
39
39
|
end
|
40
40
|
else
|
41
41
|
def execute(...)
|
42
|
-
if
|
42
|
+
if _host_pool_desired_database && !_no_switch
|
43
43
|
_switch_connection
|
44
44
|
end
|
45
45
|
super
|
@@ -71,30 +71,50 @@ module ActiveRecordHostPool
|
|
71
71
|
attr_accessor :_no_switch
|
72
72
|
|
73
73
|
def _switch_connection
|
74
|
-
if
|
74
|
+
if _host_pool_desired_database &&
|
75
75
|
(
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
log("select_db #{
|
76
|
+
_desired_database_changed? ||
|
77
|
+
_real_connection_changed?
|
78
|
+
)
|
79
|
+
log("select_db #{_host_pool_desired_database}", "SQL") do
|
80
80
|
clear_cache!
|
81
|
-
raw_connection.select_db(
|
81
|
+
raw_connection.select_db(_host_pool_desired_database)
|
82
|
+
clean! if respond_to?(:clean)
|
82
83
|
end
|
83
|
-
@_cached_current_database =
|
84
|
-
@_cached_connection_object_id =
|
84
|
+
@_cached_current_database = _host_pool_desired_database
|
85
|
+
@_cached_connection_object_id = _real_connection_object_id
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|
89
|
+
def _desired_database_changed?
|
90
|
+
_host_pool_desired_database != @_cached_current_database
|
91
|
+
end
|
92
|
+
|
93
|
+
# rubocop:disable Lint/DuplicateMethods
|
94
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
95
|
+
when "6.1", "7.0", "7.1"
|
96
|
+
def _real_connection_object_id
|
97
|
+
@connection.object_id
|
98
|
+
end
|
99
|
+
else
|
100
|
+
def _real_connection_object_id
|
101
|
+
@raw_connection.object_id
|
102
|
+
end
|
103
|
+
end
|
104
|
+
# rubocop:enable Lint/DuplicateMethods
|
105
|
+
|
106
|
+
def _real_connection_changed?
|
107
|
+
_real_connection_object_id != @_cached_connection_object_id
|
108
|
+
end
|
109
|
+
|
88
110
|
# prevent different databases from sharing the same query cache
|
89
111
|
def cache_sql(sql, *args)
|
90
|
-
super(
|
112
|
+
super(_host_pool_desired_database.to_s + "/" + sql, *args)
|
91
113
|
end
|
92
114
|
end
|
93
115
|
|
94
116
|
module PoolConfigPatch
|
95
117
|
def pool
|
96
|
-
ActiveSupport::ForkTracker.check!
|
97
|
-
|
98
118
|
@pool || synchronize { @pool ||= ActiveRecordHostPool::PoolProxy.new(self) }
|
99
119
|
end
|
100
120
|
end
|
@@ -43,17 +43,35 @@ module ActiveRecordHostPool
|
|
43
43
|
|
44
44
|
attr_reader :pool_config
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
46
|
+
# rubocop:disable Lint/DuplicateMethods
|
47
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
48
|
+
when "6.1", "7.0", "7.1"
|
49
|
+
def connection(*args)
|
50
|
+
real_connection = _unproxied_connection(*args)
|
51
|
+
_connection_proxy_for(real_connection, @config[:database])
|
52
|
+
rescue RESCUABLE_DB_ERROR, ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
|
53
|
+
_connection_pools.delete(_pool_key)
|
54
|
+
Kernel.raise
|
55
|
+
end
|
56
|
+
|
57
|
+
def _unproxied_connection(*args)
|
58
|
+
_connection_pool.connection(*args)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
def lease_connection(*args)
|
62
|
+
real_connection = _unproxied_connection(*args)
|
63
|
+
_connection_proxy_for(real_connection, @config[:database])
|
64
|
+
rescue RESCUABLE_DB_ERROR, ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
|
65
|
+
_connection_pools.delete(_pool_key)
|
66
|
+
Kernel.raise
|
67
|
+
end
|
68
|
+
alias_method :connection, :lease_connection
|
53
69
|
|
54
|
-
|
55
|
-
|
70
|
+
def _unproxied_connection(*args)
|
71
|
+
_connection_pool.lease_connection(*args)
|
72
|
+
end
|
56
73
|
end
|
74
|
+
# rubocop:enable Lint/DuplicateMethods
|
57
75
|
|
58
76
|
# by the time we are patched into ActiveRecord, the current thread has already established
|
59
77
|
# a connection. thus we need to patch both connection and checkout/checkin
|
@@ -67,11 +85,48 @@ module ActiveRecordHostPool
|
|
67
85
|
_connection_pool.checkin(cx)
|
68
86
|
end
|
69
87
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
88
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
89
|
+
when "6.1", "7.0", "7.1"
|
90
|
+
def with_connection
|
91
|
+
cx = checkout
|
92
|
+
yield cx
|
93
|
+
ensure
|
94
|
+
checkin cx
|
95
|
+
end
|
96
|
+
else
|
97
|
+
def with_connection(prevent_permanent_checkout: false) # rubocop:disable Lint/DuplicateMethods
|
98
|
+
real_connection_lease = _connection_pool.send(:connection_lease)
|
99
|
+
sticky_was = real_connection_lease.sticky
|
100
|
+
real_connection_lease.sticky = false if prevent_permanent_checkout
|
101
|
+
|
102
|
+
if real_connection_lease.connection
|
103
|
+
begin
|
104
|
+
yield _connection_proxy_for(real_connection_lease.connection, @config[:database])
|
105
|
+
ensure
|
106
|
+
real_connection_lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was
|
107
|
+
end
|
108
|
+
else
|
109
|
+
begin
|
110
|
+
real_connection_lease.connection = _unproxied_connection
|
111
|
+
yield _connection_proxy_for(real_connection_lease.connection, @config[:database])
|
112
|
+
ensure
|
113
|
+
real_connection_lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was
|
114
|
+
_connection_pool.release_connection(real_connection_lease) unless real_connection_lease.sticky
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def active_connection?
|
120
|
+
real_connection_lease = _connection_pool.send(:connection_lease)
|
121
|
+
if real_connection_lease.connection
|
122
|
+
_connection_proxy_for(real_connection_lease.connection, @config[:database])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
alias_method :active_connection, :active_connection?
|
126
|
+
|
127
|
+
def schema_cache
|
128
|
+
@schema_cache ||= ActiveRecord::ConnectionAdapters::BoundSchemaReflection.new(_connection_pool.schema_reflection, self)
|
129
|
+
end
|
75
130
|
end
|
76
131
|
|
77
132
|
def disconnect!
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_host_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Quorning
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2024-
|
14
|
+
date: 2024-09-02 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|