From f05016c8983a892ca0cbc284322e760da29b457d Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Mon, 21 Jan 2019 11:13:16 -0500 Subject: [PATCH] MiqReport.seed skips a report when a custom report exists with same name Prefer the custom report over the seeded report and log a warning when this case occurs. --- app/models/miq_report/seeding.rb | 17 ++++++++++++++--- spec/models/miq_report/seeding_spec.rb | 11 +++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/models/miq_report/seeding.rb b/app/models/miq_report/seeding.rb index beafca4b926..1835a36c62a 100644 --- a/app/models/miq_report/seeding.rb +++ b/app/models/miq_report/seeding.rb @@ -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 diff --git a/spec/models/miq_report/seeding_spec.rb b/spec/models/miq_report/seeding_spec.rb index 059e52fd293..49a9db6a427 100644 --- a/spec/models/miq_report/seeding_spec.rb +++ b/spec/models/miq_report/seeding_spec.rb @@ -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