diff --git a/db/migrate/20200331150436_rename_foreman_features.rb b/db/migrate/20200331150436_rename_foreman_features.rb new file mode 100644 index 000000000..0334b3c91 --- /dev/null +++ b/db/migrate/20200331150436_rename_foreman_features.rb @@ -0,0 +1,35 @@ +class RenameForemanFeatures < ActiveRecord::Migration[5.1] + class MiqProductFeature < ActiveRecord::Base; end + class MiqRolesFeature < ActiveRecord::Base; end + + def up + return if MiqProductFeature.none? + + MiqProductFeature.find_by(:identifier => 'provider_foreman_explorer')&.update(:identifier => 'ems_configuration') + + MiqProductFeature.find_by(:identifier => 'provider_foreman_view')&.update(:identifier => 'ems_configuration_view') + + provider_feature = MiqProductFeature.find_or_create_by!(:identifier => 'ems_configuration') + + profiles_feature = MiqProductFeature.create!(:identifier => 'configuration_profile') + systems_feature = MiqProductFeature.create!(:identifier => 'configured_system') + + MiqRolesFeature.where(:miq_product_feature_id => provider_feature.id).each do |roles_feature| + MiqRolesFeature.create!(:miq_product_feature_id => profiles_feature.id, :miq_user_role_id => roles_feature.miq_user_role_id) + MiqRolesFeature.create!(:miq_product_feature_id => systems_feature.id, :miq_user_role_id => roles_feature.miq_user_role_id) + end + end + + def down + return if MiqProductFeature.none? + + MiqProductFeature.find_by(:identifier => 'ems_configuration')&.update(:identifier => 'provider_foreman_explorer') + + MiqProductFeature.find_by(:identifier => 'ems_configuration_view')&.update(:identifier => 'provider_foreman_view') + + profiles_feature = MiqProductFeature.find_by!(:identifier => 'configuration_profile') + systems_feature = MiqProductFeature.find_by!(:identifier => 'configured_system') + + MiqRolesFeature.where(:miq_product_feature_id => [profiles_feature.id, systems_feature.id]).delete_all + end +end diff --git a/spec/migrations/20200331150436_rename_foreman_features_spec.rb b/spec/migrations/20200331150436_rename_foreman_features_spec.rb new file mode 100644 index 000000000..80ad3abed --- /dev/null +++ b/spec/migrations/20200331150436_rename_foreman_features_spec.rb @@ -0,0 +1,58 @@ +require_migration + +describe RenameForemanFeatures do + class MiqRolesFeature < ActiveRecord::Base; end + + let(:user_role_id) { rand(1..1000) } + let(:feature_stub) { migration_stub :MiqProductFeature } + let(:roles_feature_stub) { migration_stub :MiqRolesFeature } + + migration_context :up do + let!(:explorer_feature) { feature_stub.create!(:identifier => 'provider_foreman_explorer', :name => 'foo') } + + it 'renames the features' do + view_feature = feature_stub.create!(:identifier => 'provider_foreman_view', :name => 'bar') + + migrate + + expect(explorer_feature.reload.identifier).to eq('ems_configuration') + expect(view_feature.reload.identifier).to eq('ems_configuration_view') + end + + it 'appends the extra features for roles' do + roles_feature_stub.create!(:miq_product_feature_id => explorer_feature.id, :miq_user_role_id => user_role_id) + + expect(MiqRolesFeature.where(:miq_user_role_id => user_role_id).count).to eq(1) + + migrate + + expect(MiqRolesFeature.where(:miq_user_role_id => user_role_id).count).to eq(3) + end + end + + migration_context :down do + let!(:explorer_feature) { feature_stub.create!(:identifier => 'ems_configuration', :name => 'foo') } + let!(:profile_feature) { feature_stub.create!(:identifier => 'configuration_profile', :name => 'baz') } + let!(:system_feature) { feature_stub.create!(:identifier => 'configured_system', :name => 'yay') } + let!(:view_feature) { feature_stub.create!(:identifier => 'ems_configuration_view', :name => 'bar') } + + it 'renames the features' do + migrate + + expect(explorer_feature.reload.identifier).to eq('provider_foreman_explorer') + expect(view_feature.reload.identifier).to eq('provider_foreman_view') + end + + it 'removes the extra features for roles' do + roles_feature_stub.create!(:miq_product_feature_id => explorer_feature.id, :miq_user_role_id => user_role_id) + roles_feature_stub.create!(:miq_product_feature_id => profile_feature.id, :miq_user_role_id => user_role_id) + roles_feature_stub.create!(:miq_product_feature_id => system_feature.id, :miq_user_role_id => user_role_id) + + expect(MiqRolesFeature.where(:miq_user_role_id => user_role_id).count).to eq(3) + + migrate + + expect(MiqRolesFeature.where(:miq_user_role_id => user_role_id).count).to eq(1) + end + end +end