Merge pull request #9762 from senny/8079_do_not_grant_on_root_user

`rake db:create` does not change permissions of root user.
This commit is contained in:
Rafael Mendonça França 2013-03-17 12:35:54 -07:00
commit c4a7c31581
3 changed files with 26 additions and 3 deletions

View File

@ -1,5 +1,10 @@
## 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".

View File

@ -26,7 +26,9 @@ module ActiveRecord
$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}"

View File

@ -71,7 +71,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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