diff --git a/db/migrate/20220428165655_fix_container_image_sti_class.rb b/db/migrate/20220428165655_fix_container_image_sti_class.rb new file mode 100644 index 000000000..23e3e2619 --- /dev/null +++ b/db/migrate/20220428165655_fix_container_image_sti_class.rb @@ -0,0 +1,45 @@ +class FixContainerImageStiClass < ActiveRecord::Migration[6.0] + class ExtManagementSystem < ActiveRecord::Base + include ActiveRecord::IdRegions + self.inheritance_column = :_type_disabled + end + + class ContainerImage < ActiveRecord::Base + include ActiveRecord::IdRegions + self.inheritance_column = :_type_disabled + + belongs_to :ext_management_system, :foreign_key => :ems_id, :class_name => "FixContainerImageStiClass::ExtManagementSystem" + end + + def up + say_with_time("Fixing ContainerImage STI class names") do + # First update all "Openshift" container images to be ManagedContainerImage types + ContainerImage.where(:type => "ManageIQ::Providers::Openshift::ContainerManager::ContainerImage") + .update_all(:type => "ManageIQ::Providers::Openshift::ContainerManager::ManagedContainerImage") + + # Second update all ::ContainerImage to be subclassed under their provider namespaces + %w[Amazon Azure Kubernetes Openshift OracleCloud Vmware].each do |provider| + provider_klass = "ManageIQ::Providers::#{provider}::ContainerManager" + + container_managers = ExtManagementSystem.in_my_region.where(:type => provider_klass) + ContainerImage.in_my_region + .where(:ext_management_system => container_managers, :type => [nil, "ContainerImage"]) + .update_all(:type => "#{provider_klass}::ContainerImage") + end + end + end + + def down + %w[Amazon Azure Kubernetes Openshift OracleCloud Vmware].each do |provider| + provider_klass = "ManageIQ::Providers::#{provider}::ContainerManager" + + container_managers = ExtManagementSystem.in_my_region.where(:type => provider_klass) + ContainerImage.in_my_region + .where(:ext_management_system => container_managers, :type => "#{provider_klass}::ContainerImage") + .update_all(:type => nil) + end + + ContainerImage.where(:type => "ManageIQ::Providers::Openshift::ContainerManager::ManagedContainerImage") + .update_all(:type => "ManageIQ::Providers::Openshift::ContainerManager::ContainerImage") + end +end diff --git a/spec/migrations/20220428165655_fix_container_image_sti_class_spec.rb b/spec/migrations/20220428165655_fix_container_image_sti_class_spec.rb new file mode 100644 index 000000000..abcd8fcf5 --- /dev/null +++ b/spec/migrations/20220428165655_fix_container_image_sti_class_spec.rb @@ -0,0 +1,56 @@ +require_migration + +# This is mostly necessary for data migrations, so feel free to delete this +# file if you do no need it. +describe FixContainerImageStiClass do + let(:ext_management_system_stub) { migration_stub(:ExtManagementSystem) } + let(:container_image_stub) { migration_stub(:ContainerImage) } + + migration_context :up do + it "updates ContainerImage STI classes" do + openshift_manager = ext_management_system_stub.create!(:type => "ManageIQ::Providers::Openshift::ContainerManager") + openshift_managed_image = container_image_stub.create!(:type => "ManageIQ::Providers::Openshift::ContainerManager::ContainerImage", :ext_management_system => openshift_manager) + openshift_docker_image = container_image_stub.create!(:type => nil, :ext_management_system => openshift_manager) + + # Create data + # + # Example: + # + # my_model_stub.create!(attributes) + + migrate + + # Ensure data exists + # + # Example: + # + # expect(record).to have_attributes() + expect(openshift_managed_image.reload.type).to eq("ManageIQ::Providers::Openshift::ContainerManager::ManagedContainerImage") + expect(openshift_docker_image.reload.type).to eq("ManageIQ::Providers::Openshift::ContainerManager::ContainerImage") + end + end + + migration_context :down do + it "reverts ContainerImage STI classes" do + openshift_manager = ext_management_system_stub.create!(:type => "ManageIQ::Providers::Openshift::ContainerManager") + openshift_managed_image = container_image_stub.create!(:type => "ManageIQ::Providers::Openshift::ContainerManager::ManagedContainerImage", :ext_management_system => openshift_manager) + openshift_docker_image = container_image_stub.create!(:type => "ManageIQ::Providers::Openshift::ContainerManager::ContainerImage", :ext_management_system => openshift_manager) + + # Create data + # + # Example: + # + # my_model_stub.create!(attributes) + + migrate + + # Ensure data exists + # + # Example: + # + # expect(record).to have_attributes() + expect(openshift_managed_image.reload.type).to eq("ManageIQ::Providers::Openshift::ContainerManager::ContainerImage") + expect(openshift_docker_image.reload.type).to be_nil + end + end +end