From 9b636dcf53e4ac410186a720d5bef783fc8648c4 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 9 Jan 2013 00:58:23 -0200 Subject: [PATCH] Bring back "database already exists" messages when running rake tasks When running tasks such "rake db:setup", instead of showing messages like "db_development already exists", it was showing a big stack trace and a message "Couldn't create database for ..." with the configuration options, a very confusing message with a big trace. This brings back the functionality present in 3-2, showing the same message. --- .../lib/active_record/tasks/database_tasks.rb | 4 ++++ .../tasks/mysql_database_tasks.rb | 18 +++++++++--------- .../tasks/postgresql_database_tasks.rb | 6 ++++++ .../tasks/sqlite_database_tasks.rb | 5 +---- .../test/cases/tasks/database_tasks_test.rb | 14 +++++++------- .../test/cases/tasks/mysql_rake_test.rb | 10 ++++++++++ .../test/cases/tasks/postgresql_rake_test.rb | 10 ++++++++++ 7 files changed, 47 insertions(+), 20 deletions(-) diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index fda51b3d76710..67c7e714e6b49 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -1,5 +1,7 @@ module ActiveRecord module Tasks # :nodoc: + class DatabaseAlreadyExists < StandardError; end # :nodoc: + module DatabaseTasks # :nodoc: extend self @@ -32,6 +34,8 @@ def current_config(options = {}) def create(*arguments) configuration = arguments.first class_for_adapter(configuration['adapter']).new(*arguments).create + rescue DatabaseAlreadyExists + $stderr.puts "#{configuration['database']} already exists" rescue Exception => error $stderr.puts error, *(error.backtrace) $stderr.puts "Couldn't create database for #{configuration.inspect}" diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index 3d27c97254254..3ae5aa2cea2eb 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -24,10 +24,13 @@ def create connection.create_database configuration['database'], creation_options connection.execute grant_statement.gsub(/\s+/, ' ').strip establish_connection configuration - rescue error_class => error - $stderr.puts error.error - $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}" - $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding'] + rescue error_class, ActiveRecord::StatementInvalid => error + if /database exists/ === error.message + raise DatabaseAlreadyExists + else + $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}" + $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding'] + end end def drop @@ -87,14 +90,11 @@ def creation_options end def error_class - case configuration['adapter'] - when /jdbc/ + if configuration['adapter'] =~ /jdbc/ require 'active_record/railties/jdbcmysql_error' ArJdbcMySQL::Error - when /mysql2/ - Mysql2::Error else - Mysql::Error + defined?(Mysql2) ? Mysql2::Error : Mysql::Error end end diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index ea5cb888fb9a7..e6a85b1917adb 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -18,6 +18,12 @@ def create(master_established = false) connection.create_database configuration['database'], configuration.merge('encoding' => encoding) establish_connection configuration + rescue ActiveRecord::StatementInvalid => error + if /database .* already exists/ === error.message + raise DatabaseAlreadyExists + else + raise + end end def drop diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index da01058a82947..33c43c7774e92 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -9,10 +9,7 @@ def initialize(configuration, root = Rails.root) end def create - if File.exist?(configuration['database']) - $stderr.puts "#{configuration['database']} already exists" - return - end + raise DatabaseAlreadyExists if File.exist?(configuration['database']) establish_connection configuration connection diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index 4f3489b7a5ec2..659d5eae7271f 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -11,10 +11,10 @@ def setup end ADAPTERS_TASKS = { - :mysql => :mysql_tasks, - :mysql2 => :mysql_tasks, - :postgresql => :postgresql_tasks, - :sqlite3 => :sqlite_tasks + mysql: :mysql_tasks, + mysql2: :mysql_tasks, + postgresql: :postgresql_tasks, + sqlite3: :sqlite_tasks } class DatabaseTasksRegisterTask < ActiveRecord::TestCase @@ -32,7 +32,7 @@ def structure_dump(filename); end ActiveRecord::Tasks::DatabaseTasks.structure_dump({'adapter' => :foo}, "awesome-file.sql") end end - + class DatabaseTasksCreateTest < ActiveRecord::TestCase include DatabaseTasksSetupper @@ -258,7 +258,7 @@ class DatabaseTasksPurgeTest < ActiveRecord::TestCase class DatabaseTasksCharsetTest < ActiveRecord::TestCase include DatabaseTasksSetupper - + ADAPTERS_TASKS.each do |k, v| define_method("test_#{k}_charset") do eval("@#{v}").expects(:charset) @@ -269,7 +269,7 @@ class DatabaseTasksCharsetTest < ActiveRecord::TestCase class DatabaseTasksCollationTest < ActiveRecord::TestCase include DatabaseTasksSetupper - + ADAPTERS_TASKS.each do |k, v| define_method("test_#{k}_collation") do eval("@#{v}").expects(:collation) diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb index 69a049fcfa601..38b9dd02f0bc7 100644 --- a/activerecord/test/cases/tasks/mysql_rake_test.rb +++ b/activerecord/test/cases/tasks/mysql_rake_test.rb @@ -53,6 +53,16 @@ def test_establishes_connection_to_database ActiveRecord::Tasks::DatabaseTasks.create @configuration end + + def test_create_when_database_exists_outputs_info_to_stderr + $stderr.expects(:puts).with("my-app-db already exists").once + + ActiveRecord::Base.connection.stubs(:create_database).raises( + ActiveRecord::StatementInvalid.new("Can't create database 'dev'; database exists:") + ) + + ActiveRecord::Tasks::DatabaseTasks.create @configuration + end end class MysqlDBCreateAsRootTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb index 62acd53003a02..3006a87589e40 100644 --- a/activerecord/test/cases/tasks/postgresql_rake_test.rb +++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb @@ -61,6 +61,16 @@ def test_db_create_with_error_prints_message ActiveRecord::Tasks::DatabaseTasks.create @configuration end + + def test_create_when_database_exists_outputs_info_to_stderr + $stderr.expects(:puts).with("my-app-db already exists").once + + ActiveRecord::Base.connection.stubs(:create_database).raises( + ActiveRecord::StatementInvalid.new('database "my-app-db" already exists') + ) + + ActiveRecord::Tasks::DatabaseTasks.create @configuration + end end class PostgreSQLDBDropTest < ActiveRecord::TestCase