Merge pull request #44707 from rails/create-users-when-creating-dbs

Auto-create user and grant privs when creating dbs
This commit is contained in:
Eileen M. Uchitelle 2022-03-17 13:28:30 -04:00 committed by GitHub
commit 0169d15bc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View File

@ -14,11 +14,5 @@ sudo su postgres -c "createdb -O vscode -E UTF8 -T template0 activerecord_unitte
sudo su postgres -c "createdb -O vscode -E UTF8 -T template0 activerecord_unittest2"
# Create MySQL database and databases
MYSQL_PWD=root sudo mysql -uroot <<SQL
CREATE USER 'rails'@'localhost';
CREATE DATABASE activerecord_unittest DEFAULT CHARACTER SET utf8mb4;
CREATE DATABASE activerecord_unittest2 DEFAULT CHARACTER SET utf8mb4;
GRANT ALL PRIVILEGES ON activerecord_unittest.* to 'rails'@'localhost';
GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost';
GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to 'rails'@'localhost';
SQL
cd activerecord
MYSQL_CODESPACES=1 bundle exec rake db:mysql:build

View File

@ -201,8 +201,24 @@ namespace :db do
["--user=#{config["username"]}", "--password=#{config["password"]}", ("--host=#{config["host"]}" if config["host"]), ("--socket=#{config["socket"]}" if config["socket"])].join(" ")
end
desc "Create the MySQL Rails User"
task :build_user do
if ENV["MYSQL_CODESPACES"]
mysql_command = "sudo mysql -uroot -proot -e"
else
mysql_command = "mysql -uroot -e"
end
config = ARTest.config["connections"]["mysql2"]
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit"]["username"]}'@'localhost';" )
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit2"]["username"]}'@'localhost';" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit"]["database"]}.* to '#{config["arunit"]["username"]}'@'localhost'" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit2"]["database"]}.* to '#{config["arunit2"]["username"]}'@'localhost'" )
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to '#{config["arunit"]["username"]}'@'localhost';" )
end
desc "Build the MySQL test databases"
task :build do
task build: ["db:mysql:build_user"] do
config = ARTest.config["connections"]["mysql2"]
%x( mysql #{connection_arguments["arunit"]} -e "create DATABASE #{config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
%x( mysql #{connection_arguments["arunit2"]} -e "create DATABASE #{config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )