Wednesday 22 February 2017

[SOLVED] Rails multiple databases not cleaning up in tests.

So the other day I was facing this issue with Rails where the data created in a test would not get deleted from my secondary database.

The database was not cleaning up after the first test. the order didn’t matter. which ever test runs first the records created in that block were not deleted. after thorough debugging i found that there is a block inside actived record which rollsback the db. here is that block (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L1008).

 So in case of the first test the @fixture_connections count is 1 it only contains the primary db connection.

So for my secondary db I had created an abstract class SecondaryModel  which inherited from  ActiveRecord::Base 
So adding SecondaryModel.connection to test_helper solved this problem.

hope this helped!

No comments:

Post a Comment