-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from fdupont-redhat/v2v_add_conversion_host_i…
…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
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
db/migrate/20181001131632_add_conversion_host_id_to_miq_request_tasks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
58 changes: 58 additions & 0 deletions
58
spec/migrations/20181001131632_add_conversion_host_id_to_miq_request_tasks_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |