active_record_host_pool 2.2.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc285794dcf2d9fd3d54249bc94aea1a9ae64d76868040dc583aa669388cd444
4
- data.tar.gz: fe57775f7b80266bddb5f019b48eb7f5a741a7a9f8e0858604e6a74e12df5501
3
+ metadata.gz: 4db6c9a5a94e8f01d14956b6ce9446b73b3411956a2ad8fd6414ad72ca6ea3cc
4
+ data.tar.gz: 541c9f3b94434e0d21e6ba04d43ef2d299273f10b487c1224d18a5bb85b202c0
5
5
  SHA512:
6
- metadata.gz: 76037970cdd4288865b5e7e02d233d64699c60cc9054887465b21442e136da7f336d77623be76222ee45a75daac74b5e34581942793bf379fcb7df0b6229a4c4
7
- data.tar.gz: e8ae94a9a9141891e5a4dbb0a61d08b13499546603b409f73d9a0de799112c68ca2705670a200c08cbb7ce3178135040808ac14323ac223afbc9519e21bff960
6
+ metadata.gz: 55b8be16acfb0518e16c428f57fa7269c44b5a339d7c0df435796855150fb8beb514f1e8e484e0197befbe9b6dc8e3e80e4df176654945e791f4420b75e98cd8
7
+ data.tar.gz: '09648c47d09220fb302318b8a65c5a42e4280704cc1c8edbd7a6fd95ac57e8ff78dcc4a406fedc5d46ffa05c2cd61991d6bc9f9985911d7a626abf1a318121bf'
data/Changelog.md CHANGED
@@ -6,7 +6,16 @@ and as of v1.0.0 this project adheres to [Semantic Versioning](https://semver.or
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.0.0]
10
+
11
+ ### Added
12
+ - Support and testing for Rails 7.2 & Rails main.
13
+
14
+ ### Removed
15
+ - Support for ActiveRecord's legacy connection handling.
16
+
9
17
  ## [2.2.0]
18
+
10
19
  ### Removed
11
20
  - Support for Ruby 3.0.
12
21
 
@@ -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,49 @@ 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
82
  end
83
- @_cached_current_database = _host_pool_current_database
84
- @_cached_connection_object_id = @connection.object_id
83
+ @_cached_current_database = _host_pool_desired_database
84
+ @_cached_connection_object_id = _real_connection_object_id
85
85
  end
86
86
  end
87
87
 
88
+ def _desired_database_changed?
89
+ _host_pool_desired_database != @_cached_current_database
90
+ end
91
+
92
+ # rubocop:disable Lint/DuplicateMethods
93
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
94
+ when "6.1", "7.0", "7.1"
95
+ def _real_connection_object_id
96
+ @connection.object_id
97
+ end
98
+ else
99
+ def _real_connection_object_id
100
+ @raw_connection.object_id
101
+ end
102
+ end
103
+ # rubocop:enable Lint/DuplicateMethods
104
+
105
+ def _real_connection_changed?
106
+ _real_connection_object_id != @_cached_connection_object_id
107
+ end
108
+
88
109
  # prevent different databases from sharing the same query cache
89
110
  def cache_sql(sql, *args)
90
- super(_host_pool_current_database.to_s + "/" + sql, *args)
111
+ super(_host_pool_desired_database.to_s + "/" + sql, *args)
91
112
  end
92
113
  end
93
114
 
94
115
  module PoolConfigPatch
95
116
  def pool
96
- ActiveSupport::ForkTracker.check!
97
-
98
117
  @pool || synchronize { @pool ||= ActiveRecordHostPool::PoolProxy.new(self) }
99
118
  end
100
119
  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.0.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.0.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-08-26 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord