rails/activerecord/CHANGELOG.md

147 lines
4.3 KiB
Markdown
Raw Normal View History

Refactors Active Record connection management 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.
2018-08-16 19:49:18 +00:00
* ActiveRecord::Base.configurations now returns an object.
ActiveRecord::Base.configurations used to return a hash, but this
is an inflexible data model. In order to improve multiple-database
handling in Rails, we've changed this to return an object. Some methods
are provided to make the object behave hash-like in order to ease the
transition process. Since most applications don't manipulate the hash
we've decided to add backwards-compatible functionality that will throw
a deprecation warning if used, however calling `ActiveRecord::Base.configurations`
will use the new version internally and externally.
For example, the following database.yml...
```
development:
adapter: sqlite3
database: db/development.sqlite3
```
Used to become a hash:
```
{ "development" => { "adapter" => "sqlite3", "database" => "db/development.sqlite3" } }
```
Is now converted into the following object:
```
#<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800 @configurations=[
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
@spec_name="primary", @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>
]
```
Iterating over the database configurations has also changed. Instead of
calling hash methods on the `configurations` hash directly, a new method `configs_for` has
been provided that allows you to select the correct configuration. `env_name` is a required
argument, `spec_name` is optional as well as passing a block. These return an array of
database config objects for the requested environment and specification name respectively.
```
ActiveRecord::Base.configurations.configs_for("development")
ActiveRecord::Base.configurations.configs_for("development", "primary")
```
*Eileen M. Uchitelle*, *Aaron Patterson*
* Add database configuration to disable advisory locks.
```
production:
adapter: postgresql
advisory_locks: false
```
*Guo Xiang*
* SQLite3 adapter `alter_table` method restores foreign keys.
*Yasuo Honda*
* Allow `:to_table` option to `invert_remove_foreign_key`.
Example:
remove_foreign_key :accounts, to_table: :owners
*Nikolay Epifanov*, *Rich Chen*
* Add environment & load_config dependency to `bin/rake db:seed` to enable
seed load in environments without Rails and custom DB configuration
*Tobias Bielohlawek*
* Fix default value for mysql time types with specified precision.
*Nikolay Kondratyev*
* Fix `touch` option to behave consistently with `Persistence#touch` method.
*Ryuta Kamizono*
* Migrations raise when duplicate column definition.
Fixes #33024.
*Federico Martinez*
2018-06-10 11:39:10 +00:00
* Bump minimum SQLite version to 3.8
*Yasuo Honda*
* Fix parent record should not get saved with duplicate children records.
Fixes #32940.
*Santosh Wadghule*
* Fix logic on disabling commit callbacks so they are not called unexpectedly when errors occur.
*Brian Durand*
* Ensure `Associations::CollectionAssociation#size` and `Associations::CollectionAssociation#empty?`
use loaded association ids if present.
*Graham Turner*
* Add support to preload associations of polymorphic associations when not all the records have the requested associations.
*Dana Sherson*
* Add `touch_all` method to `ActiveRecord::Relation`.
Example:
Person.where(name: "David").touch_all(time: Time.new(2020, 5, 16, 0, 0, 0))
*fatkodima*, *duggiefresh*
* Add `ActiveRecord::Base.base_class?` predicate.
2018-04-02 11:17:24 +00:00
*Bogdan Gusiev*
* Add custom prefix/suffix options to `ActiveRecord::Store.store_accessor`.
*Tan Huynh*, *Yukio Mizuta*
* Rails 6 requires Ruby 2.4.1 or newer.
*Jeremy Daer*
* Deprecate `update_attributes`/`!` in favor of `update`/`!`.
*Eddie Lebow*
* Add ActiveRecord::Base.create_or_find_by/! to deal with the SELECT/INSERT race condition in
ActiveRecord::Base.find_or_create_by/! by leaning on unique constraints in the database.
*DHH*
* Add `Relation#pick` as short-hand for single-value plucks.
*DHH*
Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activerecord/CHANGELOG.md) for previous changes.