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)
This commit is contained in:
Hartley McGuire 2022-03-07 11:28:21 -05:00
parent 645239817d
commit 85d371ed06
3 changed files with 37 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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