diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2017-07-09 17:00:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-09 17:00:22 +0200 |
commit | bed328c888ea35b08ad1c25ffe5f3c421ae2312e (patch) | |
tree | a90dff23bceb31084b0d9d5223d90b3b0c61d3b5 /railties | |
parent | 551f88366dcbaed3b0abc89bddd63975c4c9a5ca (diff) | |
parent | e12715bfd55831a84e9398280fb294d9136ede2e (diff) | |
download | rails-bed328c888ea35b08ad1c25ffe5f3c421ae2312e.tar.gz rails-bed328c888ea35b08ad1c25ffe5f3c421ae2312e.tar.bz2 rails-bed328c888ea35b08ad1c25ffe5f3c421ae2312e.zip |
Merge pull request #29725 from y-yagi/fix_29717
Load environment file in `dbconsole` command
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 6 | ||||
-rw-r--r-- | railties/lib/rails/commands/dbconsole/dbconsole_command.rb | 1 | ||||
-rw-r--r-- | railties/test/application/dbconsole_test.rb | 66 |
3 files changed, 73 insertions, 0 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index e837a07623..d793405da1 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,9 @@ +* Load environment file in `dbconsole` command. + + Fixes #29717 + + *Yuji Yaginuma* + * Add `rails secrets:show` command. *Yuji Yaginuma* diff --git a/railties/lib/rails/commands/dbconsole/dbconsole_command.rb b/railties/lib/rails/commands/dbconsole/dbconsole_command.rb index 5db588b66a..b3df5191c6 100644 --- a/railties/lib/rails/commands/dbconsole/dbconsole_command.rb +++ b/railties/lib/rails/commands/dbconsole/dbconsole_command.rb @@ -148,6 +148,7 @@ module Rails def perform extract_environment_option_from_argument + require_application_and_environment! Rails::DBConsole.start(options) end end diff --git a/railties/test/application/dbconsole_test.rb b/railties/test/application/dbconsole_test.rb new file mode 100644 index 0000000000..7e5e9ea8aa --- /dev/null +++ b/railties/test/application/dbconsole_test.rb @@ -0,0 +1,66 @@ +require "isolation/abstract_unit" +begin + require "pty" +rescue LoadError +end + +module ApplicationTests + class DBConsoleTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + end + + def teardown + teardown_app + 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: + database: <%= Rails.application.config.database %> + adapter: sqlite3 + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + timeout: 5000 + YAML + + app_file "config/environments/development.rb", <<-RUBY + Rails.application.configure do + config.database = "db/development.sqlite3" + end + RUBY + end + + master, slave = PTY.open + spawn_dbconsole(slave) + assert_output("sqlite>", master) + ensure + master.puts ".exit" + end + + private + def spawn_dbconsole(fd) + Process.spawn("#{app_path}/bin/rails dbconsole", in: fd, out: fd, err: fd) + end + + def assert_output(expected, io, timeout = 5) + timeout = Time.now + timeout + + output = "" + until output.include?(expected) || Time.now > timeout + if IO.select([io], [], [], 0.1) + output << io.read(1) + end + end + + assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}" + end + + def available_pty? + defined?(PTY) && PTY.respond_to?(:open) + end + end +end |