active_record_proxy_adapters 0.9.0 → 0.10.0

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: f404b61f94eec82c838124e111eba202e7fdbea61c07596739219749e7c76ac2
4
- data.tar.gz: 930327b485fb9b7637a1b810c0cb52def9d0164777103f918862a9dd4a0a89e7
3
+ metadata.gz: bca625e0a728907c505db941a4398b5aa144dcdd5f6866ac9a3969f0b783c4d0
4
+ data.tar.gz: 3ab824330a83e8309b31763b24fe7c74ba0efff2a1eff28505d2a8216442f557
5
5
  SHA512:
6
- metadata.gz: 4d0c6210521feb5db9ce40fc476607fb862d818e16feb23d795e95f20f20b8db0b1311049a5895f6e9795ba2740cdef140ecb6ec67f5f1c7c7aec759e0a4c2ca
7
- data.tar.gz: 1134342f6be3093cb5a3ddaba514ef702adb611e5646a3abd3c68073fcb7060459a39a5845983733b4c5f0bc24d5928f57e8333b7741bbb8b3fa812d9472f066
6
+ metadata.gz: 1bc0567abfff9e1877abe107074a7e3f35f574ecefc2584f188809a0940c37a9192c88f3ae71f0e682d7115de75cd8b82ef827874e75195dc635572a8a8c7173
7
+ data.tar.gz: 6ee734e3da7c41d458403e10589d3edd8126862ddcfa2d63034d75c884ec98e383c58e0877166c25155c44ef3a2c13e307378df850b47b0da77d103ff31d57c2
@@ -18,19 +18,11 @@ module ActiveRecordProxyAdapters
18
18
  end
19
19
 
20
20
  def hijackable_methods
21
- hijackable = %i[execute exec_query]
22
-
23
- hijackable << :internal_exec_query if active_record_v7_1_or_greater?
24
-
25
- hijackable
21
+ %i[execute exec_query internal_exec_query]
26
22
  end
27
23
 
28
24
  def active_record_v7?
29
- active_record_version >= Gem::Version.new("7.1") && active_record_version < Gem::Version.new("8.0")
30
- end
31
-
32
- def active_record_v7_1_or_greater?
33
- active_record_version >= Gem::Version.new("7.1")
25
+ active_record_version >= Gem::Version.new("7.2") && active_record_version < Gem::Version.new("8.0")
34
26
  end
35
27
 
36
28
  def active_record_v7_2_or_greater?
@@ -6,40 +6,3 @@ rescue LoadError
6
6
  # mysql2 not available
7
7
  return
8
8
  end
9
-
10
- module ActiveRecordProxyAdapters
11
- module Mysql2
12
- # Module to extend ActiveRecord::Base with the connection handling methods.
13
- # Required to make adapter work in ActiveRecord versions <= 7.2.x
14
- module ConnectionHandling
15
- def mysql2_proxy_adapter_class
16
- ::ActiveRecord::ConnectionAdapters::Mysql2ProxyAdapter
17
- end
18
-
19
- # This method is a copy and paste from Rails' mysql2_connection,
20
- # replacing Mysql2Adapter by Mysql2ProxyAdapter
21
- # This is required by ActiveRecord versions <= 7.2.x to establish a connection using the adapter.
22
- def mysql2_proxy_connection(config) # rubocop:disable Metrics/MethodLength
23
- config = config.symbolize_keys
24
- config[:flags] ||= 0
25
-
26
- if config[:flags].is_a? Array
27
- config[:flags].push "FOUND_ROWS"
28
- else
29
- config[:flags] |= ::Mysql2::Client::FOUND_ROWS
30
- end
31
-
32
- mysql2_proxy_adapter_class.new(
33
- mysql2_proxy_adapter_class.new_client(config),
34
- logger,
35
- nil,
36
- config
37
- )
38
- end
39
- end
40
- end
41
- end
42
-
43
- ActiveSupport.on_load(:active_record) do
44
- ActiveRecord::Base.extend(ActiveRecordProxyAdapters::Mysql2::ConnectionHandling)
45
- end
@@ -6,41 +6,3 @@ rescue LoadError
6
6
  # Postgres not available
