DotenvRailsDbTasksFix

Fix for the issue when ActiveRecord rake db:* tasks are magically executed in both development and test environments, but environment variables loaded via dotenv are not picking up the change.

You are viewing the README of version v0.3.0. You can find other releases here.

Branch Status
Release Build Status Coverage Status Gem Version Total Downloads
Development Build Status Coverage Status

If you rely on environment variables loaded via Dotenv chances are you also ran into this issue. E.g.:

database.yml

development:
  database:     <%= ENV['DB_NAME'] %>
  # ...
test:
  database:     <%= ENV['DB_NAME'] %>
  # ...

.env.development

DB_NAME=development
# ...

.env.test

DB_NAME=test
# ...

In your development environment:

$ rails db:setup
=>
Created database 'app_development'
Database 'app_development' already exists

It seems like the task is executed twice for the development environment (more on that later..) and you also often encounter an EnvironmentMismatchError:

$ rails db:setup
=>
Database 'app_development' already exists
Database 'app_development' already exists
rails aborted!
ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `test` environment.
You are running in `development` environment. If you are sure you want to continue, first set the environment using:

        bin/rails db:environment:set RAILS_ENV=development

Using this gem you can do:

Rakefile

# ...
DotenvRailsDbTasksFix.activate if ActiveRecord::Tasks::DatabaseTasks.env.eql?("development") # or Rails.env
$ rails db:setup
=>
Created database 'app_development'
Created database 'app_test'

Explanation

ActiveRecord has this feature that it executes DB tasks in test env as well if the current env is development (see: environments << "test" if environment == "development"). So ActiveRecord actually executes for different environments but not via a fresh start. This makes it impossible for dotenv to pick this change up in a clean way. But even if it did there are things already loaded at this point which dotenv should not touch. E.g. reinitializing the database config after an environment change should rather be the responsibility of ActiveRecord.

Dotenv's recommendation is to use different env var names (e.g. TEST_DB_NAME, DEVELOPMENT_DB_NAME) but that would be counter-intuitive. Instead via this gem ActiveRecord::Tasks::DatabaseTasks is monkey patched to explicitly reload env vars and the DB config when it switches to test env. This approach undoubtedly has its cons but in this case it only affects the development environment and restores the expected behaviour of this widely used feature therefore sparing the annoyance and possible effort of investigation.

See also this issue and this article.

Version support

Supports ActiveRecord 5.0.0 - 5.2.0. For certain versions of ActiveRecord 4 you can explicitly set RAILS_ENV to development to avoid execution in test env (see e.g. 4.2).

Caveats

  • Outside of development environment DotenvRailsDbTasksFix.activate will raise and will not monkey patch
  • Database config is expected to reside in Rails default #{DatabaseTasks.root}/config/database.yml (if you're using Rails DatabaseTasks.root == Rails.root)
  • There's some weirdness with Rails.env vs DatabaseTasks.env. From trial-and-error it seems changing DatabaseTasks.env to reflect the current execution env will result in issues (with e.g. db:setup and db:reset), while changing Rails.env is actually required for db:setup to work correctly. This fix seems to work for the use cases I tried but it's good to keep this in mind in case any similar issue presents. This might be due to this issue: https://github.com/rails/rails/issues/32910
  • If you introduce this to a project currently in use a final db:environment:set might be needed if prompted

Installation

Add this line to your application's Gemfile:

gem 'dotenv_rails_db_tasks_fix', group: :development

And then execute:

$ bundle

Or install it yourself as:

$ gem install dotenv_rails_db_tasks_fix

Feedback

Any feedback is much appreciated.

I can only tailor this project to fit use-cases I know about - which are usually my own ones. If you find that this might be the right direction to solve your problem too but you find that it's suboptimal or lacks features don't hesitate to contact me.

Let me know if you make use of this project so that I can prioritize further efforts.

Conventions

This gem is developed using the following conventions:

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/dotenv_rails_db_tasks_fix.

License

The gem is available as open source under the terms of the MIT License.