Sim*_*kin 9 ruby postgresql activerecord ruby-on-rails ruby-on-rails-4
我很惊讶地发现,当rake db:drop(并假设Rails的其他内置raketasks)失败时,bash状态代码为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
Run Code Online (Sandbox Code Playgroud)
也许更令人惊讶的是,当从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
Run Code Online (Sandbox Code Playgroud)
这是故意的吗?挖掘源头,这似乎只是打电话execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}".这应该会在失败时引发错误.我错过了什么?
这是 Rails 5 中修复的问题:PR #19924 创建和删除失败时显式退出,状态为“1”
最好的解决方案是使用 Rails 5。:) 否则,您将陷入以下困境之一: