Sha256: 064a9497495efde6c548dba7112d88b3083e80e62ec80bf097fc263a2722e7d9

Contents?: true

Size: 1.29 KB

Versions: 79

Compression:

Stored size: 1.29 KB

Contents

class AddHostIdToTemplateInvocation < ActiveRecord::Migration[4.2]
  class FakeTemplateInvocation < ApplicationRecord
    self.table_name = 'template_invocations'
  end

  def up
    add_column :template_invocations, :host_id, :integer
    add_foreign_key 'template_invocations', 'hosts', :name => 'template_invocations_hosts_id_fk', :column => 'host_id'
    FakeTemplateInvocation.reset_column_information

    say 'Migrating existing execution locks to explicit relations, this may take a while'
    FakeTemplateInvocation.all.each do |template_invocation|
      task = ForemanTasks::Task.for_action_types('Actions::RemoteExecution::RunHostJob').joins(:locks).where(
        :'foreman_tasks_locks.resource_type' => 'TemplateInvocation',
        :'foreman_tasks_locks.resource_id' => template_invocation.id
      ).first
      next if task.nil? # skip invocations from very early versions of remote executions

      host_id = task.locks.where(:'foreman_tasks_locks.resource_type' => 'Host::Managed').first.resource_id
      next unless Host.find_by(id: host_id)

      template_invocation.host_id = host_id
      template_invocation.save!
    end
  end

  def down
    remove_foreign_key 'template_invocations', :name => 'template_invocations_hosts_id_fk'
    remove_column :template_invocations, :host_id
  end
end

Version data entries

79 entries across 79 versions & 1 rubygems

Version Path
foreman_remote_execution-10.1.3 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-11.1.3 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.1.2 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.1.1 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-11.1.1 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-11.1.0 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.1.0 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-8.3.3 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-8.3.2 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-11.0.0 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.7 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-8.3.1 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.6 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.5 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.4 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.3 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.2 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.1 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-10.0.0 db/migrate/20151215114631_add_host_id_to_template_invocation.rb
foreman_remote_execution-8.3.0 db/migrate/20151215114631_add_host_id_to_template_invocation.rb