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

Miq shortcut seeding #14915

Merged
merged 2 commits into from
May 3, 2017
Merged
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
19 changes: 13 additions & 6 deletions app/models/miq_shortcut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ class MiqShortcut < ApplicationRecord
has_many :miq_widgets, :through => :miq_widget_shortcuts

def self.seed
names = []
seed_data.each_with_index do |s, index|
names << s[:name]
db_data = all.index_by(&:name)
seed_records = seed_data

seed_records_by_name = seed_records.group_by { |x| x[:name] }
if seed_records.size != seed_records_by_name.size
names = seed_records_by_name.select { |_n, v| v.size > 1 }.map(&:first)
_log.warn("Duplicate seeds for names: #{names.join(",")}")
end

seed_records.each_with_index do |s, index|
s[:sequence] = index
rec = find_by(:name => s[:name])
rec = db_data[s[:name]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about db_data.delete(s[:name]) instead?

Then we can remove next if from the last loop (making it quicker/no-op in most cases).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed on pm with @kbrock. Not a blocker for this pr.

if rec.nil?
_log.info("Creating #{s.inspect}")
rec = create!(s)
Expand All @@ -20,8 +27,8 @@ def self.seed
end
end

all.each do |rec|
next if names.include?(rec.name)
db_data.each do |name, rec|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this block is unnecessary as you are now doing the delete above? Or am I reading this wrong?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only delete the records from the local list.
The records left in db_data are the records that did not have a corresponding seed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see what you are doing now, thanks for the clarification. Looks good. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that delete was causing duplicate records in the database. Took an approach more similar to what was there before.

next if seed_records_by_name[name]
_log.info("Deleting #{rec.inspect}")
rec.destroy
end
Expand Down