Sha256: 7b87ae6f29d4e7575b16e1c5b53e0e17aec473b7f55d52852a2457f69d8c2aa7

Contents?: true

Size: 909 Bytes

Versions: 4

Compression:

Stored size: 909 Bytes

Contents

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

  def down
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
foreman_remote_execution-14.1.2 db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb
foreman_remote_execution-14.1.1 db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb
foreman_remote_execution-14.1.0 db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb
foreman_remote_execution-14.0.2 db/migrate/20240522093413_migrate_smart_proxy_ids_to_template_invocations.rb