Skip to content

Commit 756cea1

Browse files
committed
Fix database configuration and generation
I tried using the same config for mysql2 and trilogy but instead it eneded up breaking the trilogy tests - they were only running in mysql2 mode. I wanted to do this so that the rake tasks for trilogy wouldn't need to be duplicated but since that didn't work out quite right, I've decide to duplicate the calls and add if exists / if not exists where applicable. The configs should be the same but this will make sure that if they do deviate, the dbs are always created/dropped.
1 parent 484dcb6 commit 756cea1

File tree

2 files changed

+88
-49
lines changed

2 files changed

+88
-49
lines changed

activerecord/Rakefile

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,20 @@ end
209209

210210
namespace :db do
211211
namespace :mysql do
212-
connection_arguments = lambda do |connection_name|
213-
config = ARTest.config["connections"]["mysql2"][connection_name]
214-
["--user=#{config["username"]}", ("--password=#{config["password"]}" if config["password"]), ("--host=#{config["host"]}" if config["host"]), ("--socket=#{config["socket"]}" if config["socket"])].join(" ")
212+
mysql2_config = ARTest.config["connections"]["mysql2"]
213+
mysql2_connection_arguments = lambda do |connection_name|
214+
mysql2_connection = mysql2_config[connection_name]
215+
["--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(" ")
215216
end
216217

218+
trilogy_config = ARTest.config["connections"]["trilogy"]
219+
trilogy_connection_arguments = lambda do |connection_name|
220+
trilogy_connection = trilogy_config[connection_name]
221+
["--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(" ")
222+
end
223+
224+
mysql_configs = [mysql2_config, trilogy_config]
225+
217226
desc "Create the MySQL Rails User"
218227
task :build_user do
219228
if ENV["MYSQL_CODESPACES"]
@@ -226,26 +235,30 @@ namespace :db do
226235
mysql_command = "mysql -uroot -e"
227236
end
228237

229-
config = ARTest.config["connections"]["mysql2"]
230-
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit"]["username"]}'@'localhost';" )
231-
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit2"]["username"]}'@'localhost';" )
232-
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit"]["database"]}.* to '#{config["arunit"]["username"]}'@'localhost'" )
233-
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit2"]["database"]}.* to '#{config["arunit2"]["username"]}'@'localhost'" )
234-
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to '#{config["arunit"]["username"]}'@'localhost';" )
238+
mysql_configs.each do |config|
239+
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit"]["username"]}'@'localhost';" )
240+
%x( #{mysql_command} "CREATE USER IF NOT EXISTS '#{config["arunit2"]["username"]}'@'localhost';" )
241+
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit"]["database"]}.* to '#{config["arunit"]["username"]}'@'localhost'" )
242+
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON #{config["arunit2"]["database"]}.* to '#{config["arunit2"]["username"]}'@'localhost'" )
243+
%x( #{mysql_command} "GRANT ALL PRIVILEGES ON inexistent_activerecord_unittest.* to '#{config["arunit"]["username"]}'@'localhost';" )
244+
end
235245
end
236246

237247
desc "Build the MySQL test databases"
238248
task build: ["db:mysql:build_user"] do
239-
config = ARTest.config["connections"]["mysql2"]
240-
%x( mysql #{connection_arguments["arunit"]} -e "create DATABASE #{config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
241-
%x( mysql #{connection_arguments["arunit2"]} -e "create DATABASE #{config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
249+
%x( mysql #{mysql2_connection_arguments["arunit"]} -e "create DATABASE IF NOT EXISTS #{mysql2_config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
250+
%x( mysql #{mysql2_connection_arguments["arunit2"]} -e "create DATABASE IF NOT EXISTS #{mysql2_config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
251+
%x( mysql #{trilogy_connection_arguments["arunit"]} -e "create DATABASE IF NOT EXISTS #{trilogy_config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
252+
%x( mysql #{trilogy_connection_arguments["arunit2"]} -e "create DATABASE IF NOT EXISTS #{trilogy_config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" )
242253
end
243254

244255
desc "Drop the MySQL test databases"
245256
task :drop do
246-
config = ARTest.config["connections"]["mysql2"]
247-
%x( mysqladmin #{connection_arguments["arunit"]} -f drop #{config["arunit"]["database"]} )
248-
%x( mysqladmin #{connection_arguments["arunit2"]} -f drop #{config["arunit2"]["database"]} )
257+
%x( mysql #{mysql2_connection_arguments["arunit"]} -e "drop database IF EXISTS #{mysql2_config["arunit"]["database"]}" )
258+
%x( mysql #{mysql2_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{mysql2_config["arunit2"]["database"]}" )
259+
260+
%x( mysql #{trilogy_connection_arguments["arunit"]} -e "drop database IF EXISTS #{trilogy_config["arunit"]["database"]}" )
261+
%x( mysql #{trilogy_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{trilogy_config["arunit2"]["database"]}" )
249262
end
250263

251264
desc "Rebuild the MySQL test databases"

activerecord/test/config.example.yml

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
default_connection: <%= defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3' %>
22

3-
mysql: &mysql
4-
arunit:
5-
username: rails
6-
encoding: utf8mb4
7-
collation: utf8mb4_unicode_ci
8-
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
9-
prepared_statements: true
10-
<% else %>
11-
prepared_statements: false
12-
<% end %>
13-
<% if ENV['MYSQL_HOST'] %>
14-
host: <%= ENV['MYSQL_HOST'] %>
15-
<% end %>
16-
<% if ENV['MYSQL_SOCK'] %>
17-
socket: "<%= ENV['MYSQL_SOCK'] %>"
18-
<% end %>
19-
arunit2:
20-
username: rails
21-
encoding: utf8mb4
22-
collation: utf8mb4_general_ci
23-
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
24-
prepared_statements: true
25-
<% else %>
26-
prepared_statements: false
27-
<% end %>
28-
<% if ENV['MYSQL_HOST'] %>
29-
host: <%= ENV['MYSQL_HOST'] %>
30-
<% end %>
31-
<% if ENV['MYSQL_SOCK'] %>
32-
socket: "<%= ENV['MYSQL_SOCK'] %>"
33-
<% end %>
34-
353
connections:
364
jdbcderby:
375
arunit: activerecord_unittest
@@ -68,7 +36,36 @@ connections:
6836
timeout: 5000
6937

7038
mysql2:
71-
<<: *mysql
39+
arunit:
40+
username: rails
41+
encoding: utf8mb4
42+
collation: utf8mb4_unicode_ci
43+
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
44+
prepared_statements: true
45+
<% else %>
46+
prepared_statements: false
47+
<% end %>
48+
<% if ENV['MYSQL_HOST'] %>
49+
host: <%= ENV['MYSQL_HOST'] %>
50+
<% end %>
51+
<% if ENV['MYSQL_SOCK'] %>
52+
socket: "<%= ENV['MYSQL_SOCK'] %>"
53+
<% end %>
54+
arunit2:
55+
username: rails
56+
encoding: utf8mb4
57+
collation: utf8mb4_general_ci
58+
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
59+
prepared_statements: true
60+
<% else %>
61+
prepared_statements: false
62+
<% end %>
63+
<% if ENV['MYSQL_HOST'] %>
64+
host: <%= ENV['MYSQL_HOST'] %>
65+
<% end %>
66+
<% if ENV['MYSQL_SOCK'] %>
67+
socket: "<%= ENV['MYSQL_SOCK'] %>"
68+
<% end %>
7269

7370
oracle:
7471
arunit:
@@ -112,4 +109,33 @@ connections:
112109
database: ':memory:'
113110

114111
trilogy:
115-
<<: *mysql
112+
arunit:
113+
username: rails
114+
encoding: utf8mb4
115+
collation: utf8mb4_unicode_ci
116+
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
117+
prepared_statements: true
118+
<% else %>
119+
prepared_statements: false
120+
<% end %>
121+
<% if ENV['MYSQL_HOST'] %>
122+
host: <%= ENV['MYSQL_HOST'] %>
123+
<% end %>
124+
<% if ENV['MYSQL_SOCK'] %>
125+
socket: "<%= ENV['MYSQL_SOCK'] %>"
126+
<% end %>
127+
arunit2:
128+
username: rails
129+
encoding: utf8mb4
130+
collation: utf8mb4_general_ci
131+
<% if ENV['MYSQL_PREPARED_STATEMENTS'] %>
132+
prepared_statements: true
133+
<% else %>
134+
prepared_statements: false
135+
<% end %>
136+
<% if ENV['MYSQL_HOST'] %>
137+
host: <%= ENV['MYSQL_HOST'] %>
138+
<% end %>
139+
<% if ENV['MYSQL_SOCK'] %>
140+
socket: "<%= ENV['MYSQL_SOCK'] %>"
141+
<% end %>

0 commit comments

Comments
 (0)