1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Exit with non-zero status when db:create fails

* If the create task fails for a reason other than the database already
  existing, processing should end. This is indicated by a non-zero exit
  status.
* Since the backtrace is already printed to screen, we forgo printing it
  again by using an explicit call to `exit`.
* ⚠️ This modifies the behavior of the db:create task slightly in
  that the stack trace is no longer printed by default. If the `--trace`
  option is used, it will print the trace _after_ the error message.
This commit is contained in:
Jay Hayes 2015-04-26 22:20:08 -05:00
parent 1cd35be3eb
commit 2893e6c0a4
4 changed files with 21 additions and 3 deletions

View file

@ -94,8 +94,9 @@ module ActiveRecord
rescue DatabaseAlreadyExists
$stderr.puts "#{configuration['database']} already exists"
rescue Exception => error
$stderr.puts error, *(error.backtrace)
$stderr.puts error
$stderr.puts "Couldn't create database for #{configuration.inspect}"
raise
end
def create_all

View file

@ -60,7 +60,7 @@ module ActiveRecord
$stderr.expects(:puts).
with("Couldn't create database for #{@configuration.inspect}")
ActiveRecord::Tasks::DatabaseTasks.create @configuration
assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration }
end
def test_create_when_database_exists_outputs_info_to_stderr

View file

@ -53,7 +53,7 @@ module ActiveRecord
$stderr.expects(:puts).
with("Couldn't create database for #{@configuration.inspect}")
ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root'
assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root' }
end
end

View file

@ -66,6 +66,23 @@ module ApplicationTests
end
end
def with_bad_permissions
Dir.chdir(app_path) do
set_database_url
FileUtils.chmod("-w", "db")
yield
FileUtils.chmod("+w", "db")
end
end
test 'db:create failure because bad permissions' do
with_bad_permissions do
output = `bin/rake db:create 2>&1`
assert_match /Couldn't create database/, output
assert_equal 1, $?.exitstatus
end
end
test 'db:drop failure because database does not exist' do
Dir.chdir(app_path) do
output = `bin/rake db:drop 2>&1`