Multiple Databases with Active Record allows you to switch between databases, like so:
ActiveRecord::Base.connected_to(role: :reading) { Post.last }
And it works great! But it breaks any tests that use it. The reason is it’s using a new/different session when talking with the replica db, even if it’s the same db, which means we can’t see any data from the primary session that isn’t committed. This is problematic because all our tests run in transactions, that are rolled back after each test via the DatabaseCleaner
.
We could disable this behavior for some tests that are hitting replica, and change the cleaning method to :truncation
but the problem then is that all data is wiped from the db. Some tests are relying on that data. Wiping the db causes problems for those. We could potentially reload that data after we truncate. I couldn’t find a way to get it to work with transactions.
Is there a workaround for this scenario? As it’s not a problem for the gem we’re currently using, distribute_reads.
Or is the only solution to abandon transactional tests?