dotenv_rails_db_tasks_fix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d126642ad8ea8ec2a15861353fa3b48efd0133e22df45b63f4f25d2e9499a0ca
4
+ data.tar.gz: b59c95dcecb6f997799d32d04f259a70fba3673d053fe898854c2dab1a135b25
5
+ SHA512:
6
+ metadata.gz: 6a34646ddce10d1d8c7e47ecd19d35e75695dad9becf03aee0315dbdda70a79cfd92a297dd12808f05f81beac98e34e94f5604f1bdc0935c5caa4062392e2fdd
7
+ data.tar.gz: f46d7d44344ac35610780e488d937907ee11a9607c44c03d85e84be368645ce11328a0923f5ea130a9300bac11b7d4ad4ba497f79c8e512c4c7826bc0d392049
data/.env.development ADDED
@@ -0,0 +1 @@
1
+ DB_DATABASE=development
data/.env.test ADDED
@@ -0,0 +1 @@
1
+ DB_DATABASE=test
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /Gemfile.lock
14
+ /spec/example_project/db/*.sqlite3
15
+ /.ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in dotenv_rails_db_tasks_fix.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Csaba Apagyi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # DotenvRailsDbTasksFix
2
+
3
+ #### Fix for the issue when ActiveRecord `rake db:*` tasks are magically executed in both developement and test environments, but environment variables loaded via `dotenv` are not picking up the change.
4
+
5
+ *You are viewing the README of version [v0.1.0](https://github.com/thisismydesign/dotenv_rails_db_tasks_fix/releases/tag/v0.1.0).*
6
+
7
+ | Branch | Status |
8
+ | ------ | ------ |
9
+ | Development | [![Build Status](https://travis-ci.org/thisismydesign/dotenv_rails_db_tasks_fix.svg?branch=master)](https://travis-ci.org/thisismydesign/dotenv_rails_db_tasks_fix) [![Coverage Status](https://coveralls.io/repos/github/thisismydesign/dotenv_rails_db_tasks_fix/badge.svg?branch=master)](https://coveralls.io/github/thisismydesign/dotenv_rails_db_tasks_fix?branch=master) |
10
+
11
+ If you're using environment variables in your `database.yml` and load them via `Dotenv` chances are you also ran into this annoying issue in your development environment:
12
+
13
+ `database.yml`
14
+ ```
15
+ development:
16
+ database: <%= ENV['DB_NAME'] %>
17
+ # ...
18
+ test:
19
+ database: <%= ENV['DB_NAME'] %>
20
+ # ...
21
+ ```
22
+
23
+ `.env.development`
24
+ ```
25
+ DB_NAME=development
26
+ # ...
27
+ ```
28
+
29
+ `.env.test`
30
+ ```
31
+ DB_NAME=test
32
+ # ...
33
+ ```
34
+
35
+ ```
36
+ $ rails db:setup
37
+ =>
38
+ Created database 'app_development'
39
+ Database 'app_development' already exists
40
+ ```
41
+
42
+ ```
43
+ $ rails db:setup
44
+ =>
45
+ Database 'app_development' already exists
46
+ Database 'app_development' already exists
47
+ rails aborted!
48
+ ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `test` environment.
49
+ You are running in `development` environment. If you are sure you want to continue, first set the environment using:
50
+
51
+ bin/rails db:environment:set RAILS_ENV=development
52
+ ```
53
+
54
+ Using this gem you can do:
55
+
56
+ `Rakefile`
57
+ ```
58
+ # ...
59
+ require "dotenv_rails_db_tasks_fix"
60
+ DotenvRailsDbTasksFix.activate
61
+ ```
62
+
63
+ ```
64
+ $ rails db:setup
65
+ =>
66
+ Created database 'app_development'
67
+ Created database 'app_test'
68
+ ```
69
+
70
+ ## Explanation
71
+
72
+ ActiveRecord has this feature that it executes DB tasks in `test` env as well if the current env is `development`. ([see](https://github.com/rails/rails/blob/v5.1.5/activerecord/lib/active_record/tasks/database_tasks.rb#L300):
73
+ `environments << "test" if environment == "development"`). This happens in the middle of execution so there's no way for `dotenv` to nicely intervene and even if they did there are things already loaded at this point (e.g. env vars are interpolated to configs). 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 here we monkey patch `ActiveRecord::Tasks::DatabaseTasks` to explicitly reload env vars and the DB config when it switches to test env.
74
+
75
+ *It's invasive and ugly but it only affects the development environment (so it's low-risk) and restores the expected behaviour of this widely used feature therefore sparing the annoyance and possible effort of investigation.*
76
+
77
+ See [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).
78
+
79
+ ## Caveats
80
+
81
+ - Database config is expected to reside in Rails default `#{DatabaseTasks.root}/config/database.yml` (if you're using Rails `DatabaseTasks.root == Rails.root`)
82
+ - 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)
83
+ - 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/be83ad6f97e4c1eb4bcfb5a2860eb3b53d7ff063/lib/dotenv_rails_db_tasks_fix.rb#L24-L28) seems to work for the use cases I tried but it's good to keep this in mind in case any similar issue presents.
84
+
85
+ ## Installation
86
+
87
+ Add this line to your application's Gemfile:
88
+
89
+ ```ruby
90
+ gem 'dotenv_rails_db_tasks_fix'
91
+ ```
92
+
93
+ And then execute:
94
+
95
+ $ bundle
96
+
97
+ Or install it yourself as:
98
+
99
+ $ gem install dotenv_rails_db_tasks_fix
100
+
101
+ ## Feedback
102
+
103
+ Any feedback is much appreciated.
104
+
105
+ 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.
106
+
107
+ Let me know if you make use of this project so that I can prioritize further efforts.
108
+
109
+ ## Conventions
110
+
111
+ This gem is developed using the following conventions:
112
+ - [Bundler's guide for developing a gem](http://bundler.io/v1.14/guides/creating_gem.html)
113
+ - [Better Specs](http://www.betterspecs.org/)
114
+ - [Semantic versioning](http://semver.org/)
115
+ - [RubyGems' guide on gem naming](http://guides.rubygems.org/name-your-gem/)
116
+ - [RFC memo about key words used to Indicate Requirement Levels](https://tools.ietf.org/html/rfc2119)
117
+ - [Bundler improvements](https://github.com/thisismydesign/bundler-improvements)
118
+ - [Minimal dependencies](http://www.mikeperham.com/2016/02/09/kill-your-dependencies/)
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thisismydesign/dotenv_rails_db_tasks_fix.
123
+
124
+ ## License
125
+
126
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require_relative "lib/dotenv_rails_db_tasks_fix"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+
9
+ require "erb"
10
+ require 'yaml'
11
+ require 'dotenv'
12
+ require 'active_record'
13
+
14
+ RSPEC_ROOT = Pathname.new(File.dirname(__FILE__)).join("spec")
15
+ environment = ENV["RAILS_ENV"] || "development"
16
+ db_config = Pathname.new(RSPEC_ROOT).join("example_project", "config", "database.yml")
17
+
18
+ Dotenv.overload(".env", ".env.#{environment}", ".env.local", ".env.#{environment}.local")
19
+
20
+ class Seeder; def load_seed; end; end
21
+
22
+ include ActiveRecord::Tasks
23
+ DatabaseTasks.database_configuration = YAML::load(ERB.new(File.read(db_config)).result)
24
+ DatabaseTasks.root = Pathname.new(RSPEC_ROOT).join("example_project")
25
+ DatabaseTasks.db_dir = Pathname.new(DatabaseTasks.root).join("db")
26
+ DatabaseTasks.migrations_paths = [File.join(DatabaseTasks.root, 'db/migrate')]
27
+ DatabaseTasks.env = environment
28
+ DatabaseTasks.seed_loader = Seeder.new
29
+
30
+ task :environment do
31
+ ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
32
+ ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
33
+ end
34
+
35
+ load 'active_record/railties/databases.rake'
36
+
37
+ DotenvRailsDbTasksFix.activate
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dotenv_rails_db_tasks_fix"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "dotenv_rails_db_tasks_fix/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dotenv_rails_db_tasks_fix"
7
+ spec.version = DotenvRailsDbTasksFix::VERSION
8
+ spec.authors = ["thisismydesign"]
9
+ spec.email = ["git.thisismydesign@gmail.com"]
10
+
11
+ spec.summary = ""
12
+ spec.homepage = "https://github.com/thisismydesign/dotenv_rails_db_tasks_fix"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "dotenv"
22
+ spec.add_dependency "activerecord", ">= 5.1.5", "~> 5.1.6"
23
+
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "coveralls"
29
+ end
@@ -0,0 +1,37 @@
1
+ require "dotenv_rails_db_tasks_fix/version"
2
+ require "dotenv"
3
+ require "active_record"
4
+
5
+ module DotenvRailsDbTasksFix
6
+ def self.activate
7
+ ActiveRecord::Tasks::DatabaseTasks.instance_eval do
8
+ return unless env.eql?("development")
9
+
10
+ private
11
+
12
+ def each_current_configuration(environment)
13
+ environments = [environment]
14
+ environments << "test" if environment == "development"
15
+
16
+ environments.each do |environment|
17
+ # On load order see: https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
18
+ if environment.eql?("test")
19
+ Dotenv.overload(".env", ".env.#{environment}", ".env.#{environment}.local")
20
+ else
21
+ Dotenv.overload(".env", ".env.#{environment}", ".env.local", ".env.#{environment}.local")
22
+ end
23
+
24
+ # This is a fix for consecutive `db:setup` calls
25
+ # Without this they would run into `ActiveRecord::EnvironmentMismatchError`
26
+ # Otherwise it could be circumvented by using the `DISABLE_DATABASE_ENVIRONMENT_CHECK` env var or executing `db:drop` previously
27
+ # Check for `Rails` is there because it's not a dependency
28
+ Rails.env = environment if defined?(Rails)
29
+
30
+ db_config = Pathname.new(self.root).join("config", "database.yml")
31
+ config = YAML::load(ERB.new(File.read(db_config)).result)
32
+ yield config[environment], environment if config[environment]["database"]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module DotenvRailsDbTasksFix
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotenv_rails_db_tasks_fix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - thisismydesign
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.1.5
34
+ - - "~>"
35
+ - !ruby/object:Gem::Version
36
+ version: 5.1.6
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 5.1.5
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 5.1.6
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.16'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.16'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: coveralls
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description:
118
+ email:
119
+ - git.thisismydesign@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".env.development"
125
+ - ".env.test"
126
+ - ".gitignore"
127
+ - ".rspec"
128
+ - ".travis.yml"
129
+ - Gemfile
130
+ - LICENSE.txt
131
+ - README.md
132
+ - Rakefile
133
+ - bin/console
134
+ - bin/setup
135
+ - dotenv_rails_db_tasks_fix.gemspec
136
+ - lib/dotenv_rails_db_tasks_fix.rb
137
+ - lib/dotenv_rails_db_tasks_fix/version.rb
138
+ homepage: https://github.com/thisismydesign/dotenv_rails_db_tasks_fix
139
+ licenses: []
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.7.6
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: ''
161
+ test_files: []