active_record_proxy_adapters 0.1.6 → 0.1.8
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/lib/active_record/connection_adapters/postgresql_proxy_adapter.rb +1 -1
- data/lib/active_record_proxy_adapters/active_record_context.rb +18 -0
- data/lib/active_record_proxy_adapters/postgresql_proxy.rb +1 -1
- data/lib/active_record_proxy_adapters/primary_replica_proxy.rb +18 -1
- data/lib/active_record_proxy_adapters/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc6ba1c7e7067f2807918d4afe2433c3bd2f060a28800ef9a6d543d94e592c7d
|
4
|
+
data.tar.gz: 7fd1f37ef4884d0305bdbd8d3e792f52f8fd257c0353f51392c3a9f308e88313
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6800509cbe6c11b7ff220691659880b80eb2cb7ad9d3644ec2a2853cd2197ab3648dcc57642c7e99b9d8d7d5ee090e4c25087d3f2a307bac3ada30085e576bd
|
7
|
+
data.tar.gz: 210ec622d9a4e60bdf47903b6040719d7adad82f941e9f9c6c5b71c9ecace12a6e0b068fb0b9d7de30c37524236f6a755b24e16c7b81a956b70a15fa761a4a54
|
@@ -15,7 +15,7 @@ module ActiveRecord
|
|
15
15
|
|
16
16
|
ADAPTER_NAME = "PostgreSQLProxy"
|
17
17
|
|
18
|
-
delegate_to_proxy
|
18
|
+
delegate_to_proxy(*ActiveRecordProxyAdapters::ActiveRecordContext.hijackable_methods)
|
19
19
|
|
20
20
|
unless ActiveRecordProxyAdapters::ActiveRecordContext.active_record_v8_0_or_greater?
|
21
21
|
delegate_to_proxy :exec_no_cache, :exec_cache
|
@@ -23,6 +23,8 @@ module ActiveRecordProxyAdapters
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def connection_class_for(connection)
|
26
|
+
return connection.connection_descriptor.name.constantize if active_record_v8_0_2_or_greater?
|
27
|
+
|
26
28
|
connection.connection_class || ActiveRecord::Base
|
27
29
|
end
|
28
30
|
|
@@ -33,6 +35,18 @@ module ActiveRecordProxyAdapters
|
|
33
35
|
ActiveRecord
|
34
36
|
end
|
35
37
|
|
38
|
+
def hijackable_methods
|
39
|
+
hijackable = %i[execute exec_query]
|
40
|
+
|
41
|
+
hijackable << :internal_exec_query if active_record_v8_0_or_greater?
|
42
|
+
|
43
|
+
hijackable
|
44
|
+
end
|
45
|
+
|
46
|
+
def active_record_v7?
|
47
|
+
active_record_version >= Gem::Version.new("7.0") && active_record_version < Gem::Version.new("8.0")
|
48
|
+
end
|
49
|
+
|
36
50
|
def active_record_v7_1_or_greater?
|
37
51
|
active_record_version >= Gem::Version.new("7.1")
|
38
52
|
end
|
@@ -44,5 +58,9 @@ module ActiveRecordProxyAdapters
|
|
44
58
|
def active_record_v8_0_or_greater?
|
45
59
|
active_record_version >= Gem::Version.new("8.0")
|
46
60
|
end
|
61
|
+
|
62
|
+
def active_record_v8_0_2_or_greater?
|
63
|
+
active_record_version >= Gem::Version.new("8.0.2")
|
64
|
+
end
|
47
65
|
end
|
48
66
|
end
|
@@ -7,7 +7,7 @@ module ActiveRecordProxyAdapters
|
|
7
7
|
# Proxy to the original PostgreSQLAdapter, allowing the use of the ActiveRecordProxyAdapters::PrimaryReplicaProxy.
|
8
8
|
class PostgreSQLProxy < PrimaryReplicaProxy
|
9
9
|
# ActiveRecord::PostgreSQLAdapter methods that should be proxied.
|
10
|
-
hijack_method
|
10
|
+
hijack_method(*ActiveRecordContext.hijackable_methods)
|
11
11
|
|
12
12
|
hijack_method :exec_no_cache, :exec_cache unless ActiveRecordContext.active_record_v8_0_or_greater?
|
13
13
|
end
|
@@ -65,6 +65,22 @@ module ActiveRecordProxyAdapters
|
|
65
65
|
delegate :connection_handler, to: :connection_class
|
66
66
|
delegate :reading_role, :writing_role, to: :active_record_context
|
67
67
|
|
68
|
+
# We need to call .verify! to ensure `configure_connection` is called on the instance before attempting to use it.
|
69
|
+
# This is necessary because the connection may have been lazily initialized and is an unintended side effect from a
|
70
|
+
# change in Rails to defer connection verification: https://github.com/rails/rails/pull/44576
|
71
|
+
# verify! cannot be called before the object is initialized and because of how the proxy hooks into the connection
|
72
|
+
# instance, it has to be done lazily (hence the memoization). Ideally, we shouldn't have to worry about this at all
|
73
|
+
# But there is tight coupling between methods in ActiveRecord::ConnectionAdapters::AbstractAdapter and
|
74
|
+
# its descendants which will require significant refactoring to be decoupled.
|
75
|
+
# See https://github.com/rails/rails/issues/51780
|
76
|
+
def verified_primary_connection
|
77
|
+
@verified_primary_connection ||= begin
|
78
|
+
connected_to(role: writing_role) { primary_connection.verify! }
|
79
|
+
|
80
|
+
primary_connection
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
68
84
|
def replica_pool_unavailable?
|
69
85
|
!replica_pool
|
70
86
|
end
|
@@ -135,7 +151,8 @@ module ActiveRecordProxyAdapters
|
|
135
151
|
end
|
136
152
|
|
137
153
|
def connection_for(role, sql_string)
|
138
|
-
connection =
|
154
|
+
connection = verified_primary_connection if role == writing_role || replica_pool_unavailable?
|
155
|
+
|
139
156
|
connection ||= checkout_replica_connection
|
140
157
|
|
141
158
|
result = connected_to(role:) { yield connection }
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_proxy_adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Cruz
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-03-19 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activerecord
|