diff options
author | Vladimir Dobriakov <vladimir@geekq.net> | 2008-11-24 14:17:39 +0100 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2009-06-01 12:09:19 +1200 |
commit | 64756e8f718c90a17664f2d35993696d8bf0f81e (patch) | |
tree | abf4159afcc5494001a1c220fdbe338fce47f9fa /railties | |
parent | f9bd10054c5f60d82f0a2eb9448dd0b73729e787 (diff) | |
download | rails-64756e8f718c90a17664f2d35993696d8bf0f81e.tar.gz rails-64756e8f718c90a17664f2d35993696d8bf0f81e.tar.bz2 rails-64756e8f718c90a17664f2d35993696d8bf0f81e.zip |
better db:create for mysql - do not assume root user
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1459 state:committed]
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/tasks/databases.rake | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index cdab5d8bb0..f572e914e1 100644 --- a/railties/lib/tasks/databases.rake +++ b/railties/lib/tasks/databases.rake @@ -56,12 +56,28 @@ namespace :db do when 'mysql' @charset = ENV['CHARSET'] || 'utf8' @collation = ENV['COLLATION'] || 'utf8_general_ci' + creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)} begin ActiveRecord::Base.establish_connection(config.merge('database' => nil)) - ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)) + ActiveRecord::Base.connection.create_database(config['database'], creation_options) ActiveRecord::Base.establish_connection(config) - rescue - $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation} (if you set the charset manually, make sure you have a matching collation)" + rescue Mysql::Error => sqlerr + if sqlerr.errno == Mysql::Error::ER_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']}.* " \ + "TO '#{config['username']}'@'localhost' " \ + "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;" + ActiveRecord::Base.establish_connection(config.merge( + 'database' => nil, 'username' => 'root', 'password' => root_password)) + ActiveRecord::Base.connection.create_database(config['database'], creation_options) + ActiveRecord::Base.connection.execute grant_statement + ActiveRecord::Base.establish_connection(config) + else + $stderr.puts sqlerr.error + $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}" + $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset'] + end end when 'postgresql' @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8' |