Permalink
Browse files

Merge pull request #12531 from iamvery/database-tasks-exit-status

Explicitly exit with status "1" for create and drop task failures

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
2 parents f6f4bed + 22f80ae commit 8c66623627277df9dcc3c6215d369d2c50775cc1 @rafaelfranca rafaelfranca committed Nov 15, 2013
@@ -1,3 +1,7 @@
+* Exit with non-zero status for failed database rake tasks.
+
+ *Jay Hayes*
+
* Added `ActiveRecord::Base.to_param` for convenient "pretty" URLs derived from a model's attribute or method.
Example:
@@ -8,31 +8,47 @@ db_namespace = namespace :db do
namespace :create do
task :all => :load_config do
- ActiveRecord::Tasks::DatabaseTasks.create_all
+ begin
+ ActiveRecord::Tasks::DatabaseTasks.create_all
+ rescue
+ exit(1)
+ end
end
end
desc 'Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all databases in the config)'
task :create => [:load_config] do
- if ENV['DATABASE_URL']
- ActiveRecord::Tasks::DatabaseTasks.create_database_url
- else
- ActiveRecord::Tasks::DatabaseTasks.create_current
+ begin
+ if ENV['DATABASE_URL']
+ ActiveRecord::Tasks::DatabaseTasks.create_database_url
+ else
+ ActiveRecord::Tasks::DatabaseTasks.create_current
+ end
+ rescue
+ exit(1)
end
end
namespace :drop do
task :all => :load_config do
- ActiveRecord::Tasks::DatabaseTasks.drop_all
+ begin
+ ActiveRecord::Tasks::DatabaseTasks.drop_all
+ rescue
+ exit(1)
+ end
end
end
desc 'Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases)'
task :drop => [:load_config] do
- if ENV['DATABASE_URL']
- ActiveRecord::Tasks::DatabaseTasks.drop_database_url
- else
- ActiveRecord::Tasks::DatabaseTasks.drop_current
+ begin
+ if ENV['DATABASE_URL']
+ ActiveRecord::Tasks::DatabaseTasks.drop_database_url
+ else
+ ActiveRecord::Tasks::DatabaseTasks.drop_current
+ end
+ rescue
+ exit(1)
end
end
@@ -69,9 +69,11 @@ def create(*arguments)
class_for_adapter(configuration['adapter']).new(*arguments).create
rescue DatabaseAlreadyExists
$stderr.puts "#{configuration['database']} already exists"
+ raise
rescue Exception => error
$stderr.puts error, *(error.backtrace)
$stderr.puts "Couldn't create database for #{configuration.inspect}"
+ raise
end
def create_all
@@ -95,6 +97,7 @@ def drop(*arguments)
rescue Exception => error
$stderr.puts error, *(error.backtrace)
$stderr.puts "Couldn't drop #{configuration['database']}"
+ raise
end
def drop_all
@@ -61,7 +61,9 @@ def test_create_when_database_exists_outputs_info_to_stderr
ActiveRecord::StatementInvalid.new("Can't create database 'dev'; database exists:")
)
- ActiveRecord::Tasks::DatabaseTasks.create @configuration
+ assert_raises(ActiveRecord::Tasks::DatabaseAlreadyExists) do
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration
+ end
end
end
@@ -59,7 +59,9 @@ def test_db_create_with_error_prints_message
$stderr.expects(:puts).
with("Couldn't create database for #{@configuration.inspect}")
- ActiveRecord::Tasks::DatabaseTasks.create @configuration
+ assert_raises(Exception) do
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration
+ end
end
def test_create_when_database_exists_outputs_info_to_stderr
@@ -69,7 +71,9 @@ def test_create_when_database_exists_outputs_info_to_stderr
ActiveRecord::StatementInvalid.new('database "my-app-db" already exists')
)
- ActiveRecord::Tasks::DatabaseTasks.create @configuration
+ assert_raises(ActiveRecord::Tasks::DatabaseAlreadyExists) do
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration
+ end
end
end
@@ -27,7 +27,9 @@ def test_db_create_when_file_exists
$stderr.expects(:puts).with("#{@database} already exists")
- ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root'
+ assert_raises(ActiveRecord::Tasks::DatabaseAlreadyExists) do
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root'
+ end
end
def test_db_create_with_file_does_nothing
@@ -36,7 +38,9 @@ def test_db_create_with_file_does_nothing
ActiveRecord::Base.expects(:establish_connection).never
- ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root'
+ assert_raises(ActiveRecord::Tasks::DatabaseAlreadyExists) do
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root'
+ end
end
def test_db_create_establishes_a_connection
@@ -52,7 +56,9 @@ def test_db_create_with_error_prints_message
$stderr.expects(:puts).
with("Couldn't create database for #{@configuration.inspect}")
- ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root'
+ assert_raises(Exception) do
+ ActiveRecord::Tasks::DatabaseTasks.create @configuration, '/rails/root'
+ end
end
end

0 comments on commit 8c66623

Please sign in to comment.