Skip to content

Commit

Permalink
Catch mysql2 access denied errors in rake db:create [#5432 state:reso…
Browse files Browse the repository at this point in the history
…lved]

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information
pixeltrix authored and fxn committed Aug 25, 2010
1 parent 9ba46cf commit 241dfa3
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/railties/databases.rake
Expand Up @@ -61,12 +61,14 @@ namespace :db do
@charset = ENV['CHARSET'] || 'utf8'
@collation = ENV['COLLATION'] || 'utf8_unicode_ci'
creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
error_class = config['adapter'] == 'mysql2' ? Mysql2::Error : Mysql::Error
access_denied_error = 1045
begin
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
ActiveRecord::Base.connection.create_database(config['database'], creation_options)
ActiveRecord::Base.establish_connection(config)
rescue Mysql::Error => sqlerr
if sqlerr.errno == Mysql::Error::ER_ACCESS_DENIED_ERROR
rescue error_class => sqlerr
if sqlerr.errno == access_denied_error
print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
root_password = $stdin.gets.strip
grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
Expand Down

0 comments on commit 241dfa3

Please sign in to comment.