aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Add assertions for `ActiveRecord::Base.current_role`Ryuta Kamizono2019-01-021-0/+6
| | | | Since the `current_role` is public API.
* Raise helpful error when role doesn't existEileen Uchitelle2018-12-211-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | If you try to call `connected_to` with a role that doesn't have an established connection you used to get an error that said: ``` >> ActiveRecord::Base.connected_to(role: :i_dont_exist) { Home.first } ActiveRecord::ConnectionNotEstablished Exception: No connection pool with 'primary' found. ``` This is confusing because the connection could be established but we spelled the role wrong. I've changed this to raise if the `role` used in `connected_to` doesn't have an associated handler. Users who encounter this should either check that the role is spelled correctly (writin -> writing), establish a connection to that role in the model with connects_to, or use the `database` keyword for the `role`. I think this will provide a less confusing error message for those starting out with multiple databases.
* Add AR::Base.connected_to?John Hawthorn2018-12-111-0/+11
| | | | | This can be used to check the currently connected role. It's meant to mirror AR::Base.connected_to
* Make connection handler per thread instead of per fiberEileen Uchitelle2018-11-201-0/+37
| | | | | | | | | | | | | | | The connection handler was using the RuntimeRegistry which kind of implies it's a per thread registry. But it's actually per fiber. If you have an application that uses fibers and you're using multiple databases, when you switch the connection handler to swap connections new fibers running on the same thread used to get a different connection id. This PR changes the code to actually use a thread so that we get the same connection. Fixes https://github.com/rails/rails/issues/30047 [Eileen M. Uchitelle, Aaron Patterson, & Arthur Neeves]
* Exercise `connected_to` and `connects_to` methodsbogdanvlviv2018-11-151-0/+35
| | | | | | | | | | | | | | | Since both methods are public API I think it makes sense to add these tests in order to prevent any regression in the behavior of those methods after the 6.0 release. Exercise `connected_to` - Ensure that the method raises with both `database` and `role` arguments - Ensure that the method raises without `database` and `role` Exercise `connects_to` - Ensure that the method returns an array of established connections(as mentioned in the docs of the method) Related to #34052
* Ensure that `ActiveRecord::Base#connected_to` with `:database` establishes ↵bogdanvlviv2018-11-121-3/+21
| | | | | | connection Related to #34052
* Add support for hash and url configs in connected_toGannon McGibbon2018-10-261-0/+39
| | | | | Add support for hash and url configs in database hash of `ActiveRecord::Base.connected_to`.
* Basic API for connection switchingEileen Uchitelle2018-10-101-0/+193
This PR adds the ability to 1) connect to multiple databases in a model, and 2) switch between those connections using a block. To connect a model to a set of databases for writing and reading use the following API. This API supercedes `establish_connection`. The `writing` and `reading` keys represent handler / role names and `animals` and `animals_replica` represents the database key to look up the configuration hash from. ``` class AnimalsBase < ApplicationRecord connects_to database: { writing: :animals, reading: :animals_replica } end ``` Inside the application - outside the model declaration - we can switch connections with a block call to `connected_to`. If we want to connect to a db that isn't default (ie readonly_slow) we can connect like this: Outside the model we may want to connect to a new database (one that is not in the default writing/reading set) - for example a slow replica for making slow queries. To do this we have the `connected_to` method that takes a `database` hash that matches the signature of `connects_to`. The `connected_to` method also takes a block. ``` AcitveRecord::Base.connected_to(database: { slow_readonly: :primary_replica_slow }) do ModelInPrimary.do_something_thats_slow end ``` For models that are already loaded and connections that are already connected, `connected_to` doesn't need to pass in a `database` because you may want to run queries against multiple databases using a specific role/handler. In this case `connected_to` can take a `role` and use that to swap on the connection passed. This simplies queries - and matches how we do it in GitHub. Once you're connected to the database you don't need to re-connect, we assume the connection is in the pool and simply pass the handler we'd like to swap on. ``` ActiveRecord::Base.connected_to(role: :reading) do Dog.read_something_from_dog ModelInPrimary.do_something_from_model_in_primary end ```