Skip to content

Commit 241dfa3

Browse files
pixeltrixfxn
authored andcommitted
Catch mysql2 access denied errors in rake db:create [#5432 state:resolved]
Signed-off-by: Xavier Noria <fxn@hashref.com>
1 parent 9ba46cf commit 241dfa3

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

activerecord/lib/active_record/railties/databases.rake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ namespace :db do
6161
@charset = ENV['CHARSET'] || 'utf8'
6262
@collation = ENV['COLLATION'] || 'utf8_unicode_ci'
6363
creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
64+
error_class = config['adapter'] == 'mysql2' ? Mysql2::Error : Mysql::Error
65+
access_denied_error = 1045
6466
begin
6567
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
6668
ActiveRecord::Base.connection.create_database(config['database'], creation_options)
6769
ActiveRecord::Base.establish_connection(config)
68-
rescue Mysql::Error => sqlerr
69-
if sqlerr.errno == Mysql::Error::ER_ACCESS_DENIED_ERROR
70+
rescue error_class => sqlerr
71+
if sqlerr.errno == access_denied_error
7072
print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
7173
root_password = $stdin.gets.strip
7274
grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \

0 commit comments

Comments
 (0)