From 85d371ed0619e4a516ceec066619e22dc1b9827f Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Mon, 7 Mar 2022 11:28:21 -0500 Subject: [PATCH] fix namespaced schema load ignoring protected env This makes the task requirements for db:schema:load:namespace the same as db:schema:load (adding check_protected_environments) --- .../lib/active_record/railties/databases.rake | 2 +- railties/test/application/rake/dbs_test.rb | 18 ++++++++++++++++++ .../test/application/rake/multi_dbs_test.rb | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index bae25bf46a..6d27ff8a94 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -484,7 +484,7 @@ db_namespace = namespace :db do namespace :load do ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name| desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) into the #{name} database" - task name => :load_config do + task name => [:load_config, :check_protected_environments] do original_db_config = ActiveRecord::Base.connection_db_config db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name) ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config) diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index e4a9340096..b25f0018db 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -723,6 +723,24 @@ module ApplicationTests assert_match(/Dropped database/, repeat_output) end end + + test "destructive tasks are protected" do + add_to_config "config.active_record.protected_environments = ['development', 'test']" + + require "#{app_path}/config/environment" + + Dir.chdir(app_path) do + rails "generate", "model", "book", "title:string" + rails "db:migrate" + + destructive_tasks = ["db:drop:all", "db:drop", "db:purge:all", "db:truncate_all", "db:purge", "db:schema:load", "db:test:purge"] + + destructive_tasks.each do |task| + error = assert_raises("#{task} did not raise ActiveRecord::ProtectedEnvironmentError") { rails task } + assert_match(/ActiveRecord::ProtectedEnvironmentError/, error.message) + end + end + end end end end diff --git a/railties/test/application/rake/multi_dbs_test.rb b/railties/test/application/rake/multi_dbs_test.rb index 22f66848a9..a6a64b51bb 100644 --- a/railties/test/application/rake/multi_dbs_test.rb +++ b/railties/test/application/rake/multi_dbs_test.rb @@ -1135,6 +1135,24 @@ module ApplicationTests assert_equal "true", animals_db_exists.call end end + + test "destructive tasks are protected" do + add_to_config "config.active_record.protected_environments = ['development', 'test']" + + require "#{app_path}/config/environment" + + Dir.chdir(app_path) do + generate_models_for_animals + rails "db:migrate" + + destructive_tasks = ["db:drop:animals", "db:schema:load:animals", "db:test:purge:animals"] + + destructive_tasks.each do |task| + error = assert_raises("#{task} did not raise ActiveRecord::ProtectedEnvironmentError") { rails task } + assert_match(/ActiveRecord::ProtectedEnvironmentError/, error.message) + end + end + end end end end