Skip to content

Commit 329706b

Browse files
authored
Merge pull request #104 from zendesk/bquorning.fix-clear-query-cache-patch
Fix ActiveRecord::Base.clear_on_handler
2 parents 35fca7a + be9ebe3 commit 329706b

12 files changed

Lines changed: 35 additions & 20 deletions

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ jobs:
3232
fail-fast: false
3333
matrix:
3434
ruby-version:
35-
- 2.6
36-
- 2.7
37-
- 3.0
38-
- 3.1
35+
- "2.6"
36+
- "2.7"
37+
- "3.0"
38+
- "3.1"
3939
gemfile:
4040
- rails5.1
4141
- rails5.2

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ and as of v1.0.0 this project adheres to [Semantic Versioning](https://semver.or
66

77
## [Unreleased]
88

9+
## [1.2.2] - 2023-01-18
10+
### Added
11+
- Add a new `ActiveRecordHostPool::PoolProxy#_unproxied_connection` method which gives access to the underlying, "real", shared connection without going through the connection proxy, which would call `#_host_pool_current_database=` on the underlying connection. (https://github.com/zendesk/active_record_host_pool/pull/104)
12+
13+
### Fixed
14+
- Fix the patch for `ActiveRecord::Base.clear_on_handler` to work correctly right after the creation of a new connection pool. (https://github.com/zendesk/active_record_host_pool/pull/104)
15+
916
## [1.2.1] - 2022-12-23
1017
### Fixed
1118
- Fix forwarding of kwargs when calling `#execute` in Rails 7. (https://github.com/zendesk/active_record_host_pool/pull/101)

gemfiles/rails5.1.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
active_record_host_pool (1.2.1)
4+
active_record_host_pool (1.2.2)
55
activerecord (>= 5.1.0, < 7.1)
66
mysql2
77

gemfiles/rails5.2.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
active_record_host_pool (1.2.1)
4+
active_record_host_pool (1.2.2)
55
activerecord (>= 5.1.0, < 7.1)
66
mysql2
77

gemfiles/rails6.0.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
active_record_host_pool (1.2.1)
4+
active_record_host_pool (1.2.2)
55
activerecord (>= 5.1.0, < 7.1)
66
mysql2
77

gemfiles/rails6.1.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
active_record_host_pool (1.2.1)
4+
active_record_host_pool (1.2.2)
55
activerecord (>= 5.1.0, < 7.1)
66
mysql2
77

gemfiles/rails7.0.gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
active_record_host_pool (1.2.1)
4+
active_record_host_pool (1.2.2)
55
activerecord (>= 5.1.0, < 7.1)
66
mysql2
77

lib/active_record_host_pool/clear_query_cache_patch.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def clear_query_caches_for_current_thread
2929

3030
def clear_on_handler(handler)
3131
handler.all_connection_pools.each do |pool|
32-
db_was = pool.connection.unproxied._host_pool_current_database
33-
pool.connection.clear_query_cache if pool.active_connection?
32+
db_was = pool._unproxied_connection._host_pool_current_database
33+
pool._unproxied_connection.clear_query_cache if pool.active_connection?
3434
ensure
35-
pool.connection.unproxied._host_pool_current_database = db_was
35+
pool._unproxied_connection._host_pool_current_database = db_was
3636
end
3737
end
3838
end

lib/active_record_host_pool/connection_adapter_mixin.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ def self.included(base)
88
base.class_eval do
99
attr_reader(:_host_pool_current_database)
1010

11-
def _host_pool_current_database=(database)
12-
@_host_pool_current_database = database
13-
@config[:database] = _host_pool_current_database
14-
end
15-
1611
alias_method :execute_without_switching, :execute
1712
alias_method :execute, :execute_with_switching
1813

@@ -32,6 +27,11 @@ def initialize(*)
3227
super
3328
end
3429

30+
def _host_pool_current_database=(database)
31+
@_host_pool_current_database = database
32+
@config[:database] = _host_pool_current_database
33+
end
34+
3535
def self.ruby2_keywords(*); end unless respond_to?(:ruby2_keywords, true)
3636
ruby2_keywords def execute_with_switching(*args, **kwargs)
3737
if _host_pool_current_database && !_no_switch

lib/active_record_host_pool/pool_proxy_6_1.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ def __setobj__(pool_config)
3434
attr_reader :pool_config
3535

3636
def connection(*args)
37-
real_connection = _connection_pool.connection(*args)
37+
real_connection = _unproxied_connection(*args)
3838
_connection_proxy_for(real_connection, @config[:database])
3939
rescue Mysql2::Error, ActiveRecord::NoDatabaseError
4040
_connection_pools.delete(_pool_key)
4141
Kernel.raise
4242
end
4343

44+
def _unproxied_connection(*args)
45+
_connection_pool.connection(*args)
46+
end
47+
4448
# by the time we are patched into ActiveRecord, the current thread has already established
4549
# a connection. thus we need to patch both connection and checkout/checkin
4650
def checkout(*args, &block)

0 commit comments

Comments
 (0)