2024-05-11 16:23:01 +08:00
* Support `touch_all` in batches.
```ruby
Post.in_batches.touch_all
```
*fatkodima*
2024-05-11 21:40:20 +08:00
* Add support for `:if_not_exists` and `:force` options to `create_schema`
*fatkodima*
2023-07-02 14:05:15 +08:00
* Fix `index_errors` having incorrect index in association validation errors.
*lulalala*
* Add `index_errors: :nested_attributes_order` mode.
This indexes the association validation errors based on the order received by nested attributes setter, and respects the `reject_if` configuration. This enables API to provide enough information to the frontend to map the validation errors back to their respective form fields.
*lulalala*
2024-04-09 22:51:38 +08:00
* Association option `query_constraints` is deprecated in favor of `foreign_key` .
*Nikita Vasilevsky*
2024-05-07 08:04:01 +08:00
* Add ENV["SKIP_TEST_DATABASE_TRUNCATE"] flag to speed up multi-process test runs on large DBs when all tests run within default txn. (This cuts ~10s from the test run of HEY when run by 24 processes against the 178 tables, since ~4,000 table truncates can then be skipped.)
*DHH*
2024-05-02 19:53:21 +08:00
* Added support for recursive common table expressions.
2024-04-18 23:12:51 +08:00
```ruby
Post.with_recursive(
post_and_replies: [
Post.where(id: 42),
Post.joins('JOIN post_and_replies ON posts.in_reply_to_id = post_and_replies.id'),
]
)
```
Generates the following SQL:
```sql
WITH RECURSIVE "post_and_replies" AS (
(SELECT "posts".* FROM "posts" WHERE "posts"."id" = 42)
UNION ALL
(SELECT "posts".* FROM "posts" JOIN post_and_replies ON posts.in_reply_to_id = post_and_replies.id)
)
SELECT "posts".* FROM "posts"
```
*ClearlyClaire*
2024-05-02 05:33:06 +08:00
* `validate_constraint` can be called in a `change_table` block.
ex:
```ruby
change_table :products do |t|
t.check_constraint "price > discounted_price", name: "price_check", validate: false
t.validate_check_constraint "price_check"
end
```
*Cody Cutrer*
2024-04-04 07:14:54 +08:00
* `PostgreSQLAdapter` now decodes columns of type date to `Date` instead of string.
Ex:
```ruby
ActiveRecord::Base.connection
.select_value("select '2024-01-01'::date").class #=> Date
```
*Joé Dupuis*
2023-07-23 00:32:21 +08:00
* Strict loading using `:n_plus_one_only` does not eagerly load child associations.
With this change, child associations are no longer eagerly loaded, to
match intended behavior and to prevent non-deterministic order issues caused
by calling methods like `first` or `last` . As `first` and `last` don't cause
an N+1 by themselves, calling child associations will no longer raise.
Fixes #49473 .
Before:
```ruby
person = Person.find(1)
person.strict_loading!(mode: :n_plus_one_only)
person.posts.first
# SELECT * FROM posts WHERE person_id = 1; -- non-deterministic order
person.posts.first.firm # raises ActiveRecord::StrictLoadingViolationError
```
After:
```ruby
person = Person.find(1)
person.strict_loading!(mode: :n_plus_one_only)
person.posts.first # this is 1+1, not N+1
# SELECT * FROM posts WHERE person_id = 1 ORDER BY id LIMIT 1;
person.posts.first.firm # no longer raises
```
*Reid Lynch*
2024-04-18 10:14:56 +08:00
* Allow `Sqlite3Adapter` to use `sqlite3` gem version `2.x`
*Mike Dalessio*
2024-04-14 02:39:09 +08:00
* Allow `ActiveRecord::Base#pluck` to accept hash values
```ruby
# Before
Post.joins(:comments).pluck("posts.id", "comments.id", "comments.body")
# After
Post.joins(:comments).pluck(posts: [:id], comments: [:id, :body])
```
*fatkodima*
2024-04-03 23:32:24 +08:00
* Raise an `ActiveRecord::ActiveRecordError` error when the MySQL database returns an invalid version string.
*Kevin McPhillips*
2024-04-08 18:19:29 +08:00
* `ActiveRecord::Base.transaction` now yields an `ActiveRecord::Transaction` object.
Allow to register transaction callbacks outside of a record
Ref: https://github.com/rails/rails/pull/26103
Ref: https://github.com/rails/rails/pull/51426
A fairly common mistake with Rails is to enqueue a job from inside a
transaction, and a record as argumemnt, which then lead to a RecordNotFound
error when picked up by the queue.
This is even one of the arguments advanced for job runners backed by the
database such as `solid_queue`, `delayed_job` or `good_job`. But relying
on this is undesirable iin my opinion as it makes the Active Job abstraction
leaky, and if in the future you need to migrate to another backend or even
just move the queue to a separate database, you may experience a lot of race
conditions of the sort.
But more generally, being able to defer work to after the current transaction
has been a missing feature of Active Record. Right now the only way to do it
is from a model callback, and this forces moving things in Active Record
models that sometimes are better done elsewhere. Even as a self-proclaimed
"service object skeptic", I often wanted this capability over the last decade,
and I'm sure it got asked or desired by many more people.
Also there's some 3rd party gems adding this capability using monkey patches.
It's not a reason to upstream the capability, but it's a proof that there is
demand for it.
Implementation wise, this proof of concept shows that it's not really hard to
implement, even with nested multi-db transactions support.
Co-Authored-By: Cristian Bica <cristian.bica@gmail.com>
2024-03-26 21:21:18 +08:00
This allows to register callbacks on it.
```ruby
Article.transaction do |transaction|
article.update(published: true)
transaction.after_commit do
PublishNotificationMailer.with(article: article).deliver_later
end
end
```
*Jean Boussier*
* Add `ActiveRecord::Base.current_transaction` .
Returns the current transaction, to allow registering callbacks on it.
```ruby
Article.current_transaction.after_commit do
PublishNotificationMailer.with(article: article).deliver_later
end
```
*Jean Boussier*
* Add `ActiveRecord.after_all_transactions_commit` callback.
2024-04-08 21:58:14 +08:00
Useful for code that may run either inside or outside a transaction and needs
to perform work after the state changes have been properly persisted.
Allow to register transaction callbacks outside of a record
Ref: https://github.com/rails/rails/pull/26103
Ref: https://github.com/rails/rails/pull/51426
A fairly common mistake with Rails is to enqueue a job from inside a
transaction, and a record as argumemnt, which then lead to a RecordNotFound
error when picked up by the queue.
This is even one of the arguments advanced for job runners backed by the
database such as `solid_queue`, `delayed_job` or `good_job`. But relying
on this is undesirable iin my opinion as it makes the Active Job abstraction
leaky, and if in the future you need to migrate to another backend or even
just move the queue to a separate database, you may experience a lot of race
conditions of the sort.
But more generally, being able to defer work to after the current transaction
has been a missing feature of Active Record. Right now the only way to do it
is from a model callback, and this forces moving things in Active Record
models that sometimes are better done elsewhere. Even as a self-proclaimed
"service object skeptic", I often wanted this capability over the last decade,
and I'm sure it got asked or desired by many more people.
Also there's some 3rd party gems adding this capability using monkey patches.
It's not a reason to upstream the capability, but it's a proof that there is
demand for it.
Implementation wise, this proof of concept shows that it's not really hard to
implement, even with nested multi-db transactions support.
Co-Authored-By: Cristian Bica <cristian.bica@gmail.com>
2024-03-26 21:21:18 +08:00
```ruby
def publish_article(article)
article.update(published: true)
ActiveRecord.after_all_transactions_commit do
PublishNotificationMailer.with(article: article).deliver_later
end
end
```
In the above example, the block is either executed immediately if called outside
of a transaction, or called after the open transaction is committed.
If the transaction is rolled back, the block isn't called.
*Jean Boussier*
2024-04-08 21:58:14 +08:00
* Add the ability to ignore counter cache columns until they are backfilled.
2024-03-31 01:17:29 +08:00
Starting to use counter caches on existing large tables can be troublesome, because the column
values must be backfilled separately of the column addition (to not lock the table for too long)
and before the use of `:counter_cache` (otherwise methods like `size` /`any?`/etc, which use
counter caches internally, can produce incorrect results). People usually use database triggers
or callbacks on child associations while backfilling before introducing a counter cache
configuration to the association.
Now, to safely backfill the column, while keeping the column updated with child records added/removed, use:
```ruby
class Comment < ApplicationRecord
belongs_to :post, counter_cache: { active: false }
end
```
While the counter cache is not "active", the methods like `size` /`any?`/etc will not use it,
but get the results directly from the database. After the counter cache column is backfilled, simply
remove the `{ active: false }` part from the counter cache definition, and it will now be used by the
mentioned methods.
*fatkodima*
2024-04-08 21:58:14 +08:00
* Retry known idempotent SELECT queries on connection-related exceptions.
2024-03-16 04:39:12 +08:00
SELECT queries we construct by walking the Arel tree and / or with known model attributes
are idempotent and can safely be retried in the case of a connection error. Previously,
adapters such as `TrilogyAdapter` would raise `ActiveRecord::ConnectionFailed: Trilogy::EOFError`
when encountering a connection error mid-request.
*Adrianna Chang*
2024-03-25 21:19:42 +08:00
* Allow association's `foreign_key` to be composite.
2024-03-25 09:24:18 +08:00
`query_constraints` option was the only way to configure a composite foreign key by passing an `Array` .
Now it's possible to pass an Array value as `foreign_key` to achieve the same behavior of an association.
*Nikita Vasilevsky*
2024-03-25 21:19:42 +08:00
* Allow association's `primary_key` to be composite.
2024-03-25 09:24:18 +08:00
Association's `primary_key` can be composite when derived from associated model `primary_key` or `query_constraints` .
Now it's possible to explicitly set it as composite on the association.
*Nikita Vasilevsky*
2024-03-19 18:41:53 +08:00
* Add `config.active_record.permanent_connection_checkout` setting.
Controls whether `ActiveRecord::Base.connection` raises an error, emits a deprecation warning, or neither.
2024-03-25 21:19:42 +08:00
`ActiveRecord::Base.connection` checkouts a database connection from the pool and keeps it leased until the end of
2024-03-19 18:41:53 +08:00
the request or job. This behavior can be undesirable in environments that use many more threads or fibers than there
is available connections.
This configuration can be used to track down and eliminate code that calls `ActiveRecord::Base.connection` and
migrate it to use `ActiveRecord::Base.with_connection` instead.
The default behavior remains unchanged, and there is currently no plans to change the default.
*Jean Boussier*
2024-03-25 21:19:42 +08:00
* Add dirties option to uncached.
2024-03-03 11:01:24 +08:00
This adds a `dirties` option to `ActiveRecord::Base.uncached` and
`ActiveRecord::ConnectionAdapters::ConnectionPool#uncached` .
When set to `true` (the default), writes will clear all query caches belonging to the current thread.
When set to `false` , writes to the affected connection pool will not clear any query cache.
This is needed by Solid Cache so that cache writes do not clear query caches.
*Donal McBreen*
2024-03-25 21:19:42 +08:00
* Deprecate `ActiveRecord::Base.connection` in favor of `.lease_connection` .
2024-03-01 19:18:50 +08:00
The method has been renamed as `lease_connection` to better reflect that the returned
connection will be held for the duration of the request or job.
This deprecation is a soft deprecation, no warnings will be issued and there is no
current plan to remove the method.
*Jean Boussier*
2024-03-25 21:19:42 +08:00
* Deprecate `ActiveRecord::ConnectionAdapters::ConnectionPool#connection` .
2024-03-01 19:18:50 +08:00
The method has been renamed as `lease_connection` to better reflect that the returned
connection will be held for the duration of the request or job.
*Jean Boussier*
2024-03-25 21:19:42 +08:00
* Expose a generic fixture accessor for fixture names that may conflict with Minitest.
2024-02-28 18:10:46 +08:00
```ruby
assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
```
*Jean Boussier*
2024-02-22 22:01:09 +08:00
* Using `Model.query_constraints` with a single non-primary-key column used to raise as expected, but with an
incorrect error message. This has been fixed to raise with a more appropriate error message.
*Joshua Young*
2024-01-28 17:23:33 +08:00
* Fix `has_one` association autosave setting the foreign key attribute when it is unchanged.
This behaviour is also inconsistent with autosaving `belongs_to` and can have unintended side effects like raising
an `ActiveRecord::ReadOnlyAttributeError` when the foreign key attribute is marked as read-only.
*Joshua Young*
2023-11-29 03:48:02 +08:00
* Remove deprecated behavior that would rollback a transaction block when exited using `return` , `break` or `throw` .
*Rafael Mendonça França*
* Deprecate `Rails.application.config.active_record.commit_transaction_on_non_local_return` .
*Rafael Mendonça França*
2023-11-29 00:59:45 +08:00
* Remove deprecated support to pass `rewhere` to `ActiveRecord::Relation#merge` .
*Rafael Mendonça França*
2023-11-29 00:19:51 +08:00
* Remove deprecated support to pass `deferrable: true` to `add_foreign_key` .
*Rafael Mendonça França*
2023-11-24 01:46:59 +08:00
* Remove deprecated support to quote `ActiveSupport::Duration` .
*Rafael Mendonça França*
2023-11-24 01:36:05 +08:00
* Remove deprecated `#quote_bound_value` .
*Rafael Mendonça França*
2023-11-24 00:57:23 +08:00
* Remove deprecated `ActiveRecord::ConnectionAdapters::ConnectionPool#connection_klass` .
*Rafael Mendonça França*
2023-11-24 00:41:37 +08:00
* Remove deprecated support to apply `#connection_pool_list` , `#active_connections?` , `#clear_active_connections!` ,
`#clear_reloadable_connections!` , `#clear_all_connections!` and `#flush_idle_connections!` to the connections pools
for the current role when the `role` argument isn't provided.
*Rafael Mendonça França*
2023-11-07 06:34:29 +08:00
* Remove deprecated `#all_connection_pools` .
*Rafael Mendonça França*
2023-11-07 05:35:03 +08:00
* Remove deprecated `ActiveRecord::ConnectionAdapters::SchemaCache#data_sources` .
*Rafael Mendonça França*
2023-11-07 05:09:58 +08:00
* Remove deprecated `ActiveRecord::ConnectionAdapters::SchemaCache.load_from` .
*Rafael Mendonça França*
2023-11-07 04:23:40 +08:00
* Remove deprecated `#all_foreign_keys_valid?` from database adapters.
*Rafael Mendonça França*
2023-11-07 03:58:33 +08:00
* Remove deprecated support to passing coder and class as second argument to `serialize` .
*Rafael Mendonça França*
2023-11-07 03:51:35 +08:00
* Remove deprecated support to `ActiveRecord::Base#read_attribute(:id)` to return the custom primary key value.
*Rafael Mendonça França*
2023-11-07 00:49:32 +08:00
* Remove deprecated `TestFixtures.fixture_path` .
*Rafael Mendonça França*
2023-10-13 23:29:28 +08:00
* Remove deprecated behavior to support referring to a singular association by its plural name.
*Rafael Mendonça França*
* Deprecate `Rails.application.config.active_record.allow_deprecated_singular_associations_name`
*Rafael Mendonça França*
2023-10-13 23:19:15 +08:00
* Remove deprecated support to passing `SchemaMigration` and `InternalMetadata` classes as arguments to
`ActiveRecord::MigrationContext` .
*Rafael Mendonça França*
2023-10-13 22:35:44 +08:00
* Remove deprecated `ActiveRecord::Migration.check_pending` method.
*Rafael Mendonça França*
2023-10-13 22:08:21 +08:00
* Remove deprecated `ActiveRecord::LogSubscriber.runtime` method.
*Rafael Mendonça França*
* Remove deprecated `ActiveRecord::LogSubscriber.runtime=` method.
*Rafael Mendonça França*
* Remove deprecated `ActiveRecord::LogSubscriber.reset_runtime` method.
*Rafael Mendonça França*
2023-10-13 06:07:40 +08:00
* Remove deprecated support to define `explain` in the connection adapter with 2 arguments.
*Rafael Mendonça França*
2023-10-13 05:57:15 +08:00
* Remove deprecated `ActiveRecord::ActiveJobRequiredError` .
*Rafael Mendonça França*
2023-10-13 05:49:54 +08:00
* Remove deprecated `ActiveRecord::Base.clear_active_connections!` .
*Rafael Mendonça França*
* Remove deprecated `ActiveRecord::Base.clear_reloadable_connections!` .
*Rafael Mendonça França*
* Remove deprecated `ActiveRecord::Base.clear_all_connections!` .
*Rafael Mendonça França*
* Remove deprecated `ActiveRecord::Base.flush_idle_connections!` .
*Rafael Mendonça França*
2023-10-13 05:45:21 +08:00
* Remove deprecated `name` argument from `ActiveRecord::Base.remove_connection` .
*Rafael Mendonça França*
2023-10-13 05:26:28 +08:00
* Remove deprecated support to call `alias_attribute` with non-existent attribute names.
*Rafael Mendonça França*
2023-10-13 03:29:53 +08:00
* Remove deprecated `Rails.application.config.active_record.suppress_multiple_database_warning` .
*Rafael Mendonça França*
2024-02-17 01:07:25 +08:00
* Add ActiveRecord::Encryption::MessagePackMessageSerializer
Serialize data to the MessagePack format, for efficient storage in binary columns.
The binary encoding requires around 30% less space than the base64 encoding
used by the default serializer.
*Donal McBreen*
2024-02-16 00:45:56 +08:00
* Add support for encrypting binary columns
Ensure encryption and decryption pass `Type::Binary::Data` around for binary data.
Previously encrypting binary columns with the `ActiveRecord::Encryption::MessageSerializer`
incidentally worked for MySQL and SQLite, but not PostgreSQL.
*Donal McBreen*
2024-02-10 04:28:34 +08:00
* Deprecated `ENV["SCHEMA_CACHE"]` in favor of `schema_cache_path` in the database configuration.
*Rafael Mendonça França*
2024-02-14 20:55:34 +08:00
* Add `ActiveRecord::Base.with_connection` as a shortcut for leasing a connection for a short duration.
The leased connection is yielded, and for the duration of the block, any call to `ActiveRecord::Base.connection`
will yield that same connection.
This is useful to perform a few database operations without causing a connection to be leased for the
entire duration of the request or job.
*Jean Boussier*
2024-02-14 07:20:55 +08:00
* Deprecate `config.active_record.warn_on_records_fetched_greater_than` now that `sql.active_record`
notification includes `:row_count` field.
*Jason Nochlin*
2024-02-17 18:41:23 +08:00
* The fix ensures that the association is joined using the appropriate join type
(either inner join or left outer join) based on the existing joins in the scope.
This prevents unintentional overrides of existing join types and ensures consistency in the generated SQL queries.
Example:
```ruby
# `associated` will use `LEFT JOIN` instead of using `JOIN`
Post.left_joins(:author).where.associated(:author)
```
*Saleh Alhaddad*
2024-01-06 01:46:02 +08:00
* Fix an issue where `ActiveRecord::Encryption` configurations are not ready before the loading
of Active Record models, when an application is eager loaded. As a result, encrypted attributes
could be misconfigured in some cases.
*Maxime Réty*
Deprecate defining enums with keywords args
Enums have historically been defined using keyword arguments:
```ruby
class Function > ApplicationRecord
enum color: [:red, :blue],
type: [:instance, :class],
_scopes: false
```
This has the advantage of being able to define multiple enums at once
with the same options. However, it also has a downside that enum options
must be prefixed with an underscore to separate them from the enum
definitions (to enable models to have enums with the same name as an
option).
In Rails 7, a new syntax was [introduced][1] to instead define enums with
positional arguments:
```ruby
class Function > ApplicationRecord
enum :color, [:red, :blue], scopes: false
enum :type, [:instance, :class], scopes: false
```
This new syntax eliminates the need to prefix options with an underscore,
and the docs were updated to recommend this new syntax.
However, both versions of the API have been supported since, and it has
started to cause some problems:
The first issue is that the available options have drifted. In Rails
7.1, an option was added to make assigning an invalid enum value use
validation errors instead of runtime errors. However, the equivalent
underscored prefix option was not added for the original enum syntax
Articles have been created that describe the new option in Rails 7.1,
but the examples in the articles use un-prefixed options with the old
syntax. This confusion has also lead to issues opened asking why that
incorrect syntax is not working.
Additionally, the presence of underscored options is just generally
confusing because it tends to imply an option is for internal use.
This commit aims to fix all of these issues by deprecating the old enum
syntax. With only one way to define enums, options cannot drift and
there will be less confusion around how enums should be defined.
[1]: 0618d2d84a501aea93c898aec504ff9a0e09d6f2
2024-02-06 13:11:37 +08:00
* Deprecate defining an `enum` with keyword arguments.
```ruby
class Function > ApplicationRecord
# BAD
enum color: [:red, :blue],
type: [:instance, :class]
# GOOD
enum :color, [:red, :blue]
enum :type, [:instance, :class]
end
```
*Hartley McGuire*
2024-03-18 22:41:33 +08:00
* Add `config.active_record.validate_migration_timestamps` option for validating migration timestamps.
2023-12-20 03:30:08 +08:00
When set, validates that the timestamp prefix for a migration is no more than a day ahead of
the timestamp associated with the current time. This is designed to prevent migrations prefixes
from being hand-edited to future timestamps, which impacts migration generation and other
migration commands.
*Adrianna Chang*
2024-02-08 21:46:06 +08:00
* Properly synchronize `Mysql2Adapter#active?` and `TrilogyAdapter#active?`
As well as `disconnect!` and `verify!` .
This generally isn't a big problem as connections must not be shared between
threads, but is required when running transactional tests or system tests
and could lead to a SEGV.
*Jean Boussier*
2024-02-05 21:59:14 +08:00
* Support `:source_location` tag option for query log tags
```ruby
config.active_record.query_log_tags < < :source_location
```
Calculating the caller location is a costly operation and should be used primarily in development
(note, there is also a `config.active_record.verbose_query_logs` that serves the same purpose)
or occasionally on production for debugging purposes.
*fatkodima*
2024-01-19 22:53:33 +08:00
* Add an option to `ActiveRecord::Encryption::Encryptor` to disable compression
Allow compression to be disabled by setting `compress: false`
```ruby
class User
encrypts :name, encryptor: ActiveRecord::Encryption::Encryptor.new(compress: false)
end
```
*Donal McBreen*
2024-01-27 06:02:22 +08:00
* Deprecate passing strings to `ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename` .
A `ActiveRecord::DatabaseConfigurations::DatabaseConfig` object should be passed instead.
*Rafael Mendonça França*
2024-01-26 01:32:10 +08:00
* Add row_count field to sql.active_record notification
This field returns the amount of rows returned by the query that emitted the notification.
This metric is useful in cases where one wants to detect queries with big result sets.
*Marvin Bitterlich*
2024-01-19 17:42:40 +08:00
* Consistently raise an `ArgumentError` when passing an invalid argument to a nested attributes association writer.
Previously, this would only raise on collection associations and produce a generic error on singular associations.
Now, it will raise on both collection and singular associations.
*Joshua Young*
2024-01-13 05:18:48 +08:00
* Fix single quote escapes on default generated MySQL columns
MySQL 5.7.5+ supports generated columns, which can be used to create a column that is computed from an expression.
Previously, the schema dump would output a string with double escapes for generated columns with single quotes in the default expression.
This would result in issues when importing the schema on a fresh instance of a MySQL database.
Now, the string will not be escaped and will be valid Ruby upon importing of the schema.
*Yash Kapadia*
2024-01-10 03:28:13 +08:00
* Fix Migrations with versions older than 7.1 validating options given to
2024-01-17 06:18:01 +08:00
`add_reference` and `t.references` .
2024-01-10 03:28:13 +08:00
*Hartley McGuire*
2024-05-12 13:13:00 +08:00
* Add `<role>_types` class method to `ActiveRecord::DelegatedType` so that the delegated types can be introspected
2024-01-09 09:39:23 +08:00
*JP Rosevear*
2024-01-15 22:50:52 +08:00
* Make `schema_dump` , `query_cache` , `replica` and `database_tasks` configurable via `DATABASE_URL`
This wouldn't always work previously because boolean values would be interpreted as strings.
e.g. `DATABASE_URL=postgres://localhost/foo?schema_dump=false` now properly disable dumping the schema
cache.
*Mike Coutermarsh* , *Jean Boussier*
2023-12-04 19:02:33 +08:00
* Introduce `ActiveRecord::Transactions::ClassMethods#set_callback`
It is identical to `ActiveSupport::Callbacks::ClassMethods#set_callback`
but with support for `after_commit` and `after_rollback` callback options.
*Joshua Young*
2023-07-07 01:11:39 +08:00
* Make `ActiveRecord::Encryption::Encryptor` agnostic of the serialization format used for encrypted data.
Previously, the encryptor instance only allowed an encrypted value serialized as a `String` to be passed to the message serializer.
Now, the encryptor lets the configured `message_serializer` decide which types of serialized encrypted values are supported. A custom serialiser is therefore allowed to serialize `ActiveRecord::Encryption::Message` objects using a type other than `String` .
The default `ActiveRecord::Encryption::MessageSerializer` already ensures that only `String` objects are passed for deserialization.
*Maxime Réty*
2023-08-10 22:37:04 +08:00
* Fix `encrypted_attribute?` to take into account context properties passed to `encrypts` .
*Maxime Réty*
2024-01-06 00:13:01 +08:00
* The object returned by `explain` now responds to `pluck` , `first` ,
`last` , `average` , `count` , `maximum` , `minimum` , and `sum` . Those
new methods run `EXPLAIN` on the corresponding queries:
2023-12-22 03:45:50 +08:00
```ruby
User.all.explain.count
# EXPLAIN SELECT COUNT(*) FROM `users`
# ...
User.all.explain.maximum(:id)
# EXPLAIN SELECT MAX(`users`.`id`) FROM `users`
# ...
```
*Petrik de Heus*
2024-01-06 00:40:06 +08:00
* Fixes an issue where `validates_associated` `:on` option wasn't respected
when validating associated records.
2022-10-14 01:47:41 +08:00
*Austen Madden* , *Alex Ghiculescu* , *Rafał Brize*
2024-01-06 00:40:06 +08:00
* Allow overriding SQLite defaults from `database.yml` .
2024-01-02 19:52:54 +08:00
2024-01-06 00:40:06 +08:00
Any PRAGMA configuration set under the `pragmas` key in the configuration
file takes precedence over Rails' defaults, and additional PRAGMAs can be
set as well.
2024-01-02 19:52:54 +08:00
```yaml
database: storage/development.sqlite3
timeout: 5000
pragmas:
journal_mode: off
temp_store: memory
```
*Stephen Margheim*
2024-01-06 00:40:06 +08:00
* Remove warning message when running SQLite in production, but leave it unconfigured.
2023-12-28 03:56:47 +08:00
2024-01-06 00:44:35 +08:00
There are valid use cases for running SQLite in production. However, it must be done
2023-12-28 03:56:47 +08:00
with care, so instead of a warning most users won't see anyway, it's preferable to
leave the configuration commented out to force them to think about having the database
on a persistent volume etc.
*Jacopo Beschi* , *Jean Boussier*
2024-01-06 00:40:06 +08:00
* Add support for generated columns to the SQLite3 adapter.
2023-09-21 20:56:45 +08:00
Generated columns (both stored and dynamic) are supported since version 3.31.0 of SQLite.
This adds support for those to the SQLite3 adapter.
```ruby
create_table :users do |t|
t.string :name
t.virtual :name_upper, type: :string, as: 'UPPER(name)'
t.virtual :name_lower, type: :string, as: 'LOWER(name)', stored: true
end
```
*Stephen Margheim*
2023-12-14 00:30:21 +08:00
* TrilogyAdapter: ignore `host` if `socket` parameter is set.
2024-01-06 00:40:06 +08:00
This allows to configure a connection on a UNIX socket via `DATABASE_URL` :
2023-12-14 00:30:21 +08:00
```
DATABASE_URL=trilogy://does-not-matter/my_db_production?socket=/var/run/mysql.sock
```
*Jean Boussier*
2024-01-06 00:40:06 +08:00
* Make `assert_queries_count` , `assert_no_queries` , `assert_queries_match` , and
2023-12-20 18:28:55 +08:00
`assert_no_queries_match` assertions public.
2023-03-01 20:57:02 +08:00
2023-12-20 18:28:55 +08:00
To assert the expected number of queries are made, Rails internally uses `assert_queries_count` and
`assert_no_queries` . To assert that specific SQL queries are made, `assert_queries_match` and
`assert_no_queries_match` are used. These assertions can now be used in applications as well.
2023-03-01 20:57:02 +08:00
```ruby
class ArticleTest < ActiveSupport::TestCase
test "queries are made" do
2023-12-20 18:28:55 +08:00
assert_queries_count(1) { Article.first }
end
test "creates a foreign key" do
assert_queries_match(/ADD FOREIGN KEY/i, include_schema: true) do
@connection .add_foreign_key(:comments, :posts)
end
2023-03-01 20:57:02 +08:00
end
end
```
2023-12-20 18:28:55 +08:00
*Petrik de Heus* , *fatkodima*
2023-03-01 20:57:02 +08:00
2023-12-03 18:10:51 +08:00
* Fix `has_secure_token` calls the setter method on initialize.
*Abeid Ahmed*
2023-11-23 02:06:37 +08:00
* When using a `DATABASE_URL` , allow for a configuration to map the protocol in the URL to a specific database
adapter. This allows decoupling the adapter the application chooses to use from the database connection details
set in the deployment environment.
```ruby
# ENV['DATABASE_URL'] = "mysql://localhost/example_database"
config.active_record.protocol_adapters.mysql = "trilogy"
# will connect to MySQL using the trilogy adapter
```
*Jean Boussier* , *Kevin McPhillips*
2023-11-14 03:39:14 +08:00
* In cases where MySQL returns `warning_count` greater than zero, but returns no warnings when
the `SHOW WARNINGS` query is executed, `ActiveRecord.db_warnings_action` proc will still be
called with a generic warning message rather than silently ignoring the warning(s).
*Kevin McPhillips*
2024-01-06 00:40:06 +08:00
* `DatabaseConfigurations#configs_for` accepts a symbol in the `name` parameter.
2023-11-15 06:25:07 +08:00
*Andrew Novoselac*
2023-08-27 22:41:11 +08:00
* Fix `where(field: values)` queries when `field` is a serialized attribute
(for example, when `field` uses `ActiveRecord::Base.serialize` or is a JSON
column).
*João Alves*
2023-10-21 06:27:33 +08:00
* Make the output of `ActiveRecord::Core#inspect` configurable.
By default, calling `inspect` on a record will yield a formatted string including just the `id` .
```ruby
Post.first.inspect #=> "#< Post id: 1 > "
```
The attributes to be included in the output of `inspect` can be configured with
`ActiveRecord::Core#attributes_for_inspect` .
```ruby
Post.attributes_for_inspect = [:id, :title]
Post.first.inspect #=> "#< Post id: 1 , title: " Hello , World ! " > "
```
2024-01-06 00:40:06 +08:00
With `attributes_for_inspect` set to `:all` , `inspect` will list all the record's attributes.
2023-10-21 06:27:33 +08:00
```ruby
Post.attributes_for_inspect = :all
Post.first.inspect #=> "#< Post id: 1 , title: " Hello , World ! " , published_at: " 2023-10-23 14:28:11 + 0000 " > "
```
2024-01-06 00:40:06 +08:00
In `development` and `test` mode, `attributes_for_inspect` will be set to `:all` by default.
2023-10-21 06:27:33 +08:00
You can also call `full_inspect` to get an inspection with all the attributes.
The attributes in `attribute_for_inspect` will also be used for `pretty_print` .
*Andrew Novoselac*
2024-01-06 00:40:06 +08:00
* Don't mark attributes as changed when reassigned to `Float::INFINITY` or
`-Float::INFINITY` .
2023-11-03 23:24:19 +08:00
*Maicol Bentancor*
2024-01-06 00:40:06 +08:00
* Support the `RETURNING` clause for MariaDB.
2023-10-30 07:13:48 +08:00
*fatkodima* , *Nikolay Kondratyev*
2024-01-06 00:40:06 +08:00
* The SQLite3 adapter now implements the `supports_deferrable_constraints?` contract.
2023-09-25 05:14:02 +08:00
Allows foreign keys to be deferred by adding the `:deferrable` key to the `foreign_key` options.
```ruby
add_reference :person, :alias, foreign_key: { deferrable: :deferred }
add_reference :alias, :person, foreign_key: { deferrable: :deferred }
```
*Stephen Margheim*
2024-01-06 00:40:06 +08:00
* Add the `set_constraints` helper to PostgreSQL connections.
2023-09-07 06:26:29 +08:00
```ruby
Post.create!(user_id: -1) # => ActiveRecord::InvalidForeignKey
Post.transaction do
Post.connection.set_constraints(:deferred)
p = Post.create!(user_id: -1)
u = User.create!
p.user = u
p.save!
end
```
*Cody Cutrer*
2024-01-06 00:40:06 +08:00
* Include `ActiveModel::API` in `ActiveRecord::Base` .
2023-10-18 07:08:16 +08:00
*Sean Doyle*
2023-10-06 21:24:37 +08:00
* Ensure `#signed_id` outputs `url_safe` strings.
*Jason Meller*
2023-09-27 11:08:31 +08:00
2023-11-16 05:06:25 +08:00
* Add `nulls_last` and working `desc.nulls_first` for MySQL.
*Tristan Fellows*
2024-02-22 01:43:15 +08:00
* Allow for more complex hash arguments for `order` which mimics `where` in `ActiveRecord::Relation` .
```ruby
Topic.includes(:posts).order(posts: { created_at: :desc })
```
*Myles Boone*
2023-09-27 11:59:11 +08:00
Please check [7-1-stable ](https://github.com/rails/rails/blob/7-1-stable/activerecord/CHANGELOG.md ) for previous changes.