From 1f938f5265b790d9c5449011863bd25f021ca161 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 3 Jan 2020 14:38:30 -0500 Subject: [PATCH] Pass env_name as a string in test databases In 154abca we switched from using `Rails.env` to fetch the `env_name` to `ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_sym` which changed the type from a `String` to a `Symbol`. This commit brings things back to the original state, so we can find the configurations correctly! It also modifies the configuration in the configurations array, so that future connections can find the database with the updated keyword value. --- .../lib/active_record/test_databases.rb | 7 +++- .../test/cases/test_databases_test.rb | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/test_databases.rb b/activerecord/lib/active_record/test_databases.rb index 001d15245a..2c6cb69145 100644 --- a/activerecord/lib/active_record/test_databases.rb +++ b/activerecord/lib/active_record/test_databases.rb @@ -5,7 +5,7 @@ module ActiveRecord module TestDatabases # :nodoc: ActiveSupport::Testing::Parallelization.after_fork_hook do |i| - create_and_load_schema(i, env_name: ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_sym) + create_and_load_schema(i, env_name: ActiveRecord::ConnectionHandling::DEFAULT_ENV.call) end def self.create_and_load_schema(i, env_name:) @@ -20,7 +20,12 @@ def self.create_and_load_schema(i, env_name:) db_config.configuration_hash.merge(database: database) ) + # Reconstruct with the new configuration ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config_copy, ActiveRecord::Base.schema_format, nil) + + # Replace the original configuration with our replacement + ActiveRecord::Base.configurations.configurations.delete(db_config) + ActiveRecord::Base.configurations.configurations.push(db_config_copy) end ensure ActiveRecord::Base.establish_connection(ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_sym) diff --git a/activerecord/test/cases/test_databases_test.rb b/activerecord/test/cases/test_databases_test.rb index 6b873dd96a..19f5cd0256 100644 --- a/activerecord/test/cases/test_databases_test.rb +++ b/activerecord/test/cases/test_databases_test.rb @@ -1,11 +1,17 @@ # frozen_string_literal: true require "cases/helper" +require "active_record/test_databases" class TestDatabasesTest < ActiveRecord::TestCase unless in_memory_db? def test_databases_are_created previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "arunit" + prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, { + "arunit" => { + "primary" => { "adapter" => "sqlite3", "database" => "db/primary.sqlite3" } + } + } base_db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", spec_name: "primary") expected_database = "#{base_db_config.database}-2" @@ -16,6 +22,34 @@ def test_databases_are_created ActiveRecord::TestDatabases.create_and_load_schema(2, env_name: "arunit") end ensure + ActiveRecord::Base.configurations = prev_configs + ActiveRecord::Base.establish_connection(:arunit) + ENV["RAILS_ENV"] = previous_env + end + + def test_create_databases_after_fork + previous_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "arunit" + prev_configs, ActiveRecord::Base.configurations = ActiveRecord::Base.configurations, { + "arunit" => { + "primary" => { "adapter" => "sqlite3", "database" => "db/primary.sqlite3" } + } + } + + idx = 42 + base_db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", spec_name: "primary") + expected_database = "#{base_db_config.database}-#{idx}" + + ActiveRecord::Tasks::DatabaseTasks.stub(:reconstruct_from_schema, ->(db_config, _, _) { + assert_equal expected_database, db_config.database + }) do + ActiveSupport::Testing::Parallelization.after_fork_hooks.each { |cb| cb.call(idx) } + end + + # Updates the databse configuration + assert_equal expected_database, ActiveRecord::Base.configurations.configs_for(env_name: "arunit", spec_name: "primary").database + ensure + ActiveRecord::Base.configurations = prev_configs + ActiveRecord::Base.establish_connection(:arunit) ENV["RAILS_ENV"] = previous_env end end -- GitLab