Skip to content

Commit

Permalink
MiqReport.seed skips a report when a custom report exists with same name
Browse files Browse the repository at this point in the history
Prefer the custom report over the seeded report and log a warning when
this case occurs.
  • Loading branch information
Fryguy committed Jan 22, 2019
1 parent 526045d commit f05016c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
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)
if duplicate&.rpt_type == "Custom"
_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

0 comments on commit f05016c

Please sign in to comment.