Skip to content

Commit

Permalink
Remove change_db as Trilogy switching method
Browse files Browse the repository at this point in the history
Trilogy now has `select_db` as an alias for `change_db` which is the
same method name that `mysql2` uses. Instead of conditionally
determining the method name to use we can now just use the same method
for both adapters.
  • Loading branch information
HeyNonster committed Aug 21, 2023
1 parent bd6584b commit 07db120
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ and as of v1.0.0 this project adheres to [Semantic Versioning](https://semver.or

## [Unreleased]

- Remove `mysql2` as a direct dependency, test Rails 7.0 with `mysql2` and `activerecord-trilogy-adapter`
- `Trilogy` is now a supported MySQL database adapter. ActiveRecordHostPool no longer requires `mysql2`, nor does it explicitly require `activerecord-trilogy-adapter`. Applications using ARHP will now need to explicitly require one of these adapters in its gemfile. When using `activerecord-trilogy-adapter` also ensure that the `trilogy` gem is locked to `v2.5.0+`.
- Remove `mysql2` as a direct dependency, test Rails 7.0 with `mysql2` and `activerecord-trilogy-adapter`.

## [1.2.5] - 2023-07-14
### Added
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_pool_2 => 127.0.0.1/3306//root/false

## Support

For now, the only backend known to work is MySQL, with the mysql2 or activerecord-trilogy-adapter gem.
For now, the only backend known to work is MySQL, with the mysql2 or activerecord-trilogy-adapter gem. When using the activerecord-trilogy-adapter ensure that the transitive dependency Trilogy is v2.5.0+.
Postgres, from an informal reading of the docs, will never support the concept of one server connection sharing multiple dbs.

## Installation
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails7.0_trilogy.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ gemspec path: '../'

gem 'activerecord', '~> 7.0.0'
gem 'activerecord-trilogy-adapter', '>= 3.0'
gem 'trilogy', '>= 2.5.0'

eval_gemfile 'common.rb'
3 changes: 2 additions & 1 deletion gemfiles/rails7.0_trilogy.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.7)
ruby-progressbar (1.13.0)
trilogy (2.4.1)
trilogy (2.5.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (1.6.1)
Expand All @@ -81,6 +81,7 @@ DEPENDENCIES
pry-byebug (~> 3.9)
rake (>= 12.0.0)
rubocop (~> 0.80.0)
trilogy (>= 2.5.0)

BUNDLED WITH
2.4.13
24 changes: 2 additions & 22 deletions lib/active_record_host_pool/connection_adapter_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@

module ActiveRecordHostPool
module DatabaseSwitch
case ActiveRecordHostPool.loaded_db_adapter
when :mysql2
DB_SWITCHING_METHOD = "select_db"
when :trilogy
DB_SWITCHING_METHOD = "change_db"
end

def self.included(base)
base.class_eval do
attr_reader(:_host_pool_current_database)
Expand Down Expand Up @@ -87,28 +80,15 @@ def _switch_connection
(_host_pool_current_database != @_cached_current_database) ||
@connection.object_id != @_cached_connection_object_id
)
log("#{DB_SWITCHING_METHOD} #{_host_pool_current_database}", "SQL") do
log("select_db #{_host_pool_current_database}", "SQL") do
clear_cache!
_arhp_select_db(_host_pool_current_database)
raw_connection.select_db(_host_pool_current_database)
end
@_cached_current_database = _host_pool_current_database
@_cached_connection_object_id = @connection.object_id
end
end

case ActiveRecordHostPool.loaded_db_adapter
when :mysql2
def _arhp_select_db(database)
raw_connection.select_db(database)
end
when :trilogy
# rubocop:disable Lint/DuplicateMethods
def _arhp_select_db(database)
raw_connection.change_db(database)
end
# rubocop:enable Lint/DuplicateMethods
end

# prevent different databases from sharing the same query cache
def cache_sql(sql, *args)
super(_host_pool_current_database.to_s + "/" + sql, *args)
Expand Down
4 changes: 2 additions & 2 deletions test/test_arhp_with_non_legacy_connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_correctly_writes_to_sharded_databases_and_only_switches_dbs_when_necess
# Assert that we switched, and only switched, in the order we expected.
# If this assertion starts to fail, Rails is likely calling `#connection`
# somewhere new, and we should investigate
db_switches = new_logger.string.scan(/(?:select_db|change_db) (\w+)/).flatten
db_switches = new_logger.string.scan(/select_db (\w+)/).flatten
assert_equal ['arhp_test_db_shard_b', 'arhp_test_db_shard_c', 'arhp_test_db_shard_b'], db_switches

new_logger.string = +''
Expand All @@ -83,7 +83,7 @@ def test_correctly_writes_to_sharded_databases_and_only_switches_dbs_when_necess

# If this assertion starts to fail, Rails is likely calling `#connection`
# somewhere new, and we should investigate.
db_switches = new_logger.string.scan(/(?:select_db|change_db) (\w+)/).flatten
db_switches = new_logger.string.scan(/select_db (\w+)/).flatten
assert_equal ['arhp_test_db_shard_c'], db_switches

assert_equal [3, 1, 2], [records_on_shard_b, records_on_shard_c, records_on_shard_d]
Expand Down

0 comments on commit 07db120

Please sign in to comment.