active_record_host_pool 2.1.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: 0ba0aa0ff3c0b19e4895118e76f84cc57f46481baa482c59925946ecd56765bb
4
- data.tar.gz: e276dbda95168a03b511a5d7da14f94411ca7b8bc910ea2684631f25c9a22cf1
3
+ metadata.gz: 4db6c9a5a94e8f01d14956b6ce9446b73b3411956a2ad8fd6414ad72ca6ea3cc
4
+ data.tar.gz: 541c9f3b94434e0d21e6ba04d43ef2d299273f10b487c1224d18a5bb85b202c0
5
5
  SHA512:
6
- metadata.gz: 5b455f5fd116f96035fa816b39e0baf7f698d885decece79fd213c690c07db6b2f54e2328d4fa8dab68c536e86b58a9261b3c1a4c8aa45ccf35c1cb7b237168e
7
- data.tar.gz: aa7295a9be8e2892ff34452de014ea7627255d58e6eccd6a0d4a18d3b03152adb18fb2e453df017a058eeaf26bf8d4a768db3e436cef7da498f69e03d5f50917
6
+ metadata.gz: 55b8be16acfb0518e16c428f57fa7269c44b5a339d7c0df435796855150fb8beb514f1e8e484e0197befbe9b6dc8e3e80e4df176654945e791f4420b75e98cd8
7
+ data.tar.gz: '09648c47d09220fb302318b8a65c5a42e4280704cc1c8edbd7a6fd95ac57e8ff78dcc4a406fedc5d46ffa05c2cd61991d6bc9f9985911d7a626abf1a318121bf'
data/Changelog.md CHANGED
@@ -6,6 +6,28 @@ 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
+
17
+ ## [2.2.0]
18
+
19
+ ### Removed
20
+ - Support for Ruby 3.0.
21
+
22
+ ### Added
23
+ - Rails 6.1 testing with Trilogy.
24
+
25
+ ### Fixed
26
+ - Fixed using ActiveRecordHostPool and the `activerecord-trilogy-adapter v3.1+`.
27
+
28
+ ### Changed
29
+ - ActiveRecordHostPool will now raise an exception if you try to use a version of `activerecord-trilogy-adapter < 3.1`.
30
+
9
31
  ## [2.1.0]
10
32
 
11
33
  ### Changed
@@ -7,9 +7,8 @@ when :trilogy
7
7
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
8
8
  when "6.1", "7.0"
9
9
  require "trilogy_adapter/connection"
10
- require "trilogy_adapter/errors"
11
10
  ActiveRecord::Base.extend(TrilogyAdapter::Connection)
12
- when "7.1"
11
+ when "7.1", "7.2", "8.0"
13
12
  require "active_record/connection_adapters/trilogy_adapter"
14
13
  else
15
14
  raise "Unsupported version of Rails (v#{ActiveRecord::VERSION::STRING})"
@@ -18,29 +17,29 @@ end
18
17
 
19
18
  module ActiveRecordHostPool
20
19
  module DatabaseSwitch
21
- attr_reader :_host_pool_current_database
20
+ attr_reader :_host_pool_desired_database
22
21
  def initialize(*)
23
22
  @_cached_current_database = nil
24
23
  super
25
24
  end
26
25
 
27
- def _host_pool_current_database=(database)
28
- @_host_pool_current_database = database
29
- @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
30
29
  end
31
30
 
32
- if ActiveRecord.version >= Gem::Version.new("7.1")
31
+ if ActiveRecord.version >= Gem::Version.new("7.1") || ActiveRecordHostPool.loaded_db_adapter == :trilogy
33
32
  # Patch `raw_execute` instead of `execute` since this commit:
34
33
  # https://github.com/rails/rails/commit/f69bbcbc0752ca5d5af327d55922614a26f5c7e9
35
34
  def raw_execute(...)
36
- if _host_pool_current_database && !_no_switch
35
+ if _host_pool_desired_database && !_no_switch
37
36
  _switch_connection
