This Week in Rails

November 22, 2024

Active Support's NotificationAssertions and `sql.active_record` gets `affected_rows`

Hi, it’s zzak. Let’s explore this week’s changes in the Rails codebase.

Add Active Support Notifications test helper module
This PR has been created because it’s currently cumbersome to test that a certain code block/action triggers an ActiveSupport::Notifications::Event to be emitted. It would be ideal to have some helpers to assert against such event emission. Have heard such helpers could be helpful internal to Rails as well.

Add “affected_rows” to “sql.active_record” event
The [recently added][1] row_count value is very useful for identifying cases where a query would return a large result set as large results can end up using a lot of memory or even be blocked by databases like Vitess. However, somewhere that row_count falls short is for queries that do not necessarily return their results back to the client. These queries that affect too many rows can lead to their own set of problems, such as overwhelming replication and causing replication lag.

Allow “hidden_field” tag to accept a custom autocomplete value
In #43280 autocomplete="off" was enforced for all hidden inputs generated by Rails to fix a firefox bug. Unfortunately it’s also a legitimate use-case to specify an autocomplete with a value such as username and a value on a hidden input. This hints to the browser that (in this example) the username of a password reset form is what we’ve provided as the value and the password manager can store it as such.

Allow to reset cache counters for multiple records
There is often a need to reset counter caches for multiple records. Achieving this before will generate many extra queries.

# Before
response.content_type = "text/html"

# After
response.content_type = :html

Parallel tests with :number_of_processors uses cgroups-aware usable processor count
When using parallel tests the default will now try to allocate a worker pool based of the total number of processors available to the system.

Fix Mysql2Adapter support for prepared statements
If you’re using the Mysql2 adapter and rely on prepared statements, you may want to wait for the next bug-fix release before upgrading to Rails 8.0.

Silence deprecate message during “app:update” command
When running the app:update command you previously may see warnings due to not adopting the new_framework_defaults. This PR silences those messages to avoid confusion.

Reword error message for NoDatabaseError
This patch adds some suggestions on what to do when you run into this error.

Redesign ActionView::Template::Handlers::ERB.find_offset to handle edge cases
Some excellent work was put into improving the ERB template error highlighting in this PR, as well as fixing a bug with multibyte character tokenization.

Make column_definitions queries retryable
Previously when an application wasn’t using a schema cache in production a ping query would be executed for each table when it was loaded the first time.

Make Action Dispatch Session#store method conform to Rack spec
The Rack specification states that a hash-like object stored in environment with rack.session key must implement store/2 method with []= semantics.

Preserve timezone and locale in ActiveJob exception handlers
This PR fixes a bug where the job locale and timezone were wrong inside the rescue_from block.

You can view the whole list of changes here. We had 26 contributors to the Rails codebase this past week!

Until next time!

About This Week in Rails

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