7
7
  return
8
8
  end
9
-
10
- module ActiveRecordProxyAdapters
11
- # Module to extend ActiveRecord::Base with the connection handling methods.
12
- # Required to make adapter work in ActiveRecord versions <= 7.2.x
13
- module PostgreSQL
14
- module ConnectionHandling # rubocop:disable Style/Documentation
15
- def postgresql_proxy_adapter_class
16
- ::ActiveRecord::ConnectionAdapters::PostgreSQLProxyAdapter
17
- end
18
-
19
- # This method is a copy and paste from Rails' postgresql_connection,
20
- # replacing PostgreSQLAdapter by PostgreSQLProxyAdapter
21
- # This is required by ActiveRecord versions <= 7.2.x to establish a connection using the adapter.
22
- def postgresql_proxy_connection(config) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
23
- conn_params = config.symbolize_keys.compact
24
-
25
- # Map ActiveRecords param names to PGs.
26
- conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
27
- conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
28
-
29
- # Forward only valid config params to PG::Connection.connect.
30
- valid_conn_param_keys = PG::Connection.conndefaults_hash.keys + [:requiressl]
31
- conn_params.slice!(*valid_conn_param_keys)
32
-
33
- postgresql_proxy_adapter_class.new(
34
- postgresql_proxy_adapter_class.new_client(conn_params),
35
- logger,
36
- conn_params,
37
- config
38
- )
39
- end
40
- end
41
- end
42
- end
43
-
44
- ActiveSupport.on_load(:active_record) do
45
- ActiveRecord::Base.extend(ActiveRecordProxyAdapters::PostgreSQL::ConnectionHandling)
46
- end
@@ -6,23 +6,3 @@ rescue LoadError
6
6
  # sqlite3 not available
7
7
  return
8
8
  end
9
-
10
- module ActiveRecordProxyAdapters
11
- module SQLite3
12
- # Module to extend ActiveRecord::Base with the connection handling methods.
13
- # Required to make adapter work in ActiveRecord versions <= 7.2.x
14
- module ConnectionHandling
15
- def sqlite3_proxy_adapter_class
16
- ActiveRecord::ConnectionAdapters::SQLite3ProxyAdapter
17
- end
18
-
19
- def sqlite3_proxy_connection(config)
20
- sqlite3_proxy_adapter_class.new(config)
21
- end
22
- end
23
- end
24
- end
25
-
26
- ActiveSupport.on_load(:active_record) do
27
- ActiveRecord::Base.extend(ActiveRecordProxyAdapters::SQLite3::ConnectionHandling)
28
- end
@@ -6,39 +6,3 @@ rescue LoadError
6
6
  # trilogy not available
7
7
  return
8
8
  end
9
-
10
- module ActiveRecordProxyAdapters
11
- module Trilogy
12
- # Module to extend ActiveRecord::Base with the connection handling methods.
13
- # Required to make adapter work in ActiveRecord versions <= 7.2.x
14
- module ConnectionHandling
15
- def trilogy_proxy_adapter_class
16
- ActiveRecord::ConnectionAdapters::TrilogyProxyAdapter
17
- end
18
-
19
- def trilogy_proxy_connection(config) # rubocop:disable Metrics/MethodLength
20
- configuration = config.dup
21
-
22
- # Set FOUND_ROWS capability on the connection so UPDATE queries returns number of rows
23
- # matched rather than number of rows updated.
24
- configuration[:found_rows] = true
25
-
26
- options = [
27
- configuration[:host],
28
- configuration[:port],
29
- configuration[:database],
30
- configuration[:username],
31
- configuration[:password],
32
- configuration[:socket],
33
- 0
34
- ]
35
-
36
- trilogy_proxy_adapter_class.new nil, logger, options, configuration
37
- end
38
- end
39
- end
40
- end
41
-
42
- ActiveSupport.on_load(:active_record) do
43
- ActiveRecord::Base.extend(ActiveRecordProxyAdapters::Trilogy::ConnectionHandling)
44
- end
@@ -7,7 +7,6 @@ require "active_record_proxy_adapters/hijackable"
7
7
  require "active_record_proxy_adapters/mixin/configuration"
