-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create migrations to add resource_type row
Adds new row Migrates existing data to the new row
- Loading branch information
Julian Cheal
committed
Nov 21, 2018
1 parent
41e477e
commit 2b9aca7
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
db/migrate/20181108194142_add_resource_type_to_miq_policy_and_condition.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
24 changes: 24 additions & 0 deletions
24
db/migrate/20181108195546_migrate_miq_policy_towhat_to_resource_type.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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.all.each do |policy| | ||
policy.resource_type = policy.towhat | ||
policy.save | ||
end | ||
end | ||
end | ||
|
||
def down | ||
say_with_time("Moving MiqPolicy resource type to towhat") do | ||
MiqPolicy.all.each do |policy| | ||
policy.towhat = policy.resource_type | ||
policy.save | ||
end | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
db/migrate/20181108195945_migrate_conditions_tow_hat_to_resource_type.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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.all.each do |condition| | ||
condition.resource_type = condition.towhat | ||
condition.save | ||
end | ||
end | ||
end | ||
|
||
def down | ||
say_with_time("Moving Condition resource type to towhat") do | ||
Condition.all.each do |condition| | ||
condition.towhat = condition.resource_type | ||
condition.save | ||
end | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
spec/migrations/20181108195546_migrate_miq_policy_towhat_to_resource_type_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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).to eq(:resource_type => "ContainerImage") | ||
end | ||
|
||
it "ignores already converted data" do | ||
miq_policy = miq_policy_stub.create!(:towhat => "ContainerImage") | ||
|
||
migrate | ||
|
||
expect(miq_policy.reload).to eq(:resource_type => "ContainerImage") | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
spec/migrations/20181108195945_migrate_conditions_tow_hat_to_resource_type_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require_migration | ||
|
||
describe MigrateMiqPolicyTowHatToResourceType 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).to eq(:resource_type => "ContainerImage") | ||
end | ||
|
||
it "ignores already converted data" do | ||
condition = condition_stub.create!(:towhat => "ContainerImage") | ||
|
||
migrate | ||
|
||
expect(condition.reload).to eq(:resource_type => "ContainerImage") | ||
end | ||
end | ||
end |