-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Description
This is a reopening of a task #32926
Original message
When executing Rake DB tasks in development environment they are also executed in test environment (see). This environment change doesn't prompt the usual intialization but the existing environment config is used, not taking into account that it can dynamically change between environments.
Why is this a problem?
1, Consider the following Rails feature:
The config/database.yml file can contain ERB tags <%= %>. Anything in the tags will be evaluated as Ruby code. You can use this to pull out data from an environment variable or to perform calculations to generate the needed connection information.
For example having different values in test env will not be taken into account in this case. This is known to cause issues with dotenv: bkeepers/dotenv#302.
2, AFAICT Rails.env and DatabaseTasks.env are not set correctly after the env change. Amongst other possible issues I believe is the root cause of #26731 which is being solved by hardcoding test env instead of using current_environment.
3, I would imagine there are further implications which I didn't discover.
Steps to reproduce
I reproduce the dotenv issue with a dummy project in the gem dotenv_rails_db_tasks_fix (see db config, .env.test, .env.development, test)
Expected behavior
When switching environments the new one is properly initialized at least in a way to respect existing Rails features (i.e. set env variables such as Rails.env and reload database.yml).
Actual behavior
The existing environment config is used, not taking into account that it can dynamically change between environments.
System configuration
Rails version: Rails 5.1.6
Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]