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

MiqReport.seed skips a report when a custom report exists with same name #18377

Merged
merged 1 commit into from
Jan 22, 2019
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
17 changes: 14 additions & 3 deletions app/models/miq_report/seeding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,29 @@ def seed_record(path, report)

_log.info("#{report.new_record? ? "Creating" : "Updating"} MiqReport #{filename.inspect}")

yml = YAML.load_file(path).symbolize_keys
yml = YAML.load_file(path).symbolize_keys
name = yml[:menu_name].strip

attrs = yml.slice(*column_names_symbols)
attrs.delete(:id)
attrs[:filename] = filename
attrs[:file_mtime] = mtime
attrs[:name] = yml[:menu_name].strip
attrs[:name] = name
attrs[:priority] = File.basename(path).split("_").first.to_i
attrs[:rpt_group] = File.basename(File.dirname(path)).split("_").last
attrs[:rpt_type] = "Default"
attrs[:template_type] = path.start_with?(REPORT_DIR.to_s) ? "report" : "compare"

report.update_attributes!(attrs)
begin
report.update_attributes!(attrs)
rescue ActiveRecord::RecordInvalid
duplicate = find_by(:name => name)
Copy link
Member

Choose a reason for hiding this comment

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

Thankfully this should be only happening on a rare occasion, so shouldn't be a big deal as far as adding to the N+1 count. 👍

That said, if we wanted, we could maybe raise these all at the end, and do a single find query. Probably not worth it, just a thought.

if duplicate&.rpt_type == "Custom"
Copy link
Member

Choose a reason for hiding this comment

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

😩 Nooooo... I hate this syntax...

(pedantic/personal preference)

Copy link
Member Author

Choose a reason for hiding this comment

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

Out of curiosity what is the syntax you'd prefer (FWIW I like this syntax :) )

Copy link
Member

@NickLaMuro NickLaMuro Jan 21, 2019

Choose a reason for hiding this comment

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

@Fryguy while it is "Rails specific", and I usually would reach for something out of STDLIB, .try would be preferred, or just explicitly doing it via a not duplicate.nil? and duplicate.rpt_type == "Custom".

The & symbol is already overloaded in my opinion, and is used for AND (&&), and bit-wise AND (&) already, so I just think this is a obscure syntax that doesn't help with readability in any form. Would have preferred a more verbose method like attempt() even over a single character operator. </rant> </2cents>

(Again: My personal preference, and it goes against rubocop's rule set, so not worth the change).

Copy link
Member

Choose a reason for hiding this comment

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

You would think with a name like lonely operator, it would attract more friends.

I don't like how it looks like invalid syntax or how it tries to make ugliness less ugly but besides that, I can dig it.

_log.warn("A custom report already exists with the name #{duplicate.name.inspect}. Skipping...")
else
raise
end
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/models/miq_report/seeding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@

expect { report.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { compare.reload }.to raise_error(ActiveRecord::RecordNotFound)

# Duplicate custom reports by name will be skipped in seeding
custom_report = FactoryBot.create(:miq_report, :name => "Testing Report Name", :rpt_type => "Custom", :template_type => "report")
custom_compare = FactoryBot.create(:miq_report, :name => "Testing Compare Name", :rpt_type => "Custom", :template_type => "compare")

described_class.seed

expect(described_class.where(:name => custom_report.name).count).to eq(1)
expect(custom_report.reload.rpt_type).to eq("Custom")
expect(described_class.where(:name => custom_compare.name).count).to eq(1)
expect(custom_compare.reload.rpt_type).to eq("Custom")
end
end
end
Expand Down