active_record_proxy_adapters 0.7.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eaa34c675630f13046dbc7170773fca75998eecef0b6e4990ce53110bdfdecef
4
- data.tar.gz: 92c024345625535b4f3bfcb241b898e3a26c09836a5f3c6dac61749c913aef85
3
+ metadata.gz: 5942f8ab80752d3abe9ac1015a991cc8bb8cbe7faa3961824e617ec478fd1cf0
4
+ data.tar.gz: daaf2fb00ad05b38c0477bb14d5a8f02d405863fd592252c63a887964385cc2f
5
5
  SHA512:
6
- metadata.gz: 76493c2922eb8f725410bc69223b9bc9dd121cece620e188a73b941ea7b8ef549108ffea8d3c1c38ddee2ed5686b6160284f87665ea431e41f4a32bccc80472a
7
- data.tar.gz: 24c3cf701b469e364a5206ace80d3e8a609fddcd1839337069f0b0f7bbbf27f6c35940d7642a5f717b6030bf776afa988912fb8306b0e8b5d4d2e7fe5bde0ff4
6
+ metadata.gz: 2f61e7d36e3d968c9cf4f873f3cc7d70c5a1054337aa0b773ad59d76ad52b33088dc2c4588cf996725a7fa2efda42de77888654db6b3e53a93026a06dced44e4
7
+ data.tar.gz: 022acae0581a7910927a18a7452750e9628b238cc590b05e3b3bf15c0b3d4f8b163fbcc7fcc30071f3673dc7d1c6cd33fa01c2bd1c88d0c1e5611de6f77c060f
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 primary, everything else goes to the replica).
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 = :primary
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.log_subscriber_primary_prefix = prefix
29
+ default_database_config.log_subscriber_prefix = prefix
29
30
  end
30
31
 
31
32
  def log_subscriber_primary_prefix
32
- default_database_config.log_subscriber_primary_prefix
33
+ default_database_config.log_subscriber_prefix
33
34
  end
34
35
 
35
36
  def log_subscriber_replica_prefix=(prefix)
36
- default_database_config.log_subscriber_replica_prefix = prefix
37
+ default_replica_config.log_subscriber_prefix = prefix
37
38
  end
38
39
 
39
40
  def log_subscriber_replica_prefix
40
- default_database_config.log_subscriber_replica_prefix
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 primary database is used. Thread safe.
29
- attr_reader :log_subscriber_primary_prefix
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 = Monitor.new
36
- self.proxy_delay = PROXY_DELAY
37
- self.checkout_timeout = CHECKOUT_TIMEOUT
38
- self.log_subscriber_primary_prefix = DEFAULT_PREFIX
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 log_subscriber_primary_prefix=(prefix)
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(:log_subscriber_replica_prefix, from: @log_subscriber_replica_prefix, to: prefix_proc) do
66
- @log_subscriber_replica_prefix = prefix_proc
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 = if db_config.replica?
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#log_subscriber_primary_prefix}.
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 log_subscriber_primary_prefix(database_name)
17
- database_config(database_name).log_subscriber_primary_prefix
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
@@ -35,13 +35,13 @@ module ActiveRecordProxyAdapters
35
35
  # These patterns define which database statments are considered write statments, so we can shortly re-route all
36
36
  # requests to the primary database so the replica has time to replicate
37
37
  WRITE_STATEMENT_MATCHERS = [
38
- /\ABEGIN/i,
39
- /\ACOMMIT/i,
40
- /\AROLLBACK/i,
41
- /INSERT\s[\s\S]*INTO\s[\s\S]*/i,
42
- /UPDATE\s[\s\S]*/i,
43
- /DELETE\s[\s\S]*FROM\s[\s\S]*/i,
44
- /DROP\s/i
38
+ /(\A|\s+?)BEGIN/i,
39
+ /(\A|\s+?)COMMIT/i,
40
+ /(\A|\s+?)ROLLBACK/i,
41
+ /(\A|\s+?)(INSERT\s+?INTO\s+?\S+?)/i,
42
+ /(\A|\s+?)UPDATE\s+?\S+?/i,
43
+ /(\A|\s+?)DELETE\s+?FROM\s+?\S+?/i,
44
+ /(\A|\s+?)DROP\s/i
45
45
  ].map(&:freeze).freeze
46
46
 
47
47
  # Abstract adapter methods that should be proxied.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordProxyAdapters
4
- VERSION = "0.7.0"
4
+ VERSION = "0.7.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_proxy_adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Cruz