active_record_host_pool 2.2.0 → 3.1.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: cc285794dcf2d9fd3d54249bc94aea1a9ae64d76868040dc583aa669388cd444
4
- data.tar.gz: fe57775f7b80266bddb5f019b48eb7f5a741a7a9f8e0858604e6a74e12df5501
3
+ metadata.gz: b012c288bdd269561992eb0a5a53310f7424f18102dae61655f97bc654bf146b
4
+ data.tar.gz: 9f3bdbda2e96929770dd6f9ad33293182d460e68a961a0a89370b3e2ca30a77d
5
5
  SHA512:
6
- metadata.gz: 76037970cdd4288865b5e7e02d233d64699c60cc9054887465b21442e136da7f336d77623be76222ee45a75daac74b5e34581942793bf379fcb7df0b6229a4c4
7
- data.tar.gz: e8ae94a9a9141891e5a4dbb0a61d08b13499546603b409f73d9a0de799112c68ca2705670a200c08cbb7ce3178135040808ac14323ac223afbc9519e21bff960
6
+ metadata.gz: d5540b9126209ea0a458ae8a5d3d96267cb449193a53d8798f34257b0e90dc8178faa2ef14e10477f4131f3619d7cf4406d1083f45b87ebe178b5b3c2ae1f72d
7
+ data.tar.gz: d314aca9726a5393a51921c9727df1caf688a0350abc6db0341cbc1b5d4dec5d5cec222384bd71ddb4f64fddd81f6da108fc6e2071cd016da2a48c104fd1b354
data/Changelog.md CHANGED
@@ -6,7 +6,21 @@ and as of v1.0.0 this project adheres to [Semantic Versioning](https://semver.or
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.1.0]
10
+
11
+ ### Added
12
+ - Calls `#clean!` on the connection after switching databases.
13
+
14
+ ## [3.0.0]
15
+
16
+ ### Added
17
+ - Support and testing for Rails 7.2 & Rails main.
18
+
19
+ ### Removed
20
+ - Support for ActiveRecord's legacy connection handling.
21
+
9
22
  ## [2.2.0]
23
+
10
24
  ### Removed
11
25
  - Support for Ruby 3.0.
12
26
 
@@ -8,7 +8,7 @@ when :trilogy
8
8
  when "6.1", "7.0"
9
9
  require "trilogy_adapter/connection"
10
10
  ActiveRecord::Base.extend(TrilogyAdapter::Connection)
11
- when "7.1"
11
+ when "7.1", "7.2", "8.0"
12
12
  require "active_record/connection_adapters/trilogy_adapter"
13
13
  else
14
14
  raise "Unsupported version of Rails (v#{ActiveRecord::VERSION::STRING})"
@@ -17,29 +17,29 @@ end
17
17
 
18
18
  module ActiveRecordHostPool
19
19
  module DatabaseSwitch
20
- attr_reader :_host_pool_current_database
20
+ attr_reader :_host_pool_desired_database
21
21
  def initialize(*)
22
22
  @_cached_current_database = nil
23
23
  super
24
24
  end
25
25
 
26
- def _host_pool_current_database=(database)
27
- @_host_pool_current_database = database
28
- @config[:database] = _host_pool_current_database
26
+ def _host_pool_desired_database=(database)
27
+ @_host_pool_desired_database = database
28
+ @config[:database] = _host_pool_desired_database
29
29
  end
30
30
 
31
31
  if ActiveRecord.version >= Gem::Version.new("7.1") || ActiveRecordHostPool.loaded_db_adapter == :trilogy
32
32
  # Patch `raw_execute` instead of `execute` since this commit:
33
33
  # https://github.com/rails/rails/commit/f69bbcbc0752ca5d5af327d55922614a26f5c7e9
34
34
  def raw_execute(...)
35
- if _host_pool_current_database && !_no_switch
35
+ if _host_pool_desired_database && !_no_switch
36
36
  _switch_connection
37
37
  end
38
38
  super
39
39
  end
40
40
  else
41
41
  def execute(...)
42
- if _host_pool_current_database && !_no_switch
42
+ if _host_pool_desired_database && !_no_switch
43
43
  _switch_connection
44
44
  end
45
45
  super
@@ -71,30 +71,50 @@ module ActiveRecordHostPool
71
71
  attr_accessor :_no_switch
72
72
 
73
73
  def _switch_connection
74
- if _host_pool_current_database &&
74
+ if _host_pool_desired_database &&
75
75
  (
76
- (_host_pool_current_database != @_cached_current_database) ||
77
- @connection.object_id != @_cached_connection_object_id
78
- )
79
- log("select_db #{_host_pool_current_database}", "SQL") do
76
+ _desired_database_changed? ||
77
+ _real_connection_changed?
78
+ )
79
+ log("select_db #{_host_pool_desired_database}", "SQL") do
80
80
  clear_cache!
81
- raw_connection.select_db(_host_pool_current_database)
81
+ raw_connection.select_db(_host_pool_desired_database)
82
+ clean! if respond_to?(:clean)
82
83
  end
83
- @_cached_current_database = _host_pool_current_database
84
- @_cached_connection_object_id = @connection.object_id
84
+ @_cached_current_database = _host_pool_desired_database
85
+ @_cached_connection_object_id = _real_connection_object_id
85
86
  end
86
87
  end
87
88
 