8
8
  require "active_support/core_ext/module/delegation"
9
9
  require "active_support/core_ext/object/blank"
10
- require "timeout"
11
10
 
12
11
  module ActiveRecordProxyAdapters
13
12
  # This is the base class for all proxies. It defines the methods that should be proxied
@@ -160,8 +159,7 @@ module ActiveRecordProxyAdapters
160
159
  end
161
160
 
162
161
  def pending_migration_connection?
163
- active_record_context.active_record_v7_1_or_greater? &&
164
- connection_class.name == "ActiveRecord::PendingMigrationConnection"
162
+ connection_class.name == "ActiveRecord::PendingMigrationConnection"
165
163
  end
166
164
 
167
165
  def connection_for(role, sql_string)
@@ -224,9 +222,8 @@ module ActiveRecordProxyAdapters
224
222
 
225
223
  def match_sql?(sql_string)
226
224
  proc do |matcher|
227
- # TODO: switch to regexp timeout once Ruby 3.1 support is dropped.
228
- Timeout.timeout(proxy_checkout_timeout.to_f) { matcher.match?(sql_string) }
229
- rescue Timeout::Error
225
+ Regexp.new(matcher.source, Regexp::IGNORECASE, timeout: proxy_checkout_timeout.to_f).match?(sql_string)
226
+ rescue Regexp::TimeoutError
230
227
  regexp_timeout_strategy.call(sql_string, matcher)
231
228
 
232
229
  false
@@ -11,9 +11,15 @@ module ActiveRecordProxyAdapters
11
11
  # Hooks into rails boot process to extend ActiveRecord with the proxy adapter.
12
12
  class Railtie < Rails::Railtie
13
13
  require "active_record_proxy_adapters/middleware"
14
+ require "active_record_proxy_adapters/rake"
14
15
 
15
16
  initializer "active_record_proxy_adapters.configure_rails_initialization" do |app|
16
17
  app.middleware.use ActiveRecordProxyAdapters::Middleware
17
18
  end
19
+
20
+ rake_tasks do
21
+ ActiveRecordProxyAdapters::Rake.load_tasks
22
+ ActiveRecordProxyAdapters::Rake.enhance_db_tasks
23
+ end
18
24
  end