38
37
  end
39
38
  super
40
39
  end
41
40
  else
42
41
  def execute(...)
43
- if _host_pool_current_database && !_no_switch
42
+ if _host_pool_desired_database && !_no_switch
44
43
  _switch_connection
45
44
  end
46
45
  super
@@ -72,30 +71,49 @@ module ActiveRecordHostPool
72
71
  attr_accessor :_no_switch
73
72
 
74
73
  def _switch_connection
75
- if _host_pool_current_database &&
74
+ if _host_pool_desired_database &&
76
75
  (
77
- (_host_pool_current_database != @_cached_current_database) ||
78
- @connection.object_id != @_cached_connection_object_id
79
- )
80
- 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
81
80
  clear_cache!
82
- raw_connection.select_db(_host_pool_current_database)
81
+ raw_connection.select_db(_host_pool_desired_database)
83
82
  end
84
- @_cached_current_database = _host_pool_current_database
85
- @_cached_connection_object_id = @connection.object_id
83
+ @_cached_current_database = _host_pool_desired_database
84
+ @_cached_connection_object_id = _real_connection_object_id
86
85
  end
87
86
  end
88
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
+
89
109
  # prevent different databases from sharing the same query cache
90
110
  def cache_sql(sql, *args)
91
- super(_host_pool_current_database.to_s + "/" + sql, *args)
111
+ super(_host_pool_desired_database.to_s + "/" + sql, *args)
92
112
  end
93
113
  end
94
114
 
95
115
  module PoolConfigPatch
96
116
  def pool
97
- ActiveSupport::ForkTracker.check!
98
-
99
117
  @pool || synchronize { @pool ||= ActiveRecordHostPool::PoolProxy.new(self) }
100
118
  end
101
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.1.0"
4
+ VERSION = "3.0.0"
5
5
  end
@@ -15,7 +15,11 @@ if Gem.loaded_specs.include?("mysql2")
15
15
  ActiveRecordHostPool.loaded_db_adapter = :mysql2
16
16
  elsif Gem.loaded_specs.include?("trilogy")
17
17
  require "trilogy"
18
- require "activerecord-trilogy-adapter" if ActiveRecord.version < Gem::Version.new("7.1")
18
+ if ActiveRecord.version < Gem::Version.new("7.1")
19
+ require "activerecord-trilogy-adapter"
20
+ require "trilogy_adapter/version"
21
+ raise "ActiveRecordHostPool is only compatible with activerecord-trilogy-adapter v3.1+" if Gem::Version.new("3.1") > TrilogyAdapter::VERSION
22
+ end
19
23
  ActiveRecordHostPool.loaded_db_adapter = :trilogy
20
24
  end
21
25
 
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.1.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-04-19 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
@@ -20,9 +20,6 @@ dependencies:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
22
  version: 6.1.0
23
- - - "<"
24
- - !ruby/object:Gem::Version
25
- version: '7.2'
26
23
  type: :runtime
27
24
  prerelease: false
28
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -30,9 +27,6 @@ dependencies:
30
27
  - - ">="
31
28
  - !ruby/object:Gem::Version
32
29
  version: 6.1.0
33
- - - "<"
34
- - !ruby/object:Gem::Version
35
- version: '7.2'
36
30
  description: ''
37
31
  email:
38
32
  - bquorning@zendesk.com
@@ -65,14 +59,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
59
  requirements:
66
60
  - - ">="
67
61
  - !ruby/object:Gem::Version
68
- version: 3.0.0
62
+ version: 3.1.0
69
63
  required_rubygems_version: !ruby/object:Gem::Requirement
70
64
  requirements:
71
65
  - - ">="
72
66
  - !ruby/object:Gem::Version
73
67
  version: '0'
74
68
  requirements: []
75
- rubygems_version: 3.5.3
69
+ rubygems_version: 3.5.11
76
70
  signing_key:
77
71
  specification_version: 4
78
72
  summary: Allow ActiveRecord to share a connection to multiple databases on the same