Since Rails 6.1, it is possible for a model to connect to multiple databases. A minimal example of such application is:
class ApplicationRecord < ActiveRecord::Base
connects_to shard: {
defaul: { writing: :primary_db },
shard_one: { writing: :secondary_db }
}
end
class User < ApplicationRecord; end
ApplicationRecord.connected_to(shard: :shard_one, role: :writing) do
User.create!(...) # creates users in secondary_db DB
end
ApplicationRecord.connection_handler.connection_pools.map { |pool|
pool.db_config.configuration_hash[:database] } # [:primary_db,
:secondary_db]
With support for multiple databases for a model, one would have something like this in tests:
Since Rails 6.1, it is possible for a model to connect to multiple databases. A minimal example of such application is:
With support for multiple databases for a model, one would have something like this in tests:
In
.clean
, however, the bug occurs: it doesn't actually delete or truncate data from the :secondary_db.To fix the bug, DatabaseCleaner should iterate through all connection pools the model is connected to.