Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bring back "database already exists" messages when running rake tasks #8837

Closed
wants to merge 1 commit into from

2 participants

@carlosantoniodasilva

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.

Output of both mysql/postgresql:

./master-app [1.9.3-p327-perf]
$ rake db:setup
masterapp_development already exists
masterapp_test already exists
-- initialize_schema_migrations_table()
...

And for sqlite3:

./master-app [1.9.3-p327-perf]
$ rake db:setup
db/development.sqlite3 already exists
db/test.sqlite3 already exists
-- initialize_schema_migrations_table()
...

These changes should hopefully handle the issue @rubys has been seeing
on master related to the rake tasks.

I'm opening as a pull request to get some quick review before applying.
Let me know about any improvements.

@rafaelfranca
Owner

:shipit:

@carlosantoniodasilva carlosantoniodasilva 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.
8f232b9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 11, 2013
  1. @carlosantoniodasilva

    Bring back "database already exists" messages when running rake tasks

    carlosantoniodasilva authored
    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.
This page is out of date. Refresh to see the latest.
View
4 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}"
View
18 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
View
6 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
View
5 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
View
14 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)
View
10 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
View
10 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
Something went wrong with that request. Please try again.