Skip to content

Commit 57fe7e7

Browse files
authored
Merge pull request rails#53252 from davidstosik/sto/activerecord-rakefile
Refactor `activerecord/Rakefile` and add `port` option to the config
2 parents 3e28346 + d009741 commit 57fe7e7

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

activerecord/Rakefile

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -207,52 +207,46 @@ end
207207

208208
namespace :db do
209209
namespace :mysql do
210-
mysql2_config = ARTest.config["connections"]["mysql2"]
211-
mysql2_connection_arguments = lambda do |connection_name|
212-
mysql2_connection = mysql2_config[connection_name]
213-
["--user=#{mysql2_connection["username"]}", ("--password=#{mysql2_connection["password"]}" if mysql2_connection["password"]), ("--host=#{mysql2_connection["host"]}" if mysql2_connection["host"]), ("--socket=#{mysql2_connection["socket"]}" if mysql2_connection["socket"])].join(" ")
210+
mysql_configs = ARTest.config["connections"]
211+
.values_at("mysql2", "trilogy")
212+
.flat_map { |connections| connections.values_at("arunit", "arunit2") }
213+
mysql_connection_arguments = lambda do |connection, exclude_user_password: false|
214+
connection = connection.except("username", "password") if exclude_user_password
215+
[
216+
("--user=#{connection["username"]}" if connection["username"]),
217+
("--password=#{connection["password"]}" if connection["password"]),
218+
("--host=#{connection["host"]}" if connection["host"]),
219+
("--port=#{connection["port"]}" if connection["port"]),
220+
("--socket=#{connection["socket"]}" if connection["socket"])
221+
].join(" ")
214222
end
215223

216-
trilogy_config = ARTest.config["connections"]["trilogy"]
217-
trilogy_connection_arguments = lambda do |connection_name|
218-
trilogy_connection = trilogy_config[connection_name]
219-
["--user=#{trilogy_connection["username"]}", ("--password=#{trilogy_connection["password"]}" if trilogy_connection["password"]), ("--host=#{trilogy_connection["host"]}" if trilogy_connection["host"]), ("--socket=#{trilogy_connection["socket"]}" if trilogy_connection["socket"])].join(" ")
220-
end
221-
222-
mysql_configs = [mysql2_config, trilogy_config]
223-
224224
desc "Create the MySQL Rails User"
225225
task :build_user do
226-
if ENV["MYSQL_CODESPACES"]
227-
mysql_command = "mysql -uroot -proot -e"
228-
else
229-
mysql_command = "mysql -uroot -e"
230-
end
231-
232-
mysql_configs.each do |config|
233-
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit"]["username"]}'@'%';" )
234-
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit2"]["username"]}'@'%';" )
235-
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit"]["database"]}.* to '#{config["arunit"]["username"]}'@'%'" )
236-
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit2"]["database"]}.* to '#{config["arunit2"]["username"]}'@'%'" )
237-
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to '#{config["arunit"]["username"]}'@'%';" )
226+
mysql_configs.each do |connection|
227+
mysql_command = [
228+
"mysql -uroot",
229+
("-proot" if ENV["MYSQL_CODESPACES"]),
230+
mysql_connection_arguments[connection, exclude_user_password: true],
231+
].join(" ")
232+
%x( #{mysql_command} -e "CREATE USER IF NOT EXISTS '#{connection["username"]}'@'%';" )
233+
%x( #{mysql_command} -e "GRANT ALL PRIVILEGES ON #{connection["database"]}.* to '#{connection["username"]}'@'%'" )
234+
%x( #{mysql_command} -e "GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to '#{connection["username"]}'@'%';" )
238235
end
239236
end
240237

241238
desc "Build the MySQL test databases"
242239
task build: ["db:mysql:build_user"] do
243-
%x( mysql #{mysql2_connection_arguments["arunit"]} -e "create DATABASE IF NOT EXISTS #{mysql2_config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
244-
%x( mysql #{mysql2_connection_arguments["arunit2"]} -e "create DATABASE IF NOT EXISTS #{mysql2_config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
245-
%x( mysql #{trilogy_connection_arguments["arunit"]} -e "create DATABASE IF NOT EXISTS #{trilogy_config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
246-
%x( mysql #{trilogy_connection_arguments["arunit2"]} -e "create DATABASE IF NOT EXISTS #{trilogy_config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
240+
mysql_configs.each do |connection|
241+
%x( mysql #{mysql_connection_arguments[connection]} -e "create DATABASE IF NOT EXISTS #{connection["database"]} DEFAULT CHARACTER SET utf8mb4" )
242+
end
247243
end
248244

249245
desc "Drop the MySQL test databases"
250246
task drop: ["db:mysql:build_user"] do
251-
%x( mysql #{mysql2_connection_arguments["arunit"]} -e "drop database IF EXISTS #{mysql2_config["arunit"]["database"]}" )
252-
%x( mysql #{mysql2_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{mysql2_config["arunit2"]["database"]}" )
253-
254-
%x( mysql #{trilogy_connection_arguments["arunit"]} -e "drop database IF EXISTS #{trilogy_config["arunit"]["database"]}" )
255-
%x( mysql #{trilogy_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{trilogy_config["arunit2"]["database"]}" )
247+
mysql_configs.each do |connection|
248+
%x( mysql #{mysql_connection_arguments[connection]} -e "drop database IF EXISTS #{connection["database"]}" )
249+
end
256250
end
257251

258252
desc "Rebuild the MySQL test databases"

activerecord/test/config.example.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ connections:
1212
<% if ENV['MYSQL_HOST'] %>
1313
host: <%= ENV['MYSQL_HOST'] %>
1414
<% end %>
15+
<% if ENV['MYSQL_PORT'] %>
16+
port: <%= ENV['MYSQL_PORT'] %>
17+
<% end %>
1518
<% if ENV['MYSQL_SOCK'] %>
1619
socket: "<%= ENV['MYSQL_SOCK'] %>"
1720
<% end %>
@@ -27,6 +30,9 @@ connections:
2730
<% if ENV['MYSQL_HOST'] %>
2831
host: <%= ENV['MYSQL_HOST'] %>
2932
<% end %>
33+
<% if ENV['MYSQL_PORT'] %>
34+
port: <%= ENV['MYSQL_PORT'] %>
35+
<% end %>
3036
<% if ENV['MYSQL_SOCK'] %>
3137
socket: "<%= ENV['MYSQL_SOCK'] %>"
3238
<% end %>
@@ -40,6 +46,9 @@ connections:
4046
<% if ENV['MYSQL_HOST'] %>
4147
host: <%= ENV['MYSQL_HOST'] %>
4248
<% end %>
49+
<% if ENV['MYSQL_PORT'] %>
50+
port: <%= ENV['MYSQL_PORT'] %>
51+
<% end %>
4352
<% if ENV['MYSQL_SOCK'] %>
4453
socket: "<%= ENV['MYSQL_SOCK'] %>"
4554
<% end %>
@@ -51,6 +60,9 @@ connections:
5160
<% if ENV['MYSQL_HOST'] %>
5261
host: <%= ENV['MYSQL_HOST'] %>
5362
<% end %>
63+
<% if ENV['MYSQL_PORT'] %>
64+
port: <%= ENV['MYSQL_PORT'] %>
65+
<% end %>
5466
<% if ENV['MYSQL_SOCK'] %>
5567
socket: "<%= ENV['MYSQL_SOCK'] %>"
5668
<% end %>

0 commit comments

Comments
 (0)