Skip to content

Commit

Permalink
Renamed foreman features to ems_comfiguration and adjusted roles
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Apr 7, 2020
1 parent 46add9e commit 211df78
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
35 changes: 35 additions & 0 deletions db/migrate/20200331150436_rename_foreman_features.rb
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions spec/migrations/20200331150436_rename_foreman_features_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 211df78

Please sign in to comment.