mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #30414 from bogdanvlviv/clear-mysql_database_tasks
Simplify implementation of `MySQLDatabaseTasks`
This commit is contained in:
commit
542ac6b310
3 changed files with 5 additions and 139 deletions
|
@ -1,18 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
#FIXME Remove if ArJdbcMysql will give.
|
||||
module ArJdbcMySQL #:nodoc:
|
||||
class Error < StandardError #:nodoc:
|
||||
attr_accessor :error_number, :sql_state
|
||||
|
||||
def initialize(msg)
|
||||
super
|
||||
@error_number = nil
|
||||
@sql_state = nil
|
||||
end
|
||||
|
||||
# Mysql gem compatibility
|
||||
alias_method :errno, :error_number
|
||||
alias_method :error, :message
|
||||
end
|
||||
end
|
|
@ -3,8 +3,6 @@
|
|||
module ActiveRecord
|
||||
module Tasks # :nodoc:
|
||||
class MySQLDatabaseTasks # :nodoc:
|
||||
ACCESS_DENIED_ERROR = 1045
|
||||
|
||||
delegate :connection, :establish_connection, to: ActiveRecord::Base
|
||||
|
||||
def initialize(configuration)
|
||||
|
@ -21,20 +19,6 @@ module ActiveRecord
|
|||
else
|
||||
raise
|
||||
end
|
||||
rescue error_class => error
|
||||
if error.respond_to?(:errno) && error.errno == ACCESS_DENIED_ERROR
|
||||
$stdout.print error.message
|
||||
establish_connection root_configuration_without_database
|
||||
connection.create_database configuration["database"], creation_options
|
||||
if configuration["username"] != "root"
|
||||
connection.execute grant_statement.gsub(/\s+/, " ").strip
|
||||
end
|
||||
establish_connection configuration
|
||||
else
|
||||
$stderr.puts error.inspect
|
||||
$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
|
||||
end
|
||||
|
||||
def drop
|
||||
|
@ -99,37 +83,6 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def error_class
|
||||
if configuration["adapter"].include?("jdbc")
|
||||
require "active_record/railties/jdbcmysql_error"
|
||||
ArJdbcMySQL::Error
|
||||
elsif defined?(Mysql2)
|
||||
Mysql2::Error
|
||||
else
|
||||
StandardError
|
||||
end
|
||||
end
|
||||
|
||||
def grant_statement
|
||||
<<-SQL
|
||||
GRANT ALL PRIVILEGES ON `#{configuration['database']}`.*
|
||||
TO '#{configuration['username']}'@'localhost'
|
||||
IDENTIFIED BY '#{configuration['password']}' WITH GRANT OPTION;
|
||||
SQL
|
||||
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
|
||||
end
|
||||
|
||||
def prepare_command_options
|
||||
args = {
|
||||
"host" => "--host",
|
||||
|
|
|
@ -75,7 +75,7 @@ if current_adapter?(:Mysql2Adapter)
|
|||
end
|
||||
end
|
||||
|
||||
class MysqlDBCreateAsRootTest < ActiveRecord::TestCase
|
||||
class MysqlDBCreateWithInvalidPermissionsTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
@connection = stub("Connection", create_database: true)
|
||||
@error = Mysql2::Error.new("Invalid permissions")
|
||||
|
@ -86,13 +86,8 @@ if current_adapter?(:Mysql2Adapter)
|
|||
"password" => "wossname"
|
||||
}
|
||||
|
||||
$stdin.stubs(:gets).returns("secret\n")
|
||||
$stdout.stubs(:print).returns(nil)
|
||||
@error.stubs(:errno).returns(1045)
|
||||
ActiveRecord::Base.stubs(:connection).returns(@connection)
|
||||
ActiveRecord::Base.stubs(:establish_connection).
|
||||
raises(@error).
|
||||
then.returns(true)
|
||||
ActiveRecord::Base.stubs(:establish_connection).raises(@error)
|
||||
|
||||
$stdout, @original_stdout = StringIO.new, $stdout
|
||||
$stderr, @original_stderr = StringIO.new, $stderr
|
||||
|
@ -102,75 +97,11 @@ if current_adapter?(:Mysql2Adapter)
|
|||
$stdout, $stderr = @original_stdout, @original_stderr
|
||||
end
|
||||
|
||||
def test_root_password_is_requested
|
||||
assert_permissions_granted_for("pat")
|
||||
$stdin.expects(:gets).returns("secret\n")
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_connection_established_as_root
|
||||
assert_permissions_granted_for("pat")
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
"adapter" => "mysql2",
|
||||
"database" => nil,
|
||||
"username" => "root",
|
||||
"password" => "secret"
|
||||
)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_database_created_by_root
|
||||
assert_permissions_granted_for("pat")
|
||||
@connection.expects(:create_database).
|
||||
with("my-app-db", {})
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_grant_privileges_for_normal_user
|
||||
assert_permissions_granted_for("pat")
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_do_not_grant_privileges_for_root_user
|
||||
@configuration["username"] = "root"
|
||||
@configuration["password"] = ""
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_connection_established_as_normal_user
|
||||
assert_permissions_granted_for("pat")
|
||||
ActiveRecord::Base.expects(:establish_connection).returns do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
"adapter" => "mysql2",
|
||||
"database" => "my-app-db",
|
||||
"username" => "pat",
|
||||
"password" => "secret"
|
||||
)
|
||||
|
||||
raise @error
|
||||
def test_raises_error
|
||||
assert_raises(Mysql2::Error) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
def test_sends_output_to_stderr_when_other_errors
|
||||
@error.stubs(:errno).returns(42)
|
||||
|
||||
$stderr.expects(:puts).at_least_once.returns(nil)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def assert_permissions_granted_for(db_user)
|
||||
db_name = @configuration["database"]
|
||||
db_password = @configuration["password"]
|
||||
@connection.expects(:execute).with("GRANT ALL PRIVILEGES ON `#{db_name}`.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_password}' WITH GRANT OPTION;")
|
||||
end
|
||||
end
|
||||
|
||||
class MySQLDBDropTest < ActiveRecord::TestCase
|
||||
|
|
Loading…
Reference in a new issue