diff --git a/db/migrate/20181001131632_add_conversion_host_id_to_miq_request_tasks.rb b/db/migrate/20181001131632_add_conversion_host_id_to_miq_request_tasks.rb new file mode 100644 index 000000000..2f3ee17ed --- /dev/null +++ b/db/migrate/20181001131632_add_conversion_host_id_to_miq_request_tasks.rb @@ -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 diff --git a/spec/migrations/20181001131632_add_conversion_host_id_to_miq_request_tasks_spec.rb b/spec/migrations/20181001131632_add_conversion_host_id_to_miq_request_tasks_spec.rb new file mode 100644 index 000000000..0f5432a49 --- /dev/null +++ b/spec/migrations/20181001131632_add_conversion_host_id_to_miq_request_tasks_spec.rb @@ -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