-
Notifications
You must be signed in to change notification settings - Fork 898
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
Miq shortcut seeding #14915
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]] | ||
if rec.nil? | ||
_log.info("Creating #{s.inspect}") | ||
rec = create!(s) | ||
|
@@ -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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only delete the records from the local list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, gotcha. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, that |
||
next if seed_records_by_name[name] | ||
_log.info("Deleting #{rec.inspect}") | ||
rec.destroy | ||
end | ||
|
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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.