Skip to content

Commit

Permalink
Merge pull request #281 from fdupont-redhat/v2v_add_conversion_host_i…
Browse files Browse the repository at this point in the history
…d_to_transformation_task

Add conversion_host_id to miq_request_task

(cherry picked from commit eb7e077)

https://bugzilla.redhat.com/show_bug.cgi?id=1634029
  • Loading branch information
carbonin authored and simaishi committed Oct 3, 2018
1 parent 4b5752c commit 58bd0c0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class AddConversionHostIdToMiqRequestTasks < ActiveRecord::Migration[5.0]
class MiqRequestTask < ActiveRecord::Base
self.inheritance_column = :_type_disabled
serialize :options, Hash
belongs_to :conversion_host, :class_name => "AddConversionHostIdToMiqRequestTasks::ConversionHost"
end

class ConversionHost < ActiveRecord::Base
self.inheritance_column = :_type_disabled
belongs_to :resource, :polymorphic => true
end

class Host < ActiveRecord::Base
self.inheritance_column = :_type_disabled
end

def up
add_column :miq_request_tasks, :conversion_host_id, :bigint

MiqRequestTask.where(:type => 'ServiceTemplateTransformationPlanTask').each do |task|
host_id = task.options.delete(:transformation_host_id)
next unless host_id
host = Host.find_by(:id => host_id)
if host.present?
task.conversion_host = ConversionHost.find_or_create_by!(:resource => host) do |ch|
ch.name = host.name
ch.vddk_transport_supported = true
ch.ssh_transport_supported = false
end
end
task.save!
end
end

def down
conversion_host_ids = MiqRequestTask.where(:type => 'ServiceTemplateTransformationPlanTask').map do |task|
next if task.conversion_host.nil?
task.options[:transformation_host_id] = task.conversion_host.resource.id
task.save!
task.conversion_host.id
end.uniq.compact
ConversionHost.destroy(conversion_host_ids)

remove_column :miq_request_tasks, :conversion_host_id
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require_migration

describe AddConversionHostIdToMiqRequestTasks do
let(:task_stub) { migration_stub(:MiqRequestTask) }
let(:host_stub) { migration_stub(:Host) }
let(:conversion_host_stub) { migration_stub(:ConversionHost) }

migration_context :up do
it "doesn't set the conversion host when the host doesn't exists" do
host = host_stub.create!
conversion_host_id = host.id
task = task_stub.create!(
:type => 'ServiceTemplateTransformationPlanTask',
:options => { :dummy_key => 'dummy_value', :transformation_host_id => conversion_host_id }
)
host.destroy!

migrate
task.reload

expect(task.options).to eq(:dummy_key => 'dummy_value')
expect(conversion_host_stub.find_by(:resource_id => conversion_host_id)).to be_nil
expect(task.conversion_host).to be_nil
end

it "creates a conversion host and updates the task when the host exists" do
host = host_stub.create!
task = task_stub.create!(
:type => 'ServiceTemplateTransformationPlanTask',
:options => { :dummy_key => 'dummy_value', :transformation_host_id => host.id }
)

migrate
task.reload

expect(task.options).to eq(:dummy_key => 'dummy_value')
expect(conversion_host_stub.find_by(:resource => host)).not_to be_nil
expect(task.conversion_host).to eq(conversion_host_stub.find_by(:resource => host))
end
end

migration_context :down do
it "updates task.options" do
host = host_stub.create!
conversion_host = conversion_host_stub.create!(:resource => host)
task = task_stub.create!(
:type => 'ServiceTemplateTransformationPlanTask',
:conversion_host => conversion_host
)

migrate

task.reload
expect(task.options[:transformation_host_id]).to eq(host.id)
expect { conversion_host.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end

0 comments on commit 58bd0c0

Please sign in to comment.