DatabaseCleaner / database_cleaner

Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing.
https://www.rubydoc.info/github/DatabaseCleaner/database_cleaner
MIT License
2.93k stars 484 forks source link

allow_remote_database_url not working as expected #668

Open wout opened 3 years ago

wout commented 3 years ago

I'm running Rails in development in a Dockerized environment. One of the configured Docker Compose services is a container to run commands (e.g. rails, rake, ...) and its RAILS_ENV is development. If I accidentally run rspec in that particular container, my development database gets cleaned, which is not what I want.

I tried configuring DatabaseCleaner.url_whitelist to include the full URL to the test database. For example:

DatabaseCleaner.url_whitelist = %w[postgres://postgres:postgres@postgres:5432/my_app_test]

But it looks like only base URLs are considered.

Then I tried allowing remote URLs for the test environment alone by adding the following line at the top of support/database_cleaner.rb:

DatabaseCleaner.allow_remote_database_url = Rails.env.test?

But that's ignored for some reason. I checked if Rails.env.test? is false at that point, and it is.

So now I've replaced that line with:

raise StandardError, "Database cleaner expected 'test' environment" unless Rails.env.test?

Which works. But isn't allow_remote_database_url supposed to do the same? Or am I missing something crucial?

tmaier commented 3 years ago

Re DatabaseCleaner.url_allowlist: This should support regex as well. See README

takamario commented 3 years ago

I tried with rspec by setting ENV['RAILS_ENV'] ||= 'test' in spec/rails_helper.rb, but https://github.com/DatabaseCleaner/database_cleaner/commit/77dece1e145899231a57b0fead8a20f6e43d3d0a caused Rails.env.test? to return false.