active_record_proxy_adapters 0.4.4 → 0.4.5
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: 5c60e5b3ab2242f3c816eeefd49bbb4db18d039b9dd958cc6f356820434f9625
|
4
|
+
data.tar.gz: 995f0698c350be766b1b6ae83061187a4e2a76a5cd1f6a6bb3b76f4df06a0fd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 438d205bc160dfa52773b8048c9057dbf7b564d26927cb25a25ef470ed9a3cd5fe579acb067aa64cc12eeb7a02fa003ce0b75f2e7dd8093349d4925dac970cd0
|
7
|
+
data.tar.gz: dc0fb03f548118d1475f9fa5a94187b1d31dbaec66ebafef5d906d59597d116cceb4ec31ee25be61f2499cbefb2b6ba46a67c81d0a59f0e0a399f5cb26550932
|
@@ -18,16 +18,27 @@ module ActiveRecordProxyAdapters
|
|
18
18
|
/\A\s*select.+for update\Z/i, /select.+lock in share mode\Z/i,
|
19
19
|
/\A\s*select.+(nextval|currval|lastval|get_lock|release_lock|pg_advisory_lock|pg_advisory_unlock)\(/i
|
20
20
|
].map(&:freeze).freeze
|
21
|
+
|
22
|
+
CTE_MATCHER = /\A\s*WITH\s+(?<CTE>\S+\s+AS\s+\(\s?[\s\S]*\))/i
|
21
23
|
# All queries that match these patterns should be sent to the replica database
|
22
|
-
SQL_REPLICA_MATCHERS
|
24
|
+
SQL_REPLICA_MATCHERS = [
|
25
|
+
/\A\s*(select)\s/i,
|
26
|
+
/#{CTE_MATCHER.source}\s*select/i
|
27
|
+
].map(&:freeze).freeze
|
23
28
|
# All queries that match these patterns should be sent to all databases
|
24
29
|
SQL_ALL_MATCHERS = [/\A\s*set\s/i].map(&:freeze).freeze
|
25
30
|
# Local sets queries should not be sent to all datbases
|
26
31
|
SQL_SKIP_ALL_MATCHERS = [/\A\s*set\s+local\s/i].map(&:freeze).freeze
|
27
32
|
# These patterns define which database statments are considered write statments, so we can shortly re-route all
|
28
33
|
# requests to the primary database so the replica has time to replicate
|
29
|
-
WRITE_STATEMENT_MATCHERS = [
|
30
|
-
|
34
|
+
WRITE_STATEMENT_MATCHERS = [
|
35
|
+
/\ABEGIN/i,
|
36
|
+
/\ACOMMIT/i,
|
37
|
+
/INSERT\s[\s\S]*INTO\s[\s\S]*/i,
|
38
|
+
/UPDATE\s[\s\S]*/i,
|
39
|
+
/DELETE\s[\s\S]*FROM\s[\s\S]*/i,
|
40
|
+
/DROP\s/i
|
41
|
+
].map(&:freeze).freeze
|
31
42
|
|
32
43
|
# Abstract adapter methods that should be proxied.
|
33
44
|
hijack_method(*ActiveRecordContext.hijackable_methods)
|
@@ -171,16 +182,21 @@ module ActiveRecordProxyAdapters
|
|
171
182
|
# @return [TrueClass] if there has been a write within the last {#proxy_delay} seconds
|
172
183
|
# @return [TrueClass] if sql_string matches a write statement (i.e. INSERT, UPDATE, DELETE, SELECT FOR UPDATE)
|
173
184
|
# @return [FalseClass] if sql_string matches a read statement (i.e. SELECT)
|
174
|
-
def need_primary?(sql_string)
|
175
|
-
return true
|
176
|
-
|
185
|
+
def need_primary?(sql_string) # rubocop:disable Metrics/CyclomaticComplexity
|
186
|
+
return true if recent_write_to_primary?
|
177
187
|
return true if in_transaction?
|
188
|
+
return true if cte_for_write?(sql_string)
|
178
189
|
return true if SQL_PRIMARY_MATCHERS.any?(&match_sql?(sql_string))
|
179
190
|
return false if SQL_REPLICA_MATCHERS.any?(&match_sql?(sql_string))
|
180
191
|
|
181
192
|
true
|
182
193
|
end
|
183
194
|
|
195
|
+
def cte_for_write?(sql_string)
|
196
|
+
CTE_MATCHER.match?(sql_string) &&
|
197
|
+
WRITE_STATEMENT_MATCHERS.any?(&match_sql?(sql_string))
|
198
|
+
end
|
199
|
+
|
184
200
|
def need_all?(sql_string)
|
185
201
|
return false if SQL_SKIP_ALL_MATCHERS.any?(&match_sql?(sql_string))
|
186
202
|
|
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.4.
|
4
|
+
version: 0.4.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Cruz
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-03-
|
10
|
+
date: 2025-03-31 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activerecord
|