Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `rake db:drop` have a 0 exit status, and raise no error, when it fails?

I was surprised to discover that, when rake db:drop (and assumedly Rails' other built-in raketasks) fails, the bash status code is 0.

$ rake db:drop
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
...
$ echo $?
0

Perhaps more surprising, it doesn't even raise an error when the task is invoked from within Rails.

2.3.0 :001 > begin
2.3.0 :002 >   Rake::Task["db:drop"].invoke
2.3.0 :003 >   puts "After raketask invoked"
2.3.0 :004 > end
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
...
After raketask invoked

Is this intentional? Digging into the source, this seems to simply call execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}". This should raise an error on failure. What am I missing?

like image 654
Simon Lepkin Avatar asked Apr 05 '16 23:04

Simon Lepkin


Video Answer


1 Answers

This is an issue fixed in Rails 5: PR #19924 Explicitly exit with status "1" for create and drop failures

Best solution would be to use Rails 5. :) Otherwise, you're stuck with one of:

  • Check that the db was dropped another way (could miss other errors).
  • Capture stderr output and inspect
  • Monkeypatch ActiveRecord::Tasks::DatabaseTasks::drop
like image 128
Kris Kumler Avatar answered Oct 12 '22 23:10

Kris Kumler