From e14287084bbe3668f64280df507ebb72196d2262 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Fri, 30 Aug 2024 13:33:04 -0400 Subject: [PATCH] Reset resource_pool_infra product features --- ...0172702_reset_resource_pool_identifiers.rb | 35 ++++++++++++++++ ...02_reset_resource_pool_identifiers_spec.rb | 41 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 db/migrate/20240830172702_reset_resource_pool_identifiers.rb create mode 100644 spec/migrations/20240830172702_reset_resource_pool_identifiers_spec.rb diff --git a/db/migrate/20240830172702_reset_resource_pool_identifiers.rb b/db/migrate/20240830172702_reset_resource_pool_identifiers.rb new file mode 100644 index 00000000..404c739b --- /dev/null +++ b/db/migrate/20240830172702_reset_resource_pool_identifiers.rb @@ -0,0 +1,35 @@ +class ResetResourcePoolIdentifiers < ActiveRecord::Migration[6.1] + class MiqProductFeature < ActiveRecord::Base; end + + FEATURE_MAPPING_UPDATE = { + 'resource_pool_infra' => 'resource_pool', + 'resource_pool_infra_view' => 'resource_pool_view', + 'resource_pool_infra_show_list' => 'resource_pool_show_list', + 'resource_pool_infra_show' => 'resource_pool_show', + 'resource_pool_infra_control' => 'resource_pool_control', + 'resource_pool_infra_tag' => 'resource_pool_tag', + 'resource_pool_infra_protect' => 'resource_pool_protect', + 'resource_pool_infra_admin' => 'resource_pool_admin', + 'resource_pool_infra_delete' => 'resource_pool_delete' + }.freeze + + def up + return if MiqProductFeature.none? + + say_with_time('Resetting resource_pool_infra features back to resource_pool') do + FEATURE_MAPPING_UPDATE.each do |from, to| + MiqProductFeature.find_by(:identifier => from)&.update!(:identifier => to) + end + end + end + + def down + return if MiqProductFeature.none? + + say_with_time('Updating resource_pool features to resource_pool_infra') do + FEATURE_MAPPING_UPDATE.each do |to, from| + MiqProductFeature.find_by(:identifier => from)&.update!(:identifier => to) + end + end + end +end diff --git a/spec/migrations/20240830172702_reset_resource_pool_identifiers_spec.rb b/spec/migrations/20240830172702_reset_resource_pool_identifiers_spec.rb new file mode 100644 index 00000000..c4008872 --- /dev/null +++ b/spec/migrations/20240830172702_reset_resource_pool_identifiers_spec.rb @@ -0,0 +1,41 @@ +require_migration + +# This is mostly necessary for data migrations, so feel free to delete this +# file if you do no need it. +describe ResetResourcePoolIdentifiers do + let(:miq_product_feature) { migration_stub(:MiqProductFeature) } + + before do + described_class::FEATURE_MAPPING_UPDATE.each_key do |old_identifier| + miq_product_feature.create!(:identifier => old_identifier) + end + end + + migration_context :up do + it "updates existing resource_pool features to resource_pool_infra" do + migrate + + described_class::FEATURE_MAPPING_UPDATE.each do |old_identifier, new_identifier| + expect(miq_product_feature.exists?(:identifier => old_identifier)).to be_falsy + expect(miq_product_feature.exists?(:identifier => new_identifier)).to be_truthy + end + end + end + + migration_context :down do + before do + described_class::FEATURE_MAPPING_UPDATE.each_value do |new_identifier| + miq_product_feature.create!(:identifier => new_identifier) + end + end + + it "reverts resource_pool_infra features back to resource_pool" do + migrate + + described_class::FEATURE_MAPPING_UPDATE.each do |old_identifier, new_identifier| + expect(miq_product_feature.exists?(:identifier => new_identifier)).to be_falsy + expect(miq_product_feature.exists?(:identifier => old_identifier)).to be_truthy + end + end + end +end