database_cleaner is wiping my development database

Solution 1:

I'd recommend changing

ENV["RAILS_ENV"] ||= 'test'

to

ENV["RAILS_ENV"] = 'test'

and remove

Rails.env = 'test'

as the RAILS_ENV environment variable should be sufficient for configuration

Solution 2:

If anyone is looking for another potential source of this issue, I randomly had $DATABASE_URL defined in my .bashrc file to point directly to my development database. Took me a few hours to find that.

Solution 3:

In my case it was database connection specified in .env file when I used dotenv-rails gem. For some reasons database_cleaner prefer connection from there instead of rails application config.