aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/connection_specification
Commit message (Collapse)AuthorAgeFilesLines
* Refactors Active Record connection managementEileen Uchitelle2018-08-301-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Distinguish missing adapter gems from load errors within the adapterJeremy Daer2017-10-081-1/+1
| | | | | | | | * When the adapter is missing, raise an exception that points out config typos and missing Gemfile entries. (We can assume that a non-builtin adapter was used since these are always available.) * When loading an adapter raises a LoadError, prefix its error message to indicate that the adapter is likely missing an optional dependency.
* 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
|
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-2/+2
|
* Add three new rubocop rulesRafael Mendonça França2016-08-161-8/+8
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* remove redundant curlies from hash argumentsXavier Noria2016-08-061-1/+1
|
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-21/+21
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Remove `name` from `establish_connection`Arthur Neves2016-05-241-4/+8
| | | | | 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.
* s/specification_id/specification_nameArthur Neves2016-05-051-4/+4
|
* Add spec_id testsArthur Neves2016-05-051-0/+9
|
* Add a resolver test for the missing schemeJeremy Baker2016-02-041-0/+6
|
* Complete change of `sqlite3:///` path handlingMatthew Draper2014-04-031-2/+2
| | | | That which was now relative is now absolute.
* Revise 'sqlite3:' URL handling for smoother upgradesMatthew Draper2014-04-031-4/+23
| | | | | | | | | | | | | | | | Restore the 4.0 behaviour for 'sqlite3:///', but deprecate it. We'll change to the absolute-path interpretation in 4.2. The current "correct" spellings for in-memory, relative, and absolute URLs, respectively, are: sqlite3::memory: sqlite3:relative/path sqlite3:/full/path Substantially reverses/defers fbb79b517f3127ba620fedd01849f9628b78d6ce. Uncovered by @guilleiguaran while investigating #14495, though that sounds like a different issue.
* Allow "url" sub key in database.yml configurationschneems2013-12-301-0/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently a developer can pass in a YAML configuration that fully specifies connection information: ``` production: database: triage_production adapter: password pool: 5 ``` They can also pass in a string that specifies a connection URL directly to an environment key: ``` production: postgresql://localhost/foo ``` This PR allows the use of both a connection url and specifying connection attributes via YAML through the use of the "url" sub key: ``` production: url: postgresql://localhost/foo pool: 3 ``` This will allow developers to inherit Active Record options such as `pool` from `&defaults` and still use a secure connection url such as `<%= ENV['DATABASE_URL'] %>`. The URL is expanded into a hash and then merged back into the YAML hash. If there are any conflicts, the values from the connection URL are preferred. Talked this over with @josevalim
* Only build a ConnectionSpecification if requiredJosé Valim2013-12-241-28/+26
|
* Guarantee the connection resolver handles string valuesJosé Valim2013-12-231-2/+10
| | | | | | | | | This commit also cleans up the rake tasks that were checking for DATABASE_URL in different places. In fact, it would be nice to deprecate DATABASE_URL usage in the long term, considering the direction we are moving of allowing those in .yml files.
* added test cases for Resolver#connection_url_to_hashKuldeep Aggarwal2013-12-231-0/+10
|
* Do not type cast all the database url values.Rafael Mendonça França2013-02-241-21/+0
| | | | | | We should only type cast when we need to use. Related to 4b005fb371c2e7af80df7da63be94509b1db038c
* Whitespace :heart: and hash syntax changeRafael Mendonça França2013-02-201-23/+22
|
* descriptive error message when AR adapter was not found. Closes #7313.Yves Senn2013-02-201-0/+9
|
* DATABASE_URL parsing should turn numeric strings into numeric types, andAaron Stone2013-01-311-11/+37
| | | | | | the strings true and false into boolean types, in order to match how YAML would parse the same values from database.yml and prevent unexpected type errors in the database adapters.
* Standardize the use of current_adapter?Rafael Mendonça França2013-01-011-4/+4
|
* Fix the skip code.Rafael Mendonça França2012-10-281-4/+4
| | | | Checking for the constant doesn't work
* Decode attributes pulled from URI.parseShawn Veader2012-10-261-0/+8
| | | | | | | The RFC indicates that username and passwords may be encoded. http://tools.ietf.org/html/rfc2396#section-3.2.2 Found this trying to use the mysql://username:password@host:port/db and having special characters in the password which needed to be URI encoded.
* fixed support for DATABASE_URL for rake db tasksGrace Liu2012-09-111-2/+0
| | | | | | | | | | | | - added tests to confirm establish_connection uses DATABASE_URL and Rails.env correctly even when no arguments are passed in. - updated rake db tasks to support DATABASE_URL, and added tests to confirm correct behavior for these rake tasks. (Removed establish_connection call from some tasks since in those cases the :environment task already made sure the function would be called) - updated Resolver so that when it resolves the database url, it removes hash values with empty strings from the config spec (e.g. to support connection to postgresql when no username is specified).
* The resolver tests fail if the mysql gem is not installed; this breaksDavid Paschich2012-06-031-0/+3
| | | | | | | | being able to run the sqlite3 tests without that gem. This fix ensures the tests will only run if the mysql gem is installed. A better solution might be to move these tests into the per-adapter tests, and test each adapter's resolver.
* Support establishing connection on ActiveRecord::Model.Jon Leighton2011-12-281-1/+1
| | | | | This is the 'top level' connection, inherited by any models that include ActiveRecord::Model or inherit from ActiveRecord::Base.
* I herd you like modules.Jon Leighton2011-12-241-1/+1
|
* respond_to? information of AR is not the responsibility of the specAaron Patterson2011-11-291-1/+1
| | | | resolver.
* Fix resolver_test.rb on travis (postgresql isn't setup, so it can't load the ↵Jon Leighton2011-11-291-6/+6
| | | | connection adapter)
* Move connection resoluion logic to it's own testable class.Aaron Patterson2011-11-281-0/+41