未验证 提交 0bce8134 编写于 作者: E Eileen M. Uchitelle 提交者: GitHub

Merge pull request #39027 from jmks/skip_test_database_with_environment_variable

Database tasks can SKIP_TEST_DATABASE with environment variable
* Add `SKIP_TEST_DATABASE` environment variable to disable modifying the test database when `rails db:create` and `rails db:drop` are called.
*Jason Schweier*
* `connects_to` can only be called on `ActiveRecord::Base` or abstract classes.
Ensure that `connects_to` can only be called from `ActiveRecord::Base` or abstract classes. This protects the application from opening duplicate or too many connections.
......
......@@ -494,7 +494,7 @@ def class_for_adapter(adapter)
def each_current_configuration(environment, name = nil)
environments = [environment]
environments << "test" if environment == "development" && !ENV["DATABASE_URL"]
environments << "test" if environment == "development" && !ENV["SKIP_TEST_DATABASE"] && !ENV["DATABASE_URL"]
environments.each do |env|
ActiveRecord::Base.configurations.configs_for(env_name: env).each do |db_config|
......
......@@ -447,6 +447,29 @@ def test_creates_test_and_development_databases_when_rails_env_is_development
ENV["RAILS_ENV"] = old_env
end
def test_creates_development_database_without_test_database_when_skip_test_database
old_env = ENV["RAILS_ENV"]
ENV["RAILS_ENV"] = "development"
ENV["SKIP_TEST_DATABASE"] = "true"
with_stubbed_configurations_establish_connection do
assert_called_with(
ActiveRecord::Tasks::DatabaseTasks,
:create,
[
[config_for("development", "primary")]
],
) do
ActiveRecord::Tasks::DatabaseTasks.create_current(
ActiveSupport::StringInquirer.new("development")
)
end
end
ensure
ENV["RAILS_ENV"] = old_env
ENV.delete("SKIP_TEST_DATABASE")
end
def test_establishes_connection_for_the_given_environments
ActiveRecord::Tasks::DatabaseTasks.stub(:create, nil) do
assert_called_with(ActiveRecord::Base, :establish_connection, [:development]) do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册