diff options
author | Gannon McGibbon <gannon.mcgibbon@gmail.com> | 2018-10-11 17:54:56 -0400 |
---|---|---|
committer | Gannon McGibbon <gannon.mcgibbon@gmail.com> | 2018-10-26 16:27:25 -0400 |
commit | abf51844bb598ffdd2e6b8aa722b75b5897d9a3f (patch) | |
tree | 75adb5fce26f2c9c43614dd765af999300664b1e | |
parent | 5431e17733366da1fd10f2cd3039d66a56012683 (diff) | |
download | rails-abf51844bb598ffdd2e6b8aa722b75b5897d9a3f.tar.gz rails-abf51844bb598ffdd2e6b8aa722b75b5897d9a3f.tar.bz2 rails-abf51844bb598ffdd2e6b8aa722b75b5897d9a3f.zip |
Add support for hash and url configs in connected_to
Add support for hash and url configs in database hash
of `ActiveRecord::Base.connected_to`.
-rw-r--r-- | activerecord/CHANGELOG.md | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_handling.rb | 15 | ||||
-rw-r--r-- | activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb | 39 |
3 files changed, 63 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 0d9bc3cec7..d4f6e6ae48 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,18 @@ +* Add support for hash and url configs in database hash of `ActiveRecord::Base.connected_to`. + + ```` + User.connected_to(database: { writing: "postgres://foo" }) do + User.create!(name: "Gannon") + end + + config = { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" } + User.connected_to(database: { reading: config }) do + User.count + end + ```` + + *Gannon McGibbon* + * Support default expression for MySQL. MySQL 8.0.13 and higher supports default value to be a function or expression. diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index 5141271db9..e3b59b8a22 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -109,12 +109,15 @@ module ActiveRecord if database && role raise ArgumentError, "connected_to can only accept a database or role argument, but not both arguments." elsif database - config_hash = resolve_config_for_connection(database) - handler = lookup_connection_handler(database.to_sym) - - with_handler(database.to_sym) do - handler.establish_connection(config_hash) - return yield + database = { database => database } if database.is_a?(Symbol) + database.each do |role, database_key| + config_hash = resolve_config_for_connection(database_key) + handler = lookup_connection_handler(role.to_sym) + + with_handler(role.to_sym) do + handler.establish_connection(config_hash) + return yield + end end elsif role with_handler(role.to_sym, &blk) diff --git a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb index d4e8cbee81..a520fb1303 100644 --- a/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb @@ -120,6 +120,45 @@ module ActiveRecord ENV["RAILS_ENV"] = previous_env end + def test_switching_connections_with_database_url + previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env" + previous_url, ENV["DATABASE_URL"] = ENV["DATABASE_URL"], "postgres://localhost/foo" + + ActiveRecord::Base.connected_to(database: { writing: "postgres://localhost/bar" }) do + handler = ActiveRecord::Base.connection_handler + assert_equal handler, ActiveRecord::Base.connection_handlers[:writing] + end + ensure + ActiveRecord::Base.establish_connection(:arunit) + ENV["RAILS_ENV"] = previous_env + ENV["DATABASE_URL"] = previous_url + end + + def test_switching_connections_with_database_config_hash + previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env" + config = { "adapter" => "sqlite3", "database" => "db/readonly.sqlite3" } + + ActiveRecord::Base.connected_to(database: { writing: config }) do + handler = ActiveRecord::Base.connection_handler + assert_equal handler, ActiveRecord::Base.connection_handlers[:writing] + end + ensure + ActiveRecord::Base.establish_connection(:arunit) + ENV["RAILS_ENV"] = previous_env + end + + def test_switching_connections_with_database_symbol + previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "default_env" + + ActiveRecord::Base.connected_to(database: :arunit2) do + handler = ActiveRecord::Base.connection_handler + assert_equal handler, ActiveRecord::Base.connection_handlers[:arunit2] + end + ensure + ActiveRecord::Base.establish_connection(:arunit) + ENV["RAILS_ENV"] = previous_env + end + def test_connects_to_with_single_configuration config = { "development" => { "adapter" => "sqlite3", "database" => "db/primary.sqlite3" }, |