aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authoryuuji.yaginuma <yuuji.yaginuma@gmail.com>2017-07-17 09:11:21 +0900
committeryuuji.yaginuma <yuuji.yaginuma@gmail.com>2017-07-17 09:11:21 +0900
commit8be50181d3fbe0f727a68de33ec856efdf772487 (patch)
tree14c86580088b59b6b8eeb05073803dbd75e87d2e /railties
parentf4d52eee07954c510a8318176d6d11100afabf7b (diff)
downloadrails-8be50181d3fbe0f727a68de33ec856efdf772487.tar.gz
rails-8be50181d3fbe0f727a68de33ec856efdf772487.tar.bz2
rails-8be50181d3fbe0f727a68de33ec856efdf772487.zip
Set `RAILS_ENV` before load application file
Since #29725, load application file when `dbconsole` command is executed. However, if do not set `RAILS_ENV` before reading the application file, can not connect to the env specified in option, so added the setting of `RAILS_ENV`.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/commands/dbconsole/dbconsole_command.rb5
-rw-r--r--railties/test/application/dbconsole_test.rb35
2 files changed, 36 insertions, 4 deletions
diff --git a/railties/lib/rails/commands/dbconsole/dbconsole_command.rb b/railties/lib/rails/commands/dbconsole/dbconsole_command.rb
index a66eb16421..71b3455473 100644
--- a/railties/lib/rails/commands/dbconsole/dbconsole_command.rb
+++ b/railties/lib/rails/commands/dbconsole/dbconsole_command.rb
@@ -11,7 +11,7 @@ module Rails
end
def start
- ENV["RAILS_ENV"] = @options[:environment] || environment
+ ENV["RAILS_ENV"] ||= @options[:environment] || environment
case config["adapter"]
when /^(jdbc)?mysql/
@@ -157,6 +157,9 @@ module Rails
def perform
extract_environment_option_from_argument
+ # RAILS_ENV needs to be set before config/application is required.
+ ENV["RAILS_ENV"] = options[:environment]
+
require_application_and_environment!
Rails::DBConsole.start(options)
end
diff --git a/railties/test/application/dbconsole_test.rb b/railties/test/application/dbconsole_test.rb
index 7e5e9ea8aa..12d1cfb089 100644
--- a/railties/test/application/dbconsole_test.rb
+++ b/railties/test/application/dbconsole_test.rb
@@ -9,6 +9,8 @@ module ApplicationTests
include ActiveSupport::Testing::Isolation
def setup
+ skip "PTY unavailable" unless available_pty?
+
build_app
end
@@ -17,7 +19,6 @@ module ApplicationTests
end
def test_use_value_defined_in_environment_file_in_database_yml
- skip "PTY unavailable" unless available_pty?
Dir.chdir(app_path) do
app_file "config/database.yml", <<-YAML
development:
@@ -41,9 +42,37 @@ module ApplicationTests
master.puts ".exit"
end
+ def test_respect_environment_option
+ Dir.chdir(app_path) do
+ app_file "config/database.yml", <<-YAML
+ default: &default
+ adapter: sqlite3
+ pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
+ timeout: 5000
+
+ development:
+ <<: *default
+ database: db/development.sqlite3
+
+ production:
+ <<: *default
+ database: db/production.sqlite3
+ YAML
+ end
+
+ master, slave = PTY.open
+ spawn_dbconsole(slave, "-e production")
+ assert_output("sqlite>", master)
+
+ master.puts ".databases"
+ assert_output("production.sqlite3", master)
+ ensure
+ master.puts ".exit"
+ end
+
private
- def spawn_dbconsole(fd)
- Process.spawn("#{app_path}/bin/rails dbconsole", in: fd, out: fd, err: fd)
+ def spawn_dbconsole(fd, options = nil)
+ Process.spawn("#{app_path}/bin/rails dbconsole #{options}", in: fd, out: fd, err: fd)
end
def assert_output(expected, io, timeout = 5)