Permalink
Browse files

Merge pull request #29821 from y-yagi/set_rails_env_before_load_appli…

…cation_file

Set `RAILS_ENV` before load application file
  • Loading branch information...
kaspth committed Jul 17, 2017
2 parents 47eadb6 + 8be5018 commit a0ff5b57246f0b1439dc9b5ffbdd880e70fab93d
@@ -11,7 +11,7 @@ def initialize(options = {})
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 @@ class DbconsoleCommand < Base # :nodoc:
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
@@ -9,6 +9,8 @@ class DBConsoleTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
skip "PTY unavailable" unless available_pty?
build_app
end
@@ -17,7 +19,6 @@ def teardown
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 @@ def test_use_value_defined_in_environment_file_in_database_yml
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)

0 comments on commit a0ff5b5

Please sign in to comment.