Skip to content

Commit

Permalink
Adds support for lower AR versions
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismydesign committed Jun 4, 2018
1 parent 728a4ad commit dbf3d68
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ Dotenv's recommendation is to use different env var names (e.g. `TEST_DB_NAME`,

See also [this issue](https://github.com/thisismydesign/dotenv_rails_db_tasks_fix) and [this article](http://www.zhuwu.me/blog/posts/rake-db-tasks-always-runs-twice-in-development-environment).

## Version support

Support `ActiveRecord` 4 - 5.1.6. Although 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](https://github.com/rails/rails/blob/v4.2.0/activerecord/lib/active_record/tasks/database_tasks.rb#L271)).

## 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`)
- Requires ActiveRecord >= 5.1.5, ~> 5.1.6 (because there're slight differences in the private API, althoguh following this solution it would be easy to extend support for other versions)
- 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](https://github.com/thisismydesign/dotenv_rails_db_tasks_fix/blob/v0.2.0/lib/dotenv_rails_db_tasks_fix.rb#L25-L29) 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

Expand Down
2 changes: 1 addition & 1 deletion dotenv_rails_db_tasks_fix.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "dotenv"
spec.add_dependency "activerecord", ">= 5.1.5", "~> 5.1.6"
spec.add_dependency "activerecord", ">= 4.0.0", "<= 5.1.6"

spec.add_development_dependency "sqlite3"
spec.add_development_dependency "bundler", "~> 1.16"
Expand Down
11 changes: 10 additions & 1 deletion lib/dotenv_rails_db_tasks_fix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ def each_current_configuration(environment)

db_config = Pathname.new(self.root).join("config", "database.yml")
config = YAML::load(ERB.new(File.read(db_config)).result)
yield config[environment], environment if config[environment]["database"]

active_record_version = Gem::Version.new(ActiveRecord.version)

if active_record_version < Gem::Version.new("5.1.5")
# https://github.com/rails/rails/blob/v5.1.0/activerecord/lib/active_record/tasks/database_tasks.rb#L298-L306
yield config[environment] if config[environment]["database"]
else
# https://github.com/rails/rails/blob/v5.1.5/activerecord/lib/active_record/tasks/database_tasks.rb#L298-L307
yield config[environment], environment if config[environment]["database"]
end
end
end
end
Expand Down

0 comments on commit dbf3d68

Please sign in to comment.