diff options
author | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-06-18 01:16:44 -0500 |
---|---|---|
committer | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-06-18 01:16:44 -0500 |
commit | 588e80fd132e1eb82f4cfa9a7c21625bdd9934e8 (patch) | |
tree | 08b7d8140de6d0bca3863c2e0a5c5ef84bbf4ef6 /activerecord/lib/active_record/tasks/mysql_database_tasks.rb | |
parent | b9660bdac5ea2a632cdfbbf660e35ef1b7d1723c (diff) | |
download | rails-588e80fd132e1eb82f4cfa9a7c21625bdd9934e8.tar.gz rails-588e80fd132e1eb82f4cfa9a7c21625bdd9934e8.tar.bz2 rails-588e80fd132e1eb82f4cfa9a7c21625bdd9934e8.zip |
add :nodoc: to AR::Tasks and update to follow the coding conventions
Diffstat (limited to 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb')
-rw-r--r-- | activerecord/lib/active_record/tasks/mysql_database_tasks.rb | 145 |
1 files changed, 75 insertions, 70 deletions
diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index eb598629fa..646034f313 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -1,88 +1,93 @@ -class ActiveRecord::Tasks::MySQLDatabaseTasks - DEFAULT_CHARSET = ENV['CHARSET'] || 'utf8' - DEFAULT_COLLATION = ENV['COLLATION'] || 'utf8_unicode_ci' - ACCESS_DENIED_ERROR = 1045 +module ActiveRecord + module Tasks # :nodoc: + class MySQLDatabaseTasks # :nodoc: - delegate :connection, :establish_connection, :to => ActiveRecord::Base + DEFAULT_CHARSET = ENV['CHARSET'] || 'utf8' + DEFAULT_COLLATION = ENV['COLLATION'] || 'utf8_unicode_ci' + ACCESS_DENIED_ERROR = 1045 - def initialize(configuration) - @configuration = configuration - end + delegate :connection, :establish_connection, to: ActiveRecord::Base - 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 + def initialize(configuration) + @configuration = configuration + end - $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 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['charset'] - end + 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 - def drop - establish_connection configuration - connection.drop_database configuration['database'] - end + $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 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['charset'] + end - def purge - establish_connection :test - connection.recreate_database configuration['database'], creation_options - end + def drop + establish_connection configuration + connection.drop_database configuration['database'] + end - private + def purge + establish_connection :test + connection.recreate_database configuration['database'], creation_options + end - def configuration - @configuration - end + private - def configuration_without_database - configuration.merge('database' => nil) - end + def configuration + @configuration + end - def creation_options - { - :charset => (configuration['charset'] || DEFAULT_CHARSET), - :collation => (configuration['collation'] || DEFAULT_COLLATION) - } - end + def configuration_without_database + configuration.merge('database' => nil) + end - def error_class - case configuration['adapter'] - when /jdbc/ - require 'active_record/railties/jdbcmysql_error' - ArJdbcMySQL::Error - when /mysql2/ - Mysql2::Error - else - Mysql::Error - end - end + def creation_options + { + charset: (configuration['charset'] || DEFAULT_CHARSET), + collation: (configuration['collation'] || DEFAULT_COLLATION) + } + end + + def error_class + case configuration['adapter'] + when /jdbc/ + require 'active_record/railties/jdbcmysql_error' + ArJdbcMySQL::Error + when /mysql2/ + Mysql2::Error + else + Mysql::Error + end + end - def grant_statement - <<-SQL + def grant_statement + <<-SQL GRANT ALL PRIVILEGES ON #{configuration['database']}.* TO '#{configuration['username']}'@'localhost' IDENTIFIED BY '#{configuration['password']}' WITH GRANT OPTION; - SQL - end + SQL + end - def root_configuration_without_database - configuration_without_database.merge( - 'username' => 'root', - 'password' => root_password - ) - end + def root_configuration_without_database + configuration_without_database.merge( + 'username' => 'root', + 'password' => root_password + ) + end - def root_password - $stdout.print "Please provide the root password for your mysql installation\n>" - $stdin.gets.strip + def root_password + $stdout.print "Please provide the root password for your mysql installation\n>" + $stdin.gets.strip + end + end end -end +end
\ No newline at end of file |