Skip to content
Browse files

Improve mysql database tasks handling to ensure we always rescue from…

… an exception

We were previously rescuing "nil" when no exception class was found.
This does work in 1.9.3, but does not in 2.0, raising an exception
asking for a class or module to be given to the rescue clause.

Thanks @yahonda for catching this.
  • Loading branch information...
1 parent 5d06f8a commit e709134214571d27cf2872a38c998a0e7bbc71f8 @carlosantoniodasilva carlosantoniodasilva committed
Showing with 13 additions and 9 deletions.
  1. +13 −9 activerecord/lib/active_record/tasks/mysql_database_tasks.rb
View
22 activerecord/lib/active_record/tasks/mysql_database_tasks.rb
@@ -15,18 +15,20 @@ def create
establish_connection configuration_without_database
connection.create_database configuration['database'], creation_options
establish_connection configuration
- rescue error_class => error
- raise error unless error.errno == ACCESS_DENIED_ERROR
-
- $stdout.print error.error
- establish_connection root_configuration_without_database
- connection.create_database configuration['database'], creation_options
- connection.execute grant_statement.gsub(/\s+/, ' ').strip
- establish_connection configuration
- rescue ActiveRecord::StatementInvalid, error_class => error
+ rescue ActiveRecord::StatementInvalid => error
if /database exists/ === error.message
raise DatabaseAlreadyExists
else
+ raise
+ end
+ rescue error_class => error
+ if error.respond_to?(:errno) && error.errno == ACCESS_DENIED_ERROR
+ $stdout.print error.error
+ establish_connection root_configuration_without_database
+ connection.create_database configuration['database'], creation_options
+ connection.execute grant_statement.gsub(/\s+/, ' ').strip
+ establish_connection configuration
+ 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
@@ -96,6 +98,8 @@ def error_class
Mysql2::Error
elsif defined?(Mysql)
Mysql::Error
+ else
+ StandardError
end
end

0 comments on commit e709134

Please sign in to comment.
Something went wrong with that request. Please try again.