From b73b68c0db84128d91ab70567101f5542ed3ad27 Mon Sep 17 00:00:00 2001 From: Dax Kelson Date: Fri, 9 Oct 2026 16:00:03 -0600 Subject: [PATCH] Fixes #39892 - Fix templates:cleanup on Foreman 3 Setting::TemplateSync went away when Foreman moved settings into the settings registry, so foreman-rake templates:cleanup has been failing every time with: NameError: uninitialized constant Setting::TemplateSync Delete the stored template_sync_* settings directly. Also remove the plugin's permissions and the default role filters that held them; otherwise import_templates, export_templates and view_template_syncs stay in the database after the plugin is uninstalled. --- app/services/foreman_templates/cleaner.rb | 15 ++++++++++++- test/unit/cleaner_test.rb | 27 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/unit/cleaner_test.rb diff --git a/app/services/foreman_templates/cleaner.rb b/app/services/foreman_templates/cleaner.rb index 84d0e2ab..0eed5f57 100644 --- a/app/services/foreman_templates/cleaner.rb +++ b/app/services/foreman_templates/cleaner.rb @@ -1,13 +1,26 @@ module ForemanTemplates class Cleaner + PERMISSIONS = %w[import_templates export_templates view_template_syncs].freeze + def clean_up! remove_settings + remove_permissions end private def remove_settings - Setting::TemplateSync.destroy_all + Setting.where('name LIKE ?', 'template_sync_%').delete_all + end + + def remove_permissions + permission_ids = Permission.where(name: PERMISSIONS).pluck(:id) + return if permission_ids.empty? + + filter_ids = Filtering.where(permission_id: permission_ids).distinct.pluck(:filter_id) + Filtering.where(permission_id: permission_ids).delete_all + Filter.where(id: filter_ids).find_each { |filter| filter.destroy if filter.filterings.reload.empty? } + Permission.where(id: permission_ids).delete_all end end end diff --git a/test/unit/cleaner_test.rb b/test/unit/cleaner_test.rb new file mode 100644 index 00000000..8280dbf0 --- /dev/null +++ b/test/unit/cleaner_test.rb @@ -0,0 +1,27 @@ +require 'test_plugin_helper' + +module ForemanTemplates + class CleanerTest < ActiveSupport::TestCase + setup do + @import = Permission.find_or_create_by!(name: 'import_templates', resource_type: 'Template') + @view = Permission.find_or_create_by!(name: 'view_template_syncs', resource_type: 'Template') + @role = FactoryBot.create(:role) + @plugin_filter = FactoryBot.create(:filter, role: @role, permissions: [@import, @view]) + @other_filter = FactoryBot.create(:filter, role: @role, permissions: Permission.where(name: 'view_architectures')) + Setting[:template_sync_dirname] = '/cleaner-test' + end + + test 'removes the stored template sync settings' do + Cleaner.new.clean_up! + assert_empty Setting.where('name LIKE ?', 'template_sync_%') + end + + test 'removes the plugin permissions and the filters left empty' do + Cleaner.new.clean_up! + assert_empty Permission.where(name: Cleaner::PERMISSIONS) + refute Filter.exists?(@plugin_filter.id) + assert Filter.exists?(@other_filter.id) + assert Role.exists?(@role.id) + end + end +end