-
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.
Add conversion host relationship to task
- Loading branch information
1 parent
46cc3f2
commit 659be65
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
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 ServiceTemplateTransformationPlanTask < MiqRequestTask | ||
end | ||
|
||
class ConversionHost < ActiveRecord::Base | ||
self.inheritance_column = :_type_disabled | ||
has_many :service_template_transformation_plan_tasks, :class_name => "AddConversionHostIdToMiqRequestTasks::MiqRequestTask" | ||
end | ||
|
||
class Host < ActiveRecord::Base | ||
self.inheritance_column = :_type_disabled | ||
has_many :tags, :class_name => "AddConversionHostIdToMiqRequestTasks::Tag" | ||
end | ||
|
||
def up | ||
add_column :miq_request_tasks, :conversion_host_id, :bigint | ||
|
||
ServiceTemplateTransformationPlanTask.all.reject { |task| task.options[:transformation_host_id].nil? }.each do |task| | ||
host = Host.find_by(:id => task.options[:transformation_host_id]) | ||
task.conversion_host = ConversionHost.where(:id => task.options[:transformation_host_id]).first_or_create do |ch| | ||
ch.name = host.name | ||
ch.resource_type = host.type | ||
ch.resource_id = host.id | ||
ch.address = host.ipaddress | ||
ch.vddk_transport_supported = true | ||
ch.ssh_transport_supported = false | ||
end | ||
task.options.delete(:transformation_host_id) | ||
task.save! | ||
end | ||
end | ||
|
||
def down | ||
ServiceTemplateTransformationPlanTask.all.select { |task| task.conversion_host.present? }.each do |task| | ||
task.options[:transformation_host_id] = task.conversion_host.id | ||
ConversionHost.find(task.conversion_host.id).destroy! | ||
task.save! | ||
end | ||
|
||
remove_column :miq_request_tasks, :conversion_host_id | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
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 "creates conversion host" do | ||
task = task_stub.create! | ||
host = host_stub.create! | ||
conversion_host = conversion_host_stub.create!( | ||
:resource_id => host.id, | ||
:resource_type => host.type | ||
) | ||
task.options = { :transformation_host_id => conversion_host.id } | ||
task.save | ||
|
||
migrate | ||
|
||
task.reload | ||
expect(task.options).to eq({}) | ||
expect(task.conversion_host).to eq(conversion_host) | ||
end | ||
end | ||
|
||
migration_context :down do | ||
it "updates task.options" do | ||
task = task_stub.create! | ||
host = host_stub.create! | ||
conversion_host = conversion_host_stub.create!( | ||
:resource_id => host.id, | ||
:resource_type => host.type | ||
) | ||
conversion_host.save | ||
task.conversion_host = conversion_host | ||
task.save | ||
|
||
migrate | ||
|
||
task.reload | ||
expect(task.attributes).not_to include('conversion_host_id') | ||
expect(task.options[:transformation_host_id]).to eq(conversion_host.id) | ||
expect { conversion_host.reload }.to raise_error(ActiveRecord::RecordNotFound) | ||
end | ||
end | ||
end |