Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix ContainerImage STI classes #646

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions db/migrate/20220428165655_fix_container_image_sti_class.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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