提交 274e966a 编写于 作者: Y Yves Senn

`rake db:create` does not change permissions of root user.

Closes #8079.

I had to rework some of the tests because the mock allowed any arguments
for `connection.exeucte`. I think this is very dangerous as there could
anything be executed without the tests noticing it.
上级 8744632f
## Rails 4.0.0 (unreleased) ##
* `rake db:create` does not change permissions of the MySQL root user.
Fixes #8079.
*Yves Senn*
* The length of the `version` column in the `schema_migrations` table
created by the `mysql2` adapter is 191 if the encoding is "utf8mb4".
......
......@@ -26,7 +26,9 @@ def create
$stdout.print error.error
establish_connection root_configuration_without_database
connection.create_database configuration['database'], creation_options
connection.execute grant_statement.gsub(/\s+/, ' ').strip
if configuration['username'] != 'root'
connection.execute grant_statement.gsub(/\s+/, ' ').strip
end
establish_connection configuration
else
$stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}"
......
......@@ -71,7 +71,7 @@ def setup
return skip("only tested on mysql")
end
@connection = stub(:create_database => true, :execute => true)
@connection = stub("Connection", create_database: true)
@error = Mysql::Error.new "Invalid permissions"
@configuration = {
'adapter' => 'mysql',
......@@ -90,6 +90,7 @@ def setup
end
def test_root_password_is_requested
assert_permissions_granted_for "pat"
skip "only if mysql is available" unless defined?(::Mysql)
$stdin.expects(:gets).returns("secret\n")
......@@ -97,6 +98,7 @@ def test_root_password_is_requested
end
def test_connection_established_as_root
assert_permissions_granted_for "pat"
ActiveRecord::Base.expects(:establish_connection).with(
'adapter' => 'mysql',
'database' => nil,
......@@ -108,6 +110,7 @@ def test_connection_established_as_root
end
def test_database_created_by_root
assert_permissions_granted_for "pat"
@connection.expects(:create_database).
with('my-app-db', :charset => 'utf8', :collation => 'utf8_unicode_ci')
......@@ -115,12 +118,18 @@ def test_database_created_by_root
end
def test_grant_privileges_for_normal_user
@connection.expects(:execute).with("GRANT ALL PRIVILEGES ON my-app-db.* TO 'pat'@'localhost' IDENTIFIED BY 'wossname' WITH GRANT OPTION;")
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' => 'mysql',
......@@ -142,6 +151,13 @@ def test_sends_output_to_stderr_when_other_errors
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册