active_record_proxy_adapters 0.7.0 → 0.7.1
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_proxy_adapters/configuration.rb +10 -5
- data/lib/active_record_proxy_adapters/database_configuration.rb +9 -21
- data/lib/active_record_proxy_adapters/log_subscriber.rb +1 -5
- data/lib/active_record_proxy_adapters/mixin/configuration.rb +3 -11
- data/lib/active_record_proxy_adapters/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4a4d2396f46900885e67814c350db6d85c447838294152b62a747bdbc3908d5
|
4
|
+
data.tar.gz: 85efcfa7b1f10b8a8b10311d6a0cbeb91972856814124dcf347eb75aabfdac0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 372f71443a70435637770a3eb81e947e23fad2bf353f2ffd42a4af6d6feaea8ebed21e5ab0d8c03948c452e8f4e51695e9f01fb852b9e561cf129df6989f08a2
|
7
|
+
data.tar.gz: 5e1851d10b5b2042f18a4f48f01fc0c8597511e4bd5228535b163e4edcb9100e474e259214e5259e6c8bd4c00a06e8f8a9c7e394b42141680910adf741b8c28f
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ A set of ActiveRecord adapters that leverage Rails native multiple database setu
|
|
6
6
|
|
7
7
|
## Why do I need this?
|
8
8
|
|
9
|
-
Maybe you don't. Rails already provides, since version 6.0, a [Rack middleware](https://guides.rubyonrails.org/active_record_multiple_databases.html#activating-automatic-role-switching) that switches between primary and replica automatically based on the HTTP request (`GET` and `HEAD` requests go the
|
9
|
+
Maybe you don't. Rails already provides, since version 6.0, a [Rack middleware](https://guides.rubyonrails.org/active_record_multiple_databases.html#activating-automatic-role-switching) that switches between primary and replica automatically based on the HTTP request (`GET` and `HEAD` requests go the replica, everything else goes to the primary).
|
10
10
|
|
11
11
|
The caveat is: you are not allowed do any writes in any `GET` or `HEAD` requests (including controller callbacks).
|
12
12
|
Which means, for example, your `devise` callbacks that save user metadata will now crash.
|
@@ -11,7 +11,8 @@ module ActiveRecordProxyAdapters
|
|
11
11
|
class Configuration
|
12
12
|
include SynchronizableConfiguration
|
13
13
|
|
14
|
-
DEFAULT_DATABASE_NAME
|
14
|
+
DEFAULT_DATABASE_NAME = :primary
|
15
|
+
DEFAULT_REPLICA_DATABASE_NAME = :primary_replica
|
15
16
|
|
16
17
|
# @return [Class] The context that is used to store the current request's state.
|
17
18
|
attr_reader :context_store
|
@@ -25,19 +26,19 @@ module ActiveRecordProxyAdapters
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def log_subscriber_primary_prefix=(prefix)
|
28
|
-
default_database_config.
|
29
|
+
default_database_config.log_subscriber_prefix = prefix
|
29
30
|
end
|
30
31
|
|
31
32
|
def log_subscriber_primary_prefix
|
32
|
-
default_database_config.
|
33
|
+
default_database_config.log_subscriber_prefix
|
33
34
|
end
|
34
35
|
|
35
36
|
def log_subscriber_replica_prefix=(prefix)
|
36
|
-
|
37
|
+
default_replica_config.log_subscriber_prefix = prefix
|
37
38
|
end
|
38
39
|
|
39
40
|
def log_subscriber_replica_prefix
|
40
|
-
|
41
|
+
default_replica_config.log_subscriber_prefix
|
41
42
|
end
|
42
43
|
|
43
44
|
def proxy_delay
|
@@ -75,6 +76,10 @@ module ActiveRecordProxyAdapters
|
|
75
76
|
database(DEFAULT_DATABASE_NAME)
|
76
77
|
end
|
77
78
|
|
79
|
+
def default_replica_config
|
80
|
+
database(DEFAULT_REPLICA_DATABASE_NAME)
|
81
|
+
end
|
82
|
+
|
78
83
|
def cache_configuration=(cache_configuration)
|
79
84
|
synchronize_update(:cache_configuration, from: @cache_configuration, to: cache_configuration) do
|
80
85
|
@cache_configuration = cache_configuration
|
@@ -25,18 +25,14 @@ module ActiveRecordProxyAdapters
|
|
25
25
|
# Defaults to CHECKOUT_TIMEOUT. Thread safe.
|
26
26
|
attr_reader :checkout_timeout
|
27
27
|
|
28
|
-
# @return [Proc] Prefix for the log subscriber when the
|
29
|
-
attr_reader :
|
30
|
-
|
31
|
-
# @return [Proc] Prefix for the log subscriber when the replica database is used. Thread safe.
|
32
|
-
attr_reader :log_subscriber_replica_prefix
|
28
|
+
# @return [Proc] Prefix for the log subscriber when the database is used. Thread safe.
|
29
|
+
attr_reader :log_subscriber_prefix
|
33
30
|
|
34
31
|
def initialize
|
35
|
-
@lock
|
36
|
-
self.proxy_delay
|
37
|
-
self.checkout_timeout
|
38
|
-
self.
|
39
|
-
self.log_subscriber_replica_prefix = DEFAULT_PREFIX
|
32
|
+
@lock = Monitor.new
|
33
|
+
self.proxy_delay = PROXY_DELAY
|
34
|
+
self.checkout_timeout = CHECKOUT_TIMEOUT
|
35
|
+
self.log_subscriber_prefix = DEFAULT_PREFIX
|
40
36
|
end
|
41
37
|
|
42
38
|
def proxy_delay=(proxy_delay)
|
@@ -51,19 +47,11 @@ module ActiveRecordProxyAdapters
|
|
51
47
|
end
|
52
48
|
end
|
53
49
|
|
54
|
-
def
|
55
|
-
prefix_proc = prefix.is_a?(Proc) ? prefix : proc { prefix.to_s }
|
56
|
-
|
57
|
-
synchronize_update(:log_subscriber_primary_prefix, from: @log_subscriber_primary_prefix, to: prefix_proc) do
|
58
|
-
@log_subscriber_primary_prefix = prefix_proc
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def log_subscriber_replica_prefix=(prefix)
|
50
|
+
def log_subscriber_prefix=(prefix)
|
63
51
|
prefix_proc = prefix.is_a?(Proc) ? prefix : proc { prefix.to_s }
|
64
52
|
|
65
|
-
synchronize_update(:
|
66
|
-
@
|
53
|
+
synchronize_update(:log_subscriber_prefix, from: @log_subscriber_prefix, to: prefix_proc) do
|
54
|
+
@log_subscriber_prefix = prefix_proc
|
67
55
|
end
|
68
56
|
end
|
69
57
|
|
@@ -25,11 +25,7 @@ module ActiveRecordProxyAdapters
|
|
25
25
|
db_config = connection.pool.try(:db_config) || NullConfig.new # AR 7.0.x does not support "NullConfig"
|
26
26
|
connection_name = db_config.name
|
27
27
|
|
28
|
-
prefix =
|
29
|
-
log_subscriber_replica_prefix(connection_name)
|
30
|
-
else
|
31
|
-
log_subscriber_primary_prefix(connection_name)
|
32
|
-
end
|
28
|
+
prefix = log_subscriber_prefix(connection_name)
|
33
29
|
|
34
30
|
"[#{prefix.call(event)}]"
|
35
31
|
end
|
@@ -10,19 +10,11 @@ module ActiveRecordProxyAdapters
|
|
10
10
|
# Provides helpers to access to reduce boilerplate while retrieving configuration properties.
|
11
11
|
module Configuration
|
12
12
|
# Helper to retrieve the proxy delay from the configuration stored in
|
13
|
-
# {ActiveRecordProxyAdapters::DatabaseConfiguration#
|
13
|
+
# {ActiveRecordProxyAdapters::DatabaseConfiguration#log_subscriber_prefix}.
|
14
14
|
# @param database_name [Symbol, String] The name of the database to retrieve the prefix.
|
15
15
|
# @return [Proc]
|
16
|
-
def
|
17
|
-
database_config(database_name).
|
18
|
-
end
|
19
|
-
|
20
|
-
# Helper to retrieve the proxy delay from the configuration stored in
|
21
|
-
# {ActiveRecordProxyAdapters::DatabaseConfiguration#log_subscriber_replica_prefix}.
|
22
|
-
# @param database_name [Symbol, String] The name of the database to retrieve the prefix.
|
23
|
-
# @return [Proc]
|
24
|
-
def log_subscriber_replica_prefix(database_name)
|
25
|
-
database_config(database_name).log_subscriber_replica_prefix
|
16
|
+
def log_subscriber_prefix(database_name)
|
17
|
+
database_config(database_name).log_subscriber_prefix
|
26
18
|
end
|
27
19
|
|
28
20
|
# Helper to retrieve the proxy delay from the configuration stored in
|