Friday, March 29, 2024

Retry known idempotent SELECT queries, New Active Record configuration option, and more!

Posted by Emmanuel Hayford

Greetings! I’m Emmanuel Hayford, here to bring you your weekly instalment of “This Week In Rails”. Let’s dive straight into it.   Two new guides are now open for community review. If you are well versed in Action View, partials, and helpers, please review and submit your feedback here:

Add config.active_record.permanent_connection_checkout setting
This setting determines the action taken when ActiveRecord::Base.connection is accessed: whether it raises an error, emits a deprecation warning, or does nothing.

ActiveRecord::Base.connection acquires a database connection from the pool and maintains it until the request or job concludes. However, this behaviour may prove undesirable in environments with more threads or fibers than available connections.

This configuration facilitates the identification and elimination of code utilising ActiveRecord::Base.connection, encouraging migration to ActiveRecord::Base.with_connection instead. The default behaviour remains unaltered.

Make ActiveSupport::BacktraceCleaner copy filters on dup
Enhance ActiveSupport::BacktraceCleaner to replicate filters and silencers during duplication and cloning.

Previously, duplication still resulted in shared internal silencers and filters arrays, leading to state leakage.

Ensure necessary options are added to association options
Noting the typo in :through, take the following code sample as an example:

class User < ApplicationRecord
    has_many :courses
    has_many :assignments, trough: :courses
end

You’d get a misleading error along the lines of

“Unknown key: :trough. Valid keys are: :class_name, :anonymous_class, :primary_key, …, :index_errors (ArgumentError)”,

with this pull request, you’ll now get a clearer message, more like:

“Unknown key: :trough. Valid keys are: :class_name, :anonymous_class, :primary_key, …, :index_errors, :through (ArgumentError)”.

Note that in the second error message :through is included in the list of valid options.

Retry known idempotent SELECT queries on connection-related exceptions
Implements a mechanism to automatically retry SELECT queries that are known to be idempotent in the event of connection-related exceptions.

Queries constructed through Arel tree traversal or based on known model attributes are inherently idempotent and can be safely retried upon encountering a connection error. Previously, certain adapters like TrilogyAdapter would raise ActiveRecord::ConnectionFailed: Trilogy::EOFError when faced with a connection error during a request.

We had 22 contributors to the Rails codebase this past week!

Take care.

Your weekly inside scoop of interesting commits, pull requests and more from Rails.

Subscribe to get these updates mailed to you.