aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_handling.rb
Commit message (Collapse)AuthorAgeFilesLines
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-1/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Treat ActiveRecord::Base and ApplicationRecord as "primary"eileencodes2019-06-051-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | When someone has a multi-db application their `ApplicationRecord` will look like: ```ruby class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to database: { writing: :primary, reading: :replica } end ``` This will cause us to open 2 connections to ActiveRecord::Base's database when we actually only want 1. This is because Rails sees `ApplicationRecord` and thinks it's a new connection, not the existing `ActiveRecord::Base` connection because the `connection_specification_name` is different. This PR changes `ApplicationRecord` classes to consider themselves the same as the "primary" connection. Fixes #36382
* Format a comment to not show up as code [ci skip]Sharang Dashputre2019-04-161-2/+2
|
* Ensure a handler is set when using `connected_to`eileencodes2019-04-081-10/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | After looking at #35800 there is definitely an issue in the `connected_to` method although it's generally behaving. Here are the details: 1) I added a default connection role - writing - to the connection handler lookup. I did this because otherwise if you did this: ``` connected_to(databse: :development) ``` And development wasn't a pre-established role it would create a new handler and connect using that. I don't think this is right so I've updated it to pick up the default (:writing) unless otherwise specified. To set a handler when using the database version pass a hash like you would to `connects_to`: ``` connected_to(database: { readonly_slow: :development }) ``` This will connect the `development` database to the `readonly_slow` handler/role. 2) I updated the tests to match this behavior that we expect. 3) I updated the documentation to clarify that using `connected_to` with a `database` key will establish a new connection every time. This is exactly how `establish_connection` behaves and I feel this is correct. If you want to only establish a connection once you should do that in the model with `connects_to` and then swap on the role instead of on the database hash/key. 4) In regards to #35800 this fixes the case where you pass a symbol to the db and not a hash. But it doesn't fix a case where you may pass an unknown handler to an abstract class that's not connected. This is tricky because technical AbstractFoo doesn't have any connections except for through ApplicationRecord, so in the case of the application that was shared we should only be swapping connections on ActiveRecord::Base because there are no other actual connections - AbstractFoo isn't needed since it's not establishing a new connection. If we need AbstractFoo to connect to a new handler we should establish that connection with the handler in AbstractFoo before trying to shard there.
* Invalidate query cache for all connections in the current threadEileen Uchitelle2019-02-011-0/+9
| | | | | | This change ensures that all query cahces are cleared across all connections per handler for the current thread so if you write on one connection the read will have the query cache cleared.
* Fix error raised when handler doesn't existEileen Uchitelle2019-01-251-4/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | While working on another feature for multiple databases (auto-switching) I observed that in development the first request won't autoload the application record connection for the primary database and may not yet know about the replica connection. In my test application this caused the application to thrown an error if I tried to send the first request to the replica before the replica was connected. This wouldn't be an issue in production because the application is preloaded. In order to fix this I decided to leave the original error message and delete the new error message. I updated the original error message to include the `role` to make it a bit clearer that the connection isn't established for that particular role. The error now reads: ``` No connection pool with 'primary' found for the 'reading' role. ``` A single database application will continue uisng the original error message: ``` No connection pool with 'primary' found. ```
* Raise helpful error when role doesn't existEileen Uchitelle2018-12-211-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | 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/+23
| | | | | This can be used to check the currently connected role. It's meant to mirror AR::Base.connected_to
* Exercise `connected_to` and `connects_to` methodsbogdanvlviv2018-11-151-1/+1
| | | | | | | | | | | | | | | 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
* Fix "warning: shadowing outer local variable - role"Ryuta Kamizono2018-10-311-9/+13
| | | | Caused at #34196.
* Add support for hash and url configs in connected_toGannon McGibbon2018-10-261-6/+9
| | | | | Add support for hash and url configs in database hash of `ActiveRecord::Base.connected_to`.
* Basic API for connection switchingEileen Uchitelle2018-10-101-1/+96
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* Refactors Active Record connection managementEileen Uchitelle2018-08-301-34/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | While the three-tier config makes it easier to define databases for multiple database applications, it quickly became clear to offer full support for multiple databases we need to change the way the connections hash was handled. A three-tier config means that when Rails needed to choose a default configuration (in the case a user doesn't ask for a specific configuration) it wasn't clear to Rails which the default was. I [bandaid fixed this so the rake tasks could work](#32271) but that fix wasn't correct because it actually doubled up the configuration hashes. Instead of attemping to manipulate the hashes @tenderlove and I decided that it made more sense if we converted the hashes to objects so we can easily ask those object questions. In a three tier config like this: ``` development: primary: database: "my_primary_db" animals: database; "my_animals_db" ``` We end up with an object like this: ``` @configurations=[ #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",@spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90 @env_name="development",@spec_name="animals", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}> ]> ``` The configurations setter takes the database configuration set by your application and turns them into an `ActiveRecord::DatabaseConfigurations` object that has one getter - `@configurations` which is an array of all the database objects. The configurations getter returns this object by default since it acts like a hash in most of the cases we need. For example if you need to access the default `development` database we can simply request it as we did before: ``` ActiveRecord::Base.configurations["development"] ``` This will return primary development database configuration hash: ``` { "database" => "my_primary_db" } ``` Internally all of Active Record has been converted to use the new objects. I've built this to be backwards compatible but allow for accessing the hash if needed for a deprecation period. To get the original hash instead of the object you can either add `to_h` on the configurations call or pass `legacy: true` to `configurations. ``` ActiveRecord::Base.configurations.to_h => { "development => { "database" => "my_primary_db" } } ActiveRecord::Base.configurations(legacy: true) => { "development => { "database" => "my_primary_db" } } ``` The new configurations object allows us to iterate over the Active Record configurations without losing the known environment or specification name for that configuration. You can also select all the configs for an env or env and spec. With this we can always ask any object what environment it belongs to: ``` db_configs = ActiveRecord::Base.configurations.configurations_for("development") => #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[ #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",@spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90 @env_name="development",@spec_name="animals", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}> ]> db_config.env_name => "development" db_config.spec_name => "primary" db_config.config => { "adapter"=>"sqlite3", "database"=>"db/development.sqlite3" } ``` The configurations object is more flexible than the configurations hash and will allow us to build on top of the connection management in order to add support for primary/replica connections, sharding, and constructing queries for associations that live in multiple databases.
* Fix connection handling with three-tier configeileencodes2018-03-161-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If you had a three-tier config, the `establish_connection` that's called in the Railtie on load can't figure out how to access the default configuration. This is because Rails assumes that the config is the first value in the hash and always associated with the key from the environment. With a three tier config however we need to go one level deeper. This commit includes 2 changes. 1) removes a line from `resolve_all` which was parsing out the the environment from the config so instead of getting ``` { :development => { :primary => { :database => "whatever" } }, :animals => { :database => "whatever-animals" } }, etc with test / prod } ``` We'd instead end up with a config that had no attachment to it's envioronment. ``` { :primary => { :database => "whatever" } :animals => { :database => "whatever-animals" } etc - without test and prod } ``` Not only did this mean that Active Record didn't know how to establish a connection, it didn't have the other necessary configs along with it in the configs list. So fix this I removed the line that deletes these configs. The second thing this commit changes is adding this line to `establish_connection` ``` spec = spec[spec_name.to_sym] if spec[spec_name.to_sym] ``` When you have a three-tier config and don't pass any hash/symbol/env etc to `establish_connection` the resolver will automatically return both the primary and secondary (in this case animals db) configurations. We'll get an `database configuration does not specify adapter` error because AR will try to establish a connection on the `primary` key rather than the `primary` key's config. It assumes that the `development` or default env automatically will return a config hash, but with a three-tier config we actually get a key and config `primary => config`. This fix is a bit of a bandaid because it's not the "correct" way to handle this situation, but it does solve our immediate problem. The new code here is saying "if the config returned from the resolver (I know it's called spec in here but we interchange our meanings a LOT and what is returned is a three-tier config) has a key matching the "primary" spec name, grab the config from the spec and pass that to the estalbish_connection method". This works because if we pass `:animals` or a hash, or `:primary` we'll already have the correct configuration to connect with. This fixes the case where we want Rail to connect with the default connection. Coming soon is a refactoring that should eliminate the need to do this but I need this fix in order to write the multi-db rake tasks that I promised in my RailsConf submission. `@tenderlove` and I are working on the refactoring of the internals for connection management but it won't be ready for a few weeks and this issue has been blocking progress.
* Flush idle database connectionsMatthew Draper2017-11-261-1/+1
|
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* `DEFAULT_ENV` falls back to `default_env` when `RAILS_ENV` or `RACK_ENV` is ↵Ryuta Kamizono2017-05-251-1/+1
| | | | | | an empty string Follow up of #27399.
* fixes remaining RuboCop issues [Vipul A M, Xavier Noria]Xavier Noria2016-09-011-1/+1
|
* modernizes hash syntax in activerecordXavier Noria2016-08-061-1/+1
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-061-1/+1
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Remove unused `ActiveRecord::Base.connection_id`Sean Griffin2016-06-291-8/+0
| | | | | | | | This method appears to have been partially used in connection pool caching, but it was introduced without much reasoning or any tests. One edge case test was added later on, but it was focused on implementation details. This method is no longer used outside of tests, and as such is removed.
* [ci skip] fix typo in ActiveRecord::ConnectionHandlingAlex Kitchens2016-06-201-1/+1
|
* Remove `name` from `establish_connection`Arthur Neves2016-05-241-1/+6
| | | | | Instead of passing a separete name variable, we can make the resolver merge a name on the config, and use that before creating the Specification.
* Move establish_connection to handlerArthur Neves2016-05-241-13/+5
|
* remove_connection should not remove parent connectionArthur Neves2016-05-111-1/+2
| | | | | | When calling remove_connection in a subclass, that should not fallback to the parent, otherwise it will remove the parent connection from the handler.
* Merge pull request #24971 from arthurnn/arthurnn/dont_cache_specification_nameArthur Nogueira Neves2016-05-111-1/+1
|\ | | | | Dont cache the conn_spec_name when empty
| * Dont cache the conn_spec_name when emptyArthur Neves2016-05-111-1/+1
| | | | | | | | | | | | | | | | We cannot cache the connection_specification_name when it doesnt exist. Thats because the parent value could change, and we should keep failling back to the parent. If we cache that in a children as an ivar, we would not fallback anymore in the next call, so the children would not get the new parent spec_name.
* | Set conn_spec_name after removeArthur Neves2016-05-111-2/+2
|/ | | | | | `remove_connection` can reset the `connection_specification_name`, so we need to to set it after the remove_connection call on `establish_connection` method.
* Make sure we reset the connection_specification_name onArthur Neves2016-05-101-1/+8
| | | | | | | | | | | | | | remove_connection When calling `remove_connection` on a model, we delete the pool so we also need to reset the `connection_specification_name` so it will fallback to the parent. This was the current behavior before rails 5, which will fallback to the parent connection pool. [fixes #24959] Special thanks to @jrafanie for working with me on this fix.
* Followup to #24844Jon Moss2016-05-071-3/+2
| | | | | | | Some slight documentation edits and fixes. Also, run remove unnecessary `RuntimeError`. r? @arthurnn
* s/specification_name/connection_specification_nameArthur Neves2016-05-061-10/+10
|
* s/specification_id/specification_nameArthur Neves2016-05-051-11/+11
|
* Better code readabilityArthur Neves2016-05-051-3/+1
|
* Better specification_id methodArthur Neves2016-05-051-10/+7
|
* Rename methodArthur Neves2016-05-051-3/+3
|
* Refactor connection handlerArthur Neves2016-05-051-7/+27
| | | | | | | | | | | | | | | ConnectionHandler will not have any knowlodge of AR models now, it will only know about the specs. Like that we can decouple the two, and allow the same model to use more than one connection. Historically, folks used to create abstract AR classes on the fly in order to have multiple connections for the same model, and override the connection methods. With this, now we can override the `specificiation_id` method in the model, to return a key, that will be used to find the connection_pool from the handler.
* Remove legacy mysql adapterRyuta Kamizono2015-12-211-1/+1
| | | | Follow up to #22642.
* applies new doc guidelines to Active Record.Yves Senn2015-10-141-3/+3
| | | | | | | | | | | | | | | | | | | The focus of this change is to make the API more accessible. References to method and classes should be linked to make it easy to navigate around. This patch makes exzessiv use of `rdoc-ref:` to provide more readable docs. This makes it possible to document `ActiveRecord::Base#save` even though the method is within a separate module `ActiveRecord::Persistence`. The goal here is to bring the API closer to the actual code that you would write. This commit only deals with Active Record. The other gems will be updated accordingly but in different commits. The pass through Active Record is not completely finished yet. A follow up commit will change the spots I haven't yet had the time to update. /cc @fxn
* Improve readability of docs by using code tag [ci skip]Prakash Laxkar2015-09-291-1/+1
|
* AR::ConPool - remove synchronization around connection cache.thedarkone2015-05-141-1/+1
| | | | | | | | | | | | | | | | | | | | Renamed `@reserved_connections` -> `@thread_cached_conns`. New name clearly conveys the purpose of the cache, which is to speed-up `#connection` method. The new `@thread_cached_conns` now also uses `Thread` objects as keys (instead of previously `Thread.current.object_id`). Since there is no longer any synchronization around `@thread_cached_conns`, `disconnect!` and `clear_reloadable_connections!` methods now pre-emptively obtain ownership (via `checkout`) of all existing connections, before modifying internal data structures. A private method `release` has been renamed `thread_conn_uncache` to clear-up its purpose. Fixed some brittle `thread.status == "sleep"` tests (threads can go into sleep even without locks).
* Revert "Merge pull request #19404 from dmathieu/remove_rack_env"Jeremy Kemper2015-03-201-1/+1
| | | | | | | Preserving RACK_ENV behavior. This reverts commit 7bdc7635b885e473f6a577264fd8efad1c02174f, reversing changes made to 45786be516e13d55a1fca9a4abaddd5781209103.
* don't fallback to RACK_ENV when RAILS_ENV is not presentDamien Mathieu2015-03-191-1/+1
|
* Spell PostgreSQL correctly :elephant:Akira Matsuda2015-02-281-1/+1
| | | | [ci skip]
* Check for Rails.env instead of RailsPavel Penkov2014-12-251-1/+1
| | | | | `Rails` constant is added by rails-html-sanitizer leading to bugs in non-Rails apps using ActiveRecord and ActionMailer
* If Rails is not defined, check ENV["RAILS_ENV"] and ENV["RACK_ENV"].Keith Gable2014-10-211-1/+1
| | | | | | | This fixes a regression introduced by 6cc03675d30b58e28f585720dad14e947a57ff5b. ActiveRecord, if used without Rails, always checks the "default_env" environment. This would be OK, except that Sinatra also supports environments, and it runs with {RACK|RAILS}_ENV=production. This patch adds a fallback to RAILS_ENV and RACK_ENV (and ultimately default_env) if Rails.env doesn't exist.
* Don't deprecate after allMatthew Draper2014-04-091-16/+2
|
* Less ambition, more deprecationMatthew Draper2014-04-091-16/+16
| | | | | | | | The "DATABASE_URL_*" idea was moving in the wrong direction. Instead, let's deprecate the situation where we end up using ENV['DATABASE_URL'] at all: the Right Way is to explicitly include it in database.yml with ERB.
* Rearrange the config merger some moreMatthew Draper2014-04-081-19/+16
| | | | | | | This seems to simplify the operative part. Most importantly, by pre-loading all the configs supplied in ENV, we ensure the list is complete: if the developer specifies an unknown config, the exception includes a list of valid ones.