active_record_host_pool 2.2.0 → 3.0.0
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
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4db6c9a5a94e8f01d14956b6ce9446b73b3411956a2ad8fd6414ad72ca6ea3cc
|
4
|
+
data.tar.gz: 541c9f3b94434e0d21e6ba04d43ef2d299273f10b487c1224d18a5bb85b202c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55b8be16acfb0518e16c428f57fa7269c44b5a339d7c0df435796855150fb8beb514f1e8e484e0197befbe9b6dc8e3e80e4df176654945e791f4420b75e98cd8
|
7
|
+
data.tar.gz: '09648c47d09220fb302318b8a65c5a42e4280704cc1c8edbd7a6fd95ac57e8ff78dcc4a406fedc5d46ffa05c2cd61991d6bc9f9985911d7a626abf1a318121bf'
|
data/Changelog.md
CHANGED
@@ -6,7 +6,16 @@ and as of v1.0.0 this project adheres to [Semantic Versioning](https://semver.or
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [3.0.0]
|
10
|
+
|
11
|
+
### Added
|
12
|
+
- Support and testing for Rails 7.2 & Rails main.
|
13
|
+
|
14
|
+
### Removed
|
15
|
+
- Support for ActiveRecord's legacy connection handling.
|
16
|
+
|
9
17
|
## [2.2.0]
|
18
|
+
|
10
19
|
### Removed
|
11
20
|
- Support for Ruby 3.0.
|
12
21
|
|
@@ -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,49 @@ 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
82
|
end
|
83
|
-
@_cached_current_database =
|
84
|
-
@_cached_connection_object_id =
|
83
|
+
@_cached_current_database = _host_pool_desired_database
|
84
|
+
@_cached_connection_object_id = _real_connection_object_id
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
+
def _desired_database_changed?
|
89
|
+
_host_pool_desired_database != @_cached_current_database
|
90
|
+
end
|
91
|
+
|
92
|
+
# rubocop:disable Lint/DuplicateMethods
|
93
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
94
|
+
when "6.1", "7.0", "7.1"
|
95
|
+
def _real_connection_object_id
|
96
|
+
@connection.object_id
|
97
|
+
end
|
98
|
+
else
|
99
|
+
def _real_connection_object_id
|
100
|
+
@raw_connection.object_id
|
101
|
+
end
|
102
|
+
end
|
103
|
+
# rubocop:enable Lint/DuplicateMethods
|
104
|
+
|
105
|
+
def _real_connection_changed?
|
106
|
+
_real_connection_object_id != @_cached_connection_object_id
|
107
|
+
end
|
108
|
+
|
88
109
|
# prevent different databases from sharing the same query cache
|
89
110
|
def cache_sql(sql, *args)
|
90
|
-
super(
|
111
|
+
super(_host_pool_desired_database.to_s + "/" + sql, *args)
|
91
112
|
end
|
92
113
|
end
|
93
114
|
|
94
115
|
module PoolConfigPatch
|
95
116
|
def pool
|
96
|
-
ActiveSupport::ForkTracker.check!
|
97
|
-
|
98
117
|
@pool || synchronize { @pool ||= ActiveRecordHostPool::PoolProxy.new(self) }
|
99
118
|
end
|
100
119
|
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.0.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-08-26 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activerecord
|