1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Database tasks can SKIP_TEST_DATABASE with environment variable

Co-Authored-By: Bob Whitelock <bob.whitelock1@gmail.com>
Co-Authored-By: Rafael França <rafael@franca.dev>
Co-Authored-By: KapilSachdev <kapilsachdev03@gmail.com>
This commit is contained in:
Jason Schweier 2020-04-23 10:56:17 -04:00
parent 77fc1c2b8e
commit 0c678edb84
3 changed files with 28 additions and 1 deletions

View file

@ -1,3 +1,7 @@
* 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.

View file

@ -494,7 +494,7 @@ module ActiveRecord
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|

View file

@ -447,6 +447,29 @@ module ActiveRecord
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