From 7458f70130799eeea60a47575001672509bbbec3 Mon Sep 17 00:00:00 2001 From: Jeremy Lenz Date: Wed, 28 Feb 2024 12:01:35 -0500 Subject: [PATCH] Fixes #37137 - Use insert_all when creating available module streams (#10878) * Fixes #37137 - Use insert_all when creating available module streams --- .../concerns/host_managed_extensions.rb | 63 +++++++++++++------ .../concerns/host_managed_extensions_test.rb | 5 ++ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/app/models/katello/concerns/host_managed_extensions.rb b/app/models/katello/concerns/host_managed_extensions.rb index c483fa6b772..b0d2210dc56 100644 --- a/app/models/katello/concerns/host_managed_extensions.rb +++ b/app/models/katello/concerns/host_managed_extensions.rb @@ -325,40 +325,67 @@ def import_enabled_repositories(repos) content_facet.update_repositories_by_paths(paths.compact) end - def import_module_streams(module_streams) - streams = {} - module_streams.each do |module_stream| - stream = AvailableModuleStream.create_or_find_by!(name: module_stream["name"], - context: module_stream["context"], - stream: module_stream["stream"]) - streams[stream.id] = module_stream + def available_module_stream_id_from(name:, stream:, context:) + @indexed_available_module_streams ||= Katello::AvailableModuleStream.all.index_by do |available_module_stream| + "#{available_module_stream.name}-#{available_module_stream.stream}-#{available_module_stream.context}" end - sync_available_module_stream_associations(streams) + @indexed_available_module_streams["#{name}-#{stream}-#{context}"]&.id + end + + def import_module_streams(module_streams) + # module_streams looks like this + # {"name"=>"389-ds", "stream"=>"1.4", "version"=>"8030020201203210520", "context"=>"e114a9e7", "arch"=>"x86_64", "profiles"=>[], "installed_profiles"=>[], "status"=>"default", "active"=>false} + streams = module_streams.map do |module_stream| + { + name: module_stream["name"], + stream: module_stream["stream"], + context: module_stream["context"] + } + end + if streams.any? + AvailableModuleStream.insert_all( + streams, + unique_by: %w[name stream context], + returning: %w[id name stream context] + ) + end + indexed_module_streams = module_streams.index_by do |module_stream| + available_module_stream_id_from( + name: module_stream["name"], + stream: module_stream["stream"], + context: module_stream["context"] + ) + end + sync_available_module_stream_associations(indexed_module_streams) end def sync_available_module_stream_associations(new_available_module_streams) - upgradable_streams = self.host_available_module_streams.where(:available_module_stream_id => new_available_module_streams.keys) + new_associated_ids = new_available_module_streams.keys.compact + upgradable_streams = self.host_available_module_streams.where(:available_module_stream_id => new_associated_ids) old_associated_ids = self.available_module_stream_ids - delete_ids = old_associated_ids - new_available_module_streams.keys + delete_ids = old_associated_ids - new_associated_ids if delete_ids.any? self.host_available_module_streams.where(:available_module_stream_id => delete_ids).delete_all end - new_ids = new_available_module_streams.keys - old_associated_ids - new_ids.each do |new_id| + new_ids = new_associated_ids - old_associated_ids + + hams_to_create = new_ids.map do |new_id| module_stream = new_available_module_streams[new_id] status = module_stream["status"] # Set status to "unknown" only if the active field is in use and set to false and the module is enabled if enabled_module_stream_inactive?(module_stream) status = "unknown" end - self.host_available_module_streams.create!(host_id: self.id, - available_module_stream_id: new_id, - installed_profiles: module_stream["installed_profiles"], - status: status) - end - + { + host_id: self.id, + available_module_stream_id: new_id, + installed_profiles: module_stream["installed_profiles"], + status: status + } + end + HostAvailableModuleStream.insert_all(hams_to_create) if hams_to_create.any? upgradable_streams.each do |hams| module_stream = new_available_module_streams[hams.available_module_stream_id] shared_keys = hams.attributes.keys & module_stream.keys diff --git a/test/models/concerns/host_managed_extensions_test.rb b/test/models/concerns/host_managed_extensions_test.rb index b6b05af85f8..ac95bc1f40e 100644 --- a/test/models/concerns/host_managed_extensions_test.rb +++ b/test/models/concerns/host_managed_extensions_test.rb @@ -367,6 +367,10 @@ def test_import_modules_with_active_field assert_equal 2, @foreman_host.host_available_module_streams.unknown.count end + def clear_cached_available_module_streams + @foreman_host.instance_variable_set(:@indexed_available_module_streams, nil) + end + def test_import_modules_with_update modules_json = [make_module_json("enabled21111", "enabled")] prior_count = HostAvailableModuleStream.count @@ -384,6 +388,7 @@ def test_import_modules_with_update assert_empty @foreman_host.reload.host_available_module_streams assert_equal prior_count, HostAvailableModuleStream.count + clear_cached_available_module_streams @foreman_host.import_module_streams([make_module_json("xxxx", "enabled", 'blah', ["default"])]) assert_equal "enabled", @foreman_host.reload.host_available_module_streams.first.status assert_equal ["default"], @foreman_host.reload.host_available_module_streams.first.installed_profiles