mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
79ce7d9af6
I have so. many. regrets. about using `spec_name` for database configurations and now I'm finally putting this mistake to an end. Back when I started multi-db work I assumed that eventually `connection_specification_name` (sometimes called `spec_name`) and `spec_name` for configurations would one day be the same thing. After 2 years I no longer believe they will ever be the same thing. This PR deprecates `spec_name` on database configurations in favor of `name`. It's the same behavior, just a better name, or at least a less confusing name. `connection_specification_name` refers to the parent class name (ie ActiveRecord::Base, AnimalsBase, etc) that holds the connection for it's models. In some places like ConnectionHandler it shortens this to `spec_name`, hence the major confusion. Recently I've been working with some new folks on database stuff and connection management and realize how confusing it was to explain that `db_config.spec_name` was not `spec_name` and `connection_specification_name`. Worse than that one is a symbole while the other is a class name. This was made even more complicated by the fact that `ActiveRecord::Base` used `primary` as the `connection_specification_name` until #38190. After spending 2 years with connection management I don't believe that we can ever use the symbols from the database configs as a way to connect the database without the class name being _somewhere_ because a db_config does not know who it's owner class is until it's been connected and a model has no idea what db_config belongs to it until it's connected. The model is the only way to tie a primary/writer config to a replica/reader config. This could change in the future but I don't see value in adding a class name to the db_configs before connection or telling a model what config belongs to it before connection. That would probably break a lot of application assumptions. If we do ever end up in that world, we can use name, because tbh `spec_name` and `connection_specification_name` were always confusing to me.
82 lines
3.3 KiB
Ruby
82 lines
3.3 KiB
Ruby
# 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", name: "primary")
|
|
expected_database = "#{base_db_config.database}-2"
|
|
|
|
ActiveRecord::Tasks::DatabaseTasks.stub(:reconstruct_from_schema, ->(db_config, _, _) {
|
|
assert_equal expected_database, db_config.database
|
|
}) do
|
|
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
|
|
FileUtils.rm_rf("db")
|
|
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", 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 database configuration
|
|
assert_equal expected_database, ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary").database
|
|
ensure
|
|
ActiveRecord::Base.configurations = prev_configs
|
|
ActiveRecord::Base.establish_connection(:arunit)
|
|
ENV["RAILS_ENV"] = previous_env
|
|
FileUtils.rm_rf("db")
|
|
end
|
|
|
|
def test_order_of_configurations_isnt_changed_by_test_databases
|
|
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" },
|
|
"replica" => { "adapter" => "sqlite3", "database" => "db/primary.sqlite3" }
|
|
}
|
|
}
|
|
|
|
idx = 42
|
|
base_configs_order = ActiveRecord::Base.configurations.configs_for(env_name: "arunit").map(&:name)
|
|
|
|
ActiveRecord::Tasks::DatabaseTasks.stub(:reconstruct_from_schema, ->(db_config, _, _) {
|
|
assert_equal base_configs_order, ActiveRecord::Base.configurations.configs_for(env_name: "arunit").map(&:name)
|
|
}) do
|
|
ActiveSupport::Testing::Parallelization.after_fork_hooks.each { |cb| cb.call(idx) }
|
|
end
|
|
ensure
|
|
ActiveRecord::Base.configurations = prev_configs
|
|
ActiveRecord::Base.establish_connection(:arunit)
|
|
ENV["RAILS_ENV"] = previous_env
|
|
FileUtils.rm_rf("db")
|
|
end
|
|
end
|
|
end
|