From 4efb2382bd9555c150741b71d407b5527df5f8e4 Mon Sep 17 00:00:00 2001 From: Adam Ruzicka Date: Thu, 17 Oct 2024 09:53:22 +0200 Subject: [PATCH] Fixes #37922 - Handle case where proxy no longer exists Fixes e3578d6 --- ...22093413_migrate_smart_proxy_ids_to_template_invocations.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb b/db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb index 955263203..0cda08c4d 100644 --- a/db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb +++ b/db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb @@ -1,10 +1,13 @@ class MigrateSmartProxyIdsToTemplateInvocations < ActiveRecord::Migration[6.0] def up + proxy_lookup_cache = {} ForemanTasks::Link.joins(:task) .where(resource_type: 'SmartProxy', task: { label: 'Actions::RemoteExecution::RunHostJob' }) .where.not(resource_id: nil) .find_in_batches do |batch| batch.group_by(&:resource_id).each do |resource_id, links| + proxy_lookup_cache[resource_id] = SmartProxy.where(id: resource_id).exists? unless proxy_lookup_cache.key?(resource_id) + next unless proxy_lookup_cache[resource_id] template_invocation_ids = ForemanTasks::Link.where(resource_type: 'TemplateInvocation', task_id: links.map(&:task_id)).select(:resource_id) TemplateInvocation.where(id: template_invocation_ids).update_all(smart_proxy_id: resource_id) end