From 756cea1b6de7b6d6202f2517668a2165a0fb0d2f Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 19 Apr 2023 15:34:32 -0400 Subject: [PATCH] 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. --- activerecord/Rakefile | 43 ++++++++----- activerecord/test/config.example.yml | 94 ++++++++++++++++++---------- 2 files changed, 88 insertions(+), 49 deletions(-) diff --git a/activerecord/Rakefile b/activerecord/Rakefile index fd9c0eafbc..d9ce82741e 100644 --- a/activerecord/Rakefile +++ b/activerecord/Rakefile @@ -209,11 +209,20 @@ end namespace :db do namespace :mysql do - connection_arguments = lambda do |connection_name| - config = ARTest.config["connections"]["mysql2"][connection_name] - ["--user=#{config["username"]}", ("--password=#{config["password"]}" if config["password"]), ("--host=#{config["host"]}" if config["host"]), ("--socket=#{config["socket"]}" if config["socket"])].join(" ") + mysql2_config = ARTest.config["connections"]["mysql2"] + mysql2_connection_arguments = lambda do |connection_name| + mysql2_connection = mysql2_config[connection_name] + ["--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(" ") end + trilogy_config = ARTest.config["connections"]["trilogy"] + trilogy_connection_arguments = lambda do |connection_name| + trilogy_connection = trilogy_config[connection_name] + ["--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(" ") + end + + mysql_configs = [mysql2_config, trilogy_config] + desc "Create the MySQL Rails User" task :build_user do if ENV["MYSQL_CODESPACES"] @@ -226,26 +235,30 @@ namespace :db do 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';" ) + mysql_configs.each do |config| + %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 end desc "Build the MySQL test databases" 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" ) + %x( mysql #{mysql2_connection_arguments["arunit"]} -e "create DATABASE IF NOT EXISTS #{mysql2_config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" ) + %x( mysql #{mysql2_connection_arguments["arunit2"]} -e "create DATABASE IF NOT EXISTS #{mysql2_config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" ) + %x( mysql #{trilogy_connection_arguments["arunit"]} -e "create DATABASE IF NOT EXISTS #{trilogy_config["arunit"]["database"]} DEFAULT CHARACTER SET utf8mb4" ) + %x( mysql #{trilogy_connection_arguments["arunit2"]} -e "create DATABASE IF NOT EXISTS #{trilogy_config["arunit2"]["database"]} DEFAULT CHARACTER SET utf8mb4" ) end desc "Drop the MySQL test databases" task :drop do - config = ARTest.config["connections"]["mysql2"] - %x( mysqladmin #{connection_arguments["arunit"]} -f drop #{config["arunit"]["database"]} ) - %x( mysqladmin #{connection_arguments["arunit2"]} -f drop #{config["arunit2"]["database"]} ) + %x( mysql #{mysql2_connection_arguments["arunit"]} -e "drop database IF EXISTS #{mysql2_config["arunit"]["database"]}" ) + %x( mysql #{mysql2_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{mysql2_config["arunit2"]["database"]}" ) + + %x( mysql #{trilogy_connection_arguments["arunit"]} -e "drop database IF EXISTS #{trilogy_config["arunit"]["database"]}" ) + %x( mysql #{trilogy_connection_arguments["arunit2"]} -e "drop database IF EXISTS #{trilogy_config["arunit2"]["database"]}" ) end desc "Rebuild the MySQL test databases" diff --git a/activerecord/test/config.example.yml b/activerecord/test/config.example.yml index f290a0b2ee..ae4c21acd7 100644 --- a/activerecord/test/config.example.yml +++ b/activerecord/test/config.example.yml @@ -1,37 +1,5 @@ default_connection: <%= defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3' %> -mysql: &mysql - arunit: - username: rails - encoding: utf8mb4 - collation: utf8mb4_unicode_ci - <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> - prepared_statements: true - <% else %> - prepared_statements: false - <% end %> - <% if ENV['MYSQL_HOST'] %> - host: <%= ENV['MYSQL_HOST'] %> - <% end %> - <% if ENV['MYSQL_SOCK'] %> - socket: "<%= ENV['MYSQL_SOCK'] %>" - <% end %> - arunit2: - username: rails - encoding: utf8mb4 - collation: utf8mb4_general_ci - <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> - prepared_statements: true - <% else %> - prepared_statements: false - <% end %> - <% if ENV['MYSQL_HOST'] %> - host: <%= ENV['MYSQL_HOST'] %> - <% end %> - <% if ENV['MYSQL_SOCK'] %> - socket: "<%= ENV['MYSQL_SOCK'] %>" - <% end %> - connections: jdbcderby: arunit: activerecord_unittest @@ -68,7 +36,36 @@ connections: timeout: 5000 mysql2: - <<: *mysql + arunit: + username: rails + encoding: utf8mb4 + collation: utf8mb4_unicode_ci + <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> + prepared_statements: true + <% else %> + prepared_statements: false + <% end %> + <% if ENV['MYSQL_HOST'] %> + host: <%= ENV['MYSQL_HOST'] %> + <% end %> + <% if ENV['MYSQL_SOCK'] %> + socket: "<%= ENV['MYSQL_SOCK'] %>" + <% end %> + arunit2: + username: rails + encoding: utf8mb4 + collation: utf8mb4_general_ci + <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> + prepared_statements: true + <% else %> + prepared_statements: false + <% end %> + <% if ENV['MYSQL_HOST'] %> + host: <%= ENV['MYSQL_HOST'] %> + <% end %> + <% if ENV['MYSQL_SOCK'] %> + socket: "<%= ENV['MYSQL_SOCK'] %>" + <% end %> oracle: arunit: @@ -112,4 +109,33 @@ connections: database: ':memory:' trilogy: - <<: *mysql + arunit: + username: rails + encoding: utf8mb4 + collation: utf8mb4_unicode_ci + <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> + prepared_statements: true + <% else %> + prepared_statements: false + <% end %> + <% if ENV['MYSQL_HOST'] %> + host: <%= ENV['MYSQL_HOST'] %> + <% end %> + <% if ENV['MYSQL_SOCK'] %> + socket: "<%= ENV['MYSQL_SOCK'] %>" + <% end %> + arunit2: + username: rails + encoding: utf8mb4 + collation: utf8mb4_general_ci + <% if ENV['MYSQL_PREPARED_STATEMENTS'] %> + prepared_statements: true + <% else %> + prepared_statements: false + <% end %> + <% if ENV['MYSQL_HOST'] %> + host: <%= ENV['MYSQL_HOST'] %> + <% end %> + <% if ENV['MYSQL_SOCK'] %> + socket: "<%= ENV['MYSQL_SOCK'] %>" + <% end %>