How to opt out of parallel database hooks

The problem with parallel hooks

Previously, developers had the ability to append their own hooks to parallel tests, but there was no straightforward way to bypass the Rails database parallelization hooks. This limitation could be restrictive in cases where developers wanted to implement their own database handling processes.

Skipping database hooks

The core of this change lies in the introduction of a new configuration option that allows developers to skip Rails database parallelization. By setting the parallelize_databases parameter to false in the parallelize caller or configuring config.active_support.parallelize_test_databases to false in the application config, developers can now control whether Rails creates databases for each process during parallel testing.

Here's how that would look like:

class ActiveSupport::TestCase
  parallelize(workers: 10, parallelize_databases: false)
end

It's worth noting that this configuration is set to true by default, ensuring backward compatibility for existing projects. However, developers now have the freedom to customize this behavior based on their specific requirements. It's also important to mention potential deadlocks that may occur if databases are not created for each process.

Author
Josef Strzibny
Hello, I am Josef and I am on Rails since its 2.0 version. I always liked strong conventions and the Rails Omakase docrine. I am author of Kamal Handbook and Test Driving Rails. I think testing should be fun, not a chore.

© Test Driving Rails Blog