Skip to content

Commit

Permalink
Bring back "database already exists" messages when running rake tasks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carlosantoniodasilva committed Jan 12, 2013
1 parent c1d7225 commit 9b636dc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ActiveRecord
module Tasks # :nodoc:
class DatabaseAlreadyExists < StandardError; end # :nodoc:

module DatabaseTasks # :nodoc:
extend self

Expand Down Expand Up @@ -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}"
Expand Down
18 changes: 9 additions & 9 deletions activerecord/lib/active_record/tasks/mysql_database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions activerecord/test/cases/tasks/database_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/tasks/mysql_rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/tasks/postgresql_rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b636dc

Please sign in to comment.