diff --git a/db/migrate/20181108194142_add_resource_type_to_miq_policy_and_condition.rb b/db/migrate/20181108194142_add_resource_type_to_miq_policy_and_condition.rb new file mode 100644 index 000000000..20f43ee00 --- /dev/null +++ b/db/migrate/20181108194142_add_resource_type_to_miq_policy_and_condition.rb @@ -0,0 +1,6 @@ +class AddResourceTypeToMiqPolicyAndCondition < ActiveRecord::Migration[5.0] + def change + add_column :miq_policies, :resource_type, :string + add_column :conditions, :resource_type, :string + end +end diff --git a/db/migrate/20181108195546_migrate_miq_policy_towhat_to_resource_type.rb b/db/migrate/20181108195546_migrate_miq_policy_towhat_to_resource_type.rb new file mode 100644 index 000000000..8eae4c2dc --- /dev/null +++ b/db/migrate/20181108195546_migrate_miq_policy_towhat_to_resource_type.rb @@ -0,0 +1,23 @@ +class MigrateMiqPolicyTowhatToResourceType < ActiveRecord::Migration[5.0] + class MiqPolicy < ActiveRecord::Base + self.inheritance_column = :_type_disabled + end + + def up + say_with_time("Moving MiqPolicy towhat to resource type") do + MiqPolicy.find_each do |policy| + policy.update(:resource_type => policy.towhat) + policy.save + end + end + end + + def down + say_with_time("Moving MiqPolicy resource type to towhat") do + MiqPolicy.find_each do |policy| + policy.update(:towhat => policy.resource_type) + policy.save + end + end + end +end diff --git a/db/migrate/20181108195945_migrate_conditions_tow_hat_to_resource_type.rb b/db/migrate/20181108195945_migrate_conditions_tow_hat_to_resource_type.rb new file mode 100644 index 000000000..14577b4fe --- /dev/null +++ b/db/migrate/20181108195945_migrate_conditions_tow_hat_to_resource_type.rb @@ -0,0 +1,23 @@ +class MigrateConditionsTowHatToResourceType < ActiveRecord::Migration[5.0] + class Condition < ActiveRecord::Base + self.inheritance_column = :_type_disabled + end + + def up + say_with_time("Moving Condition towhat to resource type") do + Condition.find_each do |condition| + condition.update(:resource_type => condition.towhat) + condition.save + end + end + end + + def down + say_with_time("Moving Condition resource type to towhat") do + Condition.find_each do |condition| + condition.update(:towhat => condition.resource_type) + condition.save + end + end + end +end diff --git a/spec/migrations/20181108195546_migrate_miq_policy_towhat_to_resource_type_spec.rb b/spec/migrations/20181108195546_migrate_miq_policy_towhat_to_resource_type_spec.rb new file mode 100644 index 000000000..8772ec5be --- /dev/null +++ b/spec/migrations/20181108195546_migrate_miq_policy_towhat_to_resource_type_spec.rb @@ -0,0 +1,23 @@ +require_migration + +describe MigrateMiqPolicyTowhatToResourceType do + let(:miq_policy_stub) { migration_stub(:MiqPolicy) } + + migration_context :up do + it "converts the old data" do + miq_policy = miq_policy_stub.create!(:towhat => "ContainerImage") + + migrate + + expect(miq_policy.reload.resource_type).to eq("ContainerImage") + end + + it "ignores already converted data" do + miq_policy = miq_policy_stub.create!(:towhat => "ContainerImage") + + migrate + + expect(miq_policy.reload.resource_type).to eq("ContainerImage") + end + end +end diff --git a/spec/migrations/20181108195945_migrate_conditions_tow_hat_to_resource_type_spec.rb b/spec/migrations/20181108195945_migrate_conditions_tow_hat_to_resource_type_spec.rb new file mode 100644 index 000000000..8f1137aff --- /dev/null +++ b/spec/migrations/20181108195945_migrate_conditions_tow_hat_to_resource_type_spec.rb @@ -0,0 +1,23 @@ +require_migration + +describe MigrateConditionsTowHatToResourceType do + let(:condition_stub) { migration_stub(:Condition) } + + migration_context :up do + it "converts the old data" do + condition = condition_stub.create!(:towhat => "ContainerImage") + + migrate + + expect(condition.reload.resource_type).to eq("ContainerImage") + end + + it "ignores already converted data" do + condition = condition_stub.create!(:towhat => "ContainerImage") + + migrate + + expect(condition.reload.resource_type).to eq("ContainerImage") + end + end +end