19
25
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rake"
4
+
5
+ module ActiveRecordProxyAdapters
6
+ # Enhances all rails db rake tasks to stick to writer connection
7
+ module Rake
8
+ module_function
9
+
10
+ def load_tasks
11
+ Dir[rake_tasks_path].each { |rake_path| load(rake_path) }
12
+ end
13
+
14
+ def rake_tasks_path
15
+ File.join(__dir__, "tasks/**/*.rake")
16
+ end
17
+
18
+ def enhance_db_tasks
19
+ ::Rake::Task
20
+ .tasks
21
+ .select(&enhanceable_db_task?)
22
+ .each { |task| task.enhance([push_to_stack_rake_task.name], &pop_from_stack_and_reenable) }
23
+ end
24
+
25
+ def push_to_stack_rake_task
26
+ ::Rake::Task["arpa:push_to_stack"]
27
+ end
28
+
29
+ def pop_from_stack_rake_task
30
+ ::Rake::Task["arpa:pop_from_stack"]
31
+ end
32
+
33
+ def pop_from_stack_and_reenable
34
+ proc do
35
+ pop_from_stack_rake_task.invoke
36
+ [push_to_stack_rake_task, pop_from_stack_rake_task].each(&:reenable)
37
+ end
38
+ end
39
+
40
+ def enhanceable_db_task?
41
+ proc do |task|
42
+ task_name = task.name
43
+ task_name.start_with?("db:") && task_name != "db:load_config"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :arpa do
4
+ desc "Pushes to connected_to stack before rake task is executed"
5
+ task :push_to_stack do
6
+ writing_role = ActiveRecordProxyAdapters::ActiveRecordContext.writing_role
7
+
8
+ ActiveRecord::Base.connected_to_stack << {
9
+ role: writing_role,
10
+ shard: nil,
11
+ prevent_writes: false,
12
+ klasses: [ActiveRecord::Base]
13
+ }
14
+
15
+ Thread.current.thread_variable_set(:arpa_rake_pushed_to_stack, true)
16
+ end
17
+
18
+ desc "Pops from connected_to stack after rake task is invoked"
19
+ task :pop_from_stack do
20
+ if Thread.current.thread_variable_get(:arpa_rake_pushed_to_stack)
21
+ ActiveRecord::Base.connected_to_stack.pop
22
+ Thread.current.thread_variable_set(:arpa_rake_pushed_to_stack, nil)
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordProxyAdapters
4
- VERSION = "0.9.0"
4
+ VERSION = "0.10.0"
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.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Cruz
@@ -15,40 +15,40 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 7.1.0
18
+ version: 7.2.0
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
- version: '8.1'
21
+ version: '8.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 7.1.0
28
+ version: 7.2.0
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
- version: '8.1'
31
+ version: '8.2'
32
32
  - !ruby/object:Gem::Dependency
33
33
  name: activesupport
34
34
  requirement: !ruby/object:Gem::Requirement
35
35
  requirements:
36
36
  - - ">="
37
37
  - !ruby/object:Gem::Version
38
- version: 7.1.0
38
+ version: 7.2.0
39
39
  - - "<"
40
40
  - !ruby/object:Gem::Version
41
- version: '8.1'
41
+ version: '8.2'
42
42
  type: :runtime
43
43
  prerelease: false
44
44
  version_requirements: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: 7.1.0
48
+ version: 7.2.0
49
49
  - - "<"
50
50
  - !ruby/object:Gem::Version
51
- version: '8.1'
51
+ version: '8.2'
52
52
  - !ruby/object:Gem::Dependency
53
53
  name: digest
54
54
  requirement: !ruby/object:Gem::Requirement
@@ -91,20 +91,6 @@ dependencies:
91
91
  - - ">="
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
- - !ruby/object:Gem::Dependency
95
- name: timeout
96
- requirement: !ruby/object:Gem::Requirement
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- version: '0'
101
- type: :runtime
102
- prerelease: false
103
- version_requirements: !ruby/object:Gem::Requirement
104
- requirements:
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- version: '0'
108
94
  description: |-
109
95
  This gem allows automatic connection switching between a primary and one read replica database in ActiveRecord.
110
96
  It pattern matches the SQL statement being sent to decide whether it should go to the replica (SELECT) or the
@@ -154,8 +140,10 @@ files:
154
140
  - lib/active_record_proxy_adapters/railties/postgresql.rb
155
141
  - lib/active_record_proxy_adapters/railties/sqlite3.rb
156
142
  - lib/active_record_proxy_adapters/railties/trilogy.rb
143
+ - lib/active_record_proxy_adapters/rake.rb
157
144
  - lib/active_record_proxy_adapters/sqlite3_proxy.rb
158
145
  - lib/active_record_proxy_adapters/synchronizable_configuration.rb
146
+ - lib/active_record_proxy_adapters/tasks/arpa.rake
159
147
  - lib/active_record_proxy_adapters/trilogy_proxy.rb
160
148
  - lib/active_record_proxy_adapters/version.rb
161
149
  homepage: https://github.com/Nasdaq/active_record_proxy_adapters