diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-11-15 16:59:29 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-11-15 16:59:29 -0200 |
commit | 8c66623627277df9dcc3c6215d369d2c50775cc1 (patch) | |
tree | 6f0d147f1508c9b24e455b0efc48d660e53a24d9 /activerecord | |
parent | f6f4bed34d7f654e0dde8b2cd369066d0857e91c (diff) | |
parent | 22f80ae57b26907f662b7fd50a7270a6381e527e (diff) | |
download | rails-8c66623627277df9dcc3c6215d369d2c50775cc1.tar.gz rails-8c66623627277df9dcc3c6215d369d2c50775cc1.tar.bz2 rails-8c66623627277df9dcc3c6215d369d2c50775cc1.zip |
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
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/railties/databases.rake | 36 | ||||
-rw-r--r-- | activerecord/lib/active_record/tasks/database_tasks.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/tasks/mysql_rake_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/tasks/postgresql_rake_test.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/tasks/sqlite_rake_test.rb | 12 |
6 files changed, 51 insertions, 16 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 4cb7066c1f..b549f594bb 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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: diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 52b3d3e5e6..3ca66223b6 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -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 diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index be7d496d15..0b6a49ac85 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -69,9 +69,11 @@ module ActiveRecord 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 @@ module ActiveRecord rescue Exception => error $stderr.puts error, *(error.backtrace) $stderr.puts "Couldn't drop #{configuration['database']}" + raise end def drop_all diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb index 3e3a2828f3..d16cf40555 100644 --- a/activerecord/test/cases/tasks/mysql_rake_test.rb +++ b/activerecord/test/cases/tasks/mysql_rake_test.rb @@ -61,7 +61,9 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index 6ea225178f..88ce666619 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -59,7 +59,9 @@ module ActiveRecord $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 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/tasks/sqlite_rake_test.rb b/activerecord/test/cases/tasks/sqlite_rake_test.rb index da3471adf9..eecc3527d2 100644 --- a/activerecord/test/cases/tasks/sqlite_rake_test.rb +++ b/activerecord/test/cases/tasks/sqlite_rake_test.rb @@ -27,7 +27,9 @@ module ActiveRecord $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 @@ module ActiveRecord 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 @@ module ActiveRecord $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 |