diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index e6b2c42bd751debf25b23bbaa78dfbc8b5ae12b5..19c7f3bb1edffe739db1c6e09f48fd2609066148 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Skip test database when running `db:create` or `db:drop` in development + with `DATABASE_URL` set. + + *Brian Buchalter* + * Don't allow mutations on the datbase configurations hash. Freeze the configurations hash to disallow directly changing the configurations hash. If applications need to change the hash, for example to create adatabases for parallelization, they should use the `DatabaseConfig` object directly. diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 1e08b2d73d0fb5dd7717e3c96a6ba3e7338fb2a9..db7fc9058d6f29a0e6d58084518a8acf5a3e65f8 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -34,7 +34,7 @@ db_namespace = namespace :db do end end - desc "Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases." + desc "Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases, except when DATABASE_URL is present." task create: [:load_config] do ActiveRecord::Tasks::DatabaseTasks.create_current end @@ -53,7 +53,7 @@ db_namespace = namespace :db do end end - desc "Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases." + desc "Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases, except when DATABASE_URL is present." task drop: [:load_config, :check_protected_environments] do db_namespace["drop:_unsafe"].invoke end @@ -73,7 +73,7 @@ db_namespace = namespace :db do ActiveRecord::Tasks::DatabaseTasks.truncate_all end - # desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:purge:all to purge all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases." + # desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:purge:all to purge all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases, except when DATABASE_URL is present." task purge: [:load_config, :check_protected_environments] do ActiveRecord::Tasks::DatabaseTasks.purge_current end diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index bb14346e0a0c3daaba50837924500b33cd8840b4..891d58db4992321dbf32de11e88d27eeb204ea65 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -484,7 +484,7 @@ def class_for_adapter(adapter) def each_current_configuration(environment, spec_name = nil) environments = [environment] - environments << "test" if environment == "development" + environments << "test" if environment == "development" && !ENV["DATABASE_URL"] environments.each do |env| ActiveRecord::Base.configurations.configs_for(env_name: env).each do |db_config| diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 451b2e76c2807d524dbb581741ebfde7c56c4343..191850741f73cacfa57e87394076eeeff4cb7879 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -60,6 +60,28 @@ def db_create_with_warning(expected_database) db_create_and_drop database_url_db_name end + test "db:create and db:drop with database URL don't use YAML DBs" do + require "#{app_path}/config/environment" + set_database_url + + File.write("#{app_path}/config/database.yml", <<~YAML) + test: + adapter: sqlite3 + database: db/test.sqlite3 + + development: + adapter: sqlite3 + database: db/development.sqlite3 + YAML + + with_rails_env "development" do + db_create_and_drop database_url_db_name do + assert_not File.exist?("#{app_path}/db/test.sqlite3") + assert_not File.exist?("#{app_path}/db/development.sqlite3") + end + end + end + test "db:create and db:drop respect environment setting" do app_file "config/database.yml", <<-YAML <% 1 %>