Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a polymorphic resource to MiqSchedules #212

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions db/migrate/20180605135438_add_resource_to_miq_schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class AddResourceToMiqSchedule < ActiveRecord::Migration[5.0]
class MiqSchedule < ActiveRecord::Base
self.inheritance_column = :_type_disabled

include ReservedMigrationMixin
end

def up
add_column :miq_schedules, :resource_id, :bigint

say_with_time("Migrate data from reserved table to MiqSchedule") do
MiqSchedule.includes(:reserved_rec).each do |schedule|
schedule.reserved_hash_migrate(:resource_id)
end
end
end

def down
say_with_time("Migrate data from MiqSchedule to reserved table") do
MiqSchedule.includes(:reserved_rec).each do |schedule|
schedule.reserved_hash_set(:resource_id, schedule.resource_id)
schedule.save!
end
end

remove_column :miq_schedules, :resource_id
end
end
5 changes: 5 additions & 0 deletions db/migrate/20180606201908_rename_towhat_to_resource_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameTowhatToResourceType < ActiveRecord::Migration[5.0]
def change
rename_column :miq_schedules, :towhat, :resource_type
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_migration

describe AddResourceToMiqSchedule do
let(:reserve_stub) { Spec::Support::MigrationStubs.reserved_stub }
let(:miq_schedule_stub) { migration_stub(:MiqSchedule) }
let(:resource_id) { anonymous_class_with_id_regions.id_in_region(1, anonymous_class_with_id_regions.my_region_number) }

migration_context :up do
it "Migrates Reserve data to MiqSchedule" do
schedule = miq_schedule_stub.create!
reserve_stub.create!(
:resource_type => schedule.class.name,
:resource_id => schedule.id,
:reserved => {
:resource_id => resource_id,
}
)

migrate

schedule.reload

expect(reserve_stub.count).to eq(0)
expect(schedule.resource_id).to eq(resource_id)
end
end

migration_context :down do
it "Migrates resource_id in MiqSchedule to Reserve table" do
schedule = miq_schedule_stub.create!(:resource_id => resource_id)

migrate

expect(reserve_stub.count).to eq(1)

reserved_rec = reserve_stub.first
expect(reserved_rec.resource_id).to eq(schedule.id)
expect(reserved_rec.resource_type).to eq(schedule.class.name)

expect(reserved_rec.reserved[:resource_id]).to eq(resource_id)
end
end
end