diff --git a/db/migrate/20240105173129_add_orchestration_stack_configuration_script.rb b/db/migrate/20240105173129_add_orchestration_stack_configuration_script.rb new file mode 100644 index 00000000..e44ac0dd --- /dev/null +++ b/db/migrate/20240105173129_add_orchestration_stack_configuration_script.rb @@ -0,0 +1,5 @@ +class AddOrchestrationStackConfigurationScript < ActiveRecord::Migration[6.1] + def change + add_reference :orchestration_stacks, :configuration_script, :type => :bigint, :index => true + end +end diff --git a/db/migrate/20240108211241_fix_orchestration_stack_configuration_script_reference.rb b/db/migrate/20240108211241_fix_orchestration_stack_configuration_script_reference.rb new file mode 100644 index 00000000..06b12992 --- /dev/null +++ b/db/migrate/20240108211241_fix_orchestration_stack_configuration_script_reference.rb @@ -0,0 +1,22 @@ +class FixOrchestrationStackConfigurationScriptReference < ActiveRecord::Migration[6.1] + class OrchestrationStack < ActiveRecord::Base + include ActiveRecord::IdRegions + self.inheritance_column = :_type_disabled + end + + TYPES = %w[ManageIQ::Providers::Awx::AutomationManager::Job ManageIQ::Providers::AnsibleTower::AutomationManager::Job].freeze + + def up + say_with_time("Fixing OrchestrationStack#configuration_script foreign key") do + OrchestrationStack.in_my_region.where(:type => TYPES).update_all('configuration_script_id = orchestration_template_id') + OrchestrationStack.in_my_region.where(:type => TYPES).update_all(:orchestration_template_id => nil) + end + end + + def down + say_with_time("Resetting OrchestrationStack#configuration_script foreign key") do + OrchestrationStack.in_my_region.where(:type => TYPES).update_all('orchestration_template_id = configuration_script_id') + OrchestrationStack.in_my_region.where(:type => TYPES).update_all(:configuration_script_id => nil) + end + end +end diff --git a/spec/migrations/20240108211241_fix_orchestration_stack_configuration_script_reference_spec.rb b/spec/migrations/20240108211241_fix_orchestration_stack_configuration_script_reference_spec.rb new file mode 100644 index 00000000..d5073a47 --- /dev/null +++ b/spec/migrations/20240108211241_fix_orchestration_stack_configuration_script_reference_spec.rb @@ -0,0 +1,84 @@ +require_migration + +class FixOrchestrationStackConfigurationScriptReference + class ConfigurationScriptBase < ActiveRecord::Base + include ActiveRecord::IdRegions + self.table_name = "configuration_scripts" + self.inheritance_column = :_type_disabled + end + + class OrchestrationTemplate < ActiveRecord::Base + include ActiveRecord::IdRegions + self.inheritance_column = :_type_disabled + end +end + +describe FixOrchestrationStackConfigurationScriptReference do + let(:configuration_script_base_stub) { migration_stub(:ConfigurationScriptBase) } + let(:orchestration_stack_stub) { migration_stub(:OrchestrationStack) } + let(:orchestration_template_stub) { migration_stub(:OrchestrationTemplate) } + + migration_context :up do + it "fixes AWX and AnsibleTower Jobs" do + job_template = configuration_script_base_stub.create!(:type => "ConfigurationScript") + + awx_job = orchestration_stack_stub.create!(:type => "ManageIQ::Providers::Awx::AutomationManager::Job", :orchestration_template_id => job_template.id) + tower_job = orchestration_stack_stub.create!(:type => "ManageIQ::Providers::AnsibleTower::AutomationManager::Job", :orchestration_template_id => job_template.id) + + migrate + + awx_job.reload + tower_job.reload + + expect(awx_job.configuration_script_id).to eq(job_template.id) + expect(awx_job.orchestration_template_id).to be_nil + + expect(tower_job.configuration_script_id).to eq(job_template.id) + expect(tower_job.orchestration_template_id).to be_nil + end + + it "doesn't impact other Orchestration Stacks" do + cloud_template = orchestration_template_stub.create! + cloud_stack = orchestration_stack_stub.create!(:type => "ManageIQ::Providers::CloudManager::OrchestrationStack", :orchestration_template_id => cloud_template.id) + + migrate + + cloud_stack.reload + + expect(cloud_stack.orchestration_template_id).to eq(cloud_template.id) + expect(cloud_stack.configuration_script_id).to be_nil + end + end + + migration_context :down do + it "resets AWX and AnsibleTower Jobs" do + job_template = configuration_script_base_stub.create!(:type => "ConfigurationScript") + + awx_job = orchestration_stack_stub.create!(:type => "ManageIQ::Providers::Awx::AutomationManager::Job", :configuration_script_id => job_template.id) + tower_job = orchestration_stack_stub.create!(:type => "ManageIQ::Providers::AnsibleTower::AutomationManager::Job", :configuration_script_id => job_template.id) + + migrate + + awx_job.reload + tower_job.reload + + expect(awx_job.configuration_script_id).to be_nil + expect(awx_job.orchestration_template_id).to eq(job_template.id) + + expect(tower_job.configuration_script_id).to be_nil + expect(tower_job.orchestration_template_id).to eq(job_template.id) + end + + it "doesn't impact other Orchestration Stacks" do + cloud_template = orchestration_template_stub.create! + cloud_stack = orchestration_stack_stub.create!(:type => "ManageIQ::Providers::CloudManager::OrchestrationStack", :orchestration_template_id => cloud_template.id) + + migrate + + cloud_stack.reload + + expect(cloud_stack.orchestration_template_id).to eq(cloud_template.id) + expect(cloud_stack.configuration_script_id).to be_nil + end + end +end