Skip to content

Commit

Permalink
Create migrations to add resource_type row
Browse files Browse the repository at this point in the history
Adds new row
Migrates existing data to the new row
  • Loading branch information
Julian Cheal committed Nov 21, 2018
1 parent 41e477e commit ae1f208
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class MigrateMiqPolicyTowhatToResourceType < ActiveRecord::Migration[5.0]
class MiqPolicy < ActiveRecord::Base
end

def up
say_with_time("Moving MiqPolicy towhat to resource type") do
MiqPolicy.find_each do |policy|
policy.update(:resource_type => policy.towhat)
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)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class MigrateConditionsTowHatToResourceType < ActiveRecord::Migration[5.0]
class Condition < ActiveRecord::Base
end

def up
say_with_time("Moving Condition towhat to resource type") do
Condition.find_each do |condition|
condition.update(:resource_type => condition.towhat)
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)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ae1f208

Please sign in to comment.