89
+ def _desired_database_changed?
90
+ _host_pool_desired_database != @_cached_current_database
91
+ end
92
+
93
+ # rubocop:disable Lint/DuplicateMethods
94
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
95
+ when "6.1", "7.0", "7.1"
96
+ def _real_connection_object_id
97
+ @connection.object_id
98
+ end
99
+ else
100
+ def _real_connection_object_id
101
+ @raw_connection.object_id
102
+ end
103
+ end
104
+ # rubocop:enable Lint/DuplicateMethods
105
+
106
+ def _real_connection_changed?
107
+ _real_connection_object_id != @_cached_connection_object_id
108
+ end
109
+
88
110
  # prevent different databases from sharing the same query cache
89
111
  def cache_sql(sql, *args)
90
- super(_host_pool_current_database.to_s + "/" + sql, *args)
112
+ super(_host_pool_desired_database.to_s + "/" + sql, *args)
91
113
  end
92
114
  end
93
115
 
94
116
  module PoolConfigPatch
95
117
  def pool
96
- ActiveSupport::ForkTracker.check!
97
-
98
118
  @pool || synchronize { @pool ||= ActiveRecordHostPool::PoolProxy.new(self) }
99
119
  end
100
120
  end
@@ -14,7 +14,7 @@ module ActiveRecordHostPool
14
14
  end
15
15
 
16
16
  def __getobj__
17
- @cx._host_pool_current_database = @database
17
+ @cx._host_pool_desired_database = @database
18
18
  @cx
19
19
  end
20
20
 
@@ -43,17 +43,35 @@ module ActiveRecordHostPool
43
43
 
44
44
  attr_reader :pool_config
45
45
 
46
- def connection(*args)
47
- real_connection = _unproxied_connection(*args)
48
- _connection_proxy_for(real_connection, @config[:database])
49
- rescue RESCUABLE_DB_ERROR, ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
50
- _connection_pools.delete(_pool_key)
51
- Kernel.raise
52
- end
46
+ # rubocop:disable Lint/DuplicateMethods
47
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
48
+ when "6.1", "7.0", "7.1"
49
+ def connection(*args)
50
+ real_connection = _unproxied_connection(*args)
51
+ _connection_proxy_for(real_connection, @config[:database])
52
+ rescue RESCUABLE_DB_ERROR, ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
53
+ _connection_pools.delete(_pool_key)
54
+ Kernel.raise
55
+ end
56
+
57
+ def _unproxied_connection(*args)
58
+ _connection_pool.connection(*args)
59
+ end
60
+ else
61
+ def lease_connection(*args)
62
+ real_connection = _unproxied_connection(*args)
63
+ _connection_proxy_for(real_connection, @config[:database])
64
+ rescue RESCUABLE_DB_ERROR, ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
65
+ _connection_pools.delete(_pool_key)
66
+ Kernel.raise
67
+ end
68
+ alias_method :connection, :lease_connection
53
69
 
54
- def _unproxied_connection(*args)
55
- _connection_pool.connection(*args)
70
+ def _unproxied_connection(*args)
71
+ _connection_pool.lease_connection(*args)
72
+ end
56
73
  end
74
+ # rubocop:enable Lint/DuplicateMethods
57
75
 
58
76
  # by the time we are patched into ActiveRecord, the current thread has already established
59
77
  # a connection. thus we need to patch both connection and checkout/checkin
@@ -67,11 +85,48 @@ module ActiveRecordHostPool
67
85
  _connection_pool.checkin(cx)
68
86
  end
69
87
 
70
- def with_connection
71
- cx = checkout
72
- yield cx
73
- ensure
74
- checkin cx
88
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
89
+ when "6.1", "7.0", "7.1"
90
+ def with_connection
91
+ cx = checkout
92
+ yield cx
93
+ ensure
94
+ checkin cx
95
+ end
96
+ else
97
+ def with_connection(prevent_permanent_checkout: false) # rubocop:disable Lint/DuplicateMethods
98
+ real_connection_lease = _connection_pool.send(:connection_lease)
99
+ sticky_was = real_connection_lease.sticky
100
+ real_connection_lease.sticky = false if prevent_permanent_checkout
101
+
102
+ if real_connection_lease.connection
103
+ begin
104
+ yield _connection_proxy_for(real_connection_lease.connection, @config[:database])
105
+ ensure
106
+ real_connection_lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was
107
+ end
108
+ else
109
+ begin
110
+ real_connection_lease.connection = _unproxied_connection
111
+ yield _connection_proxy_for(real_connection_lease.connection, @config[:database])
112
+ ensure
113
+ real_connection_lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was
114
+ _connection_pool.release_connection(real_connection_lease) unless real_connection_lease.sticky
115
+ end
116
+ end
117
+ end
118
+
119
+ def active_connection?
120
+ real_connection_lease = _connection_pool.send(:connection_lease)
121
+ if real_connection_lease.connection
122
+ _connection_proxy_for(real_connection_lease.connection, @config[:database])
123
+ end
124
+ end
125
+ alias_method :active_connection, :active_connection?
126
+
127
+ def schema_cache
128
+ @schema_cache ||= ActiveRecord::ConnectionAdapters::BoundSchemaReflection.new(_connection_pool.schema_reflection, self)
129
+ end
75
130
  end
76
131
 
77
132
  def disconnect!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordHostPool
4
- VERSION = "2.2.0"
4
+ VERSION = "3.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_host_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Quorning
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2024-07-22 00:00:00.000000000 Z
14
+ date: 2024-09-02 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord