From 9178444d0a91a4598239dcd06d9a6bd85b740724 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Wed, 31 Jul 2019 11:48:37 -0400 Subject: [PATCH 1/5] fixed seeding failure when yaml file containing report was renamed Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1734076 --- app/models/miq_report/seeding.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/models/miq_report/seeding.rb b/app/models/miq_report/seeding.rb index cf35e2ded7b..c1ec1d6b935 100644 --- a/app/models/miq_report/seeding.rb +++ b/app/models/miq_report/seeding.rb @@ -7,14 +7,18 @@ module MiqReport::Seeding module ClassMethods def seed transaction do + # seeding from files, :filename attribute of existing record may be changed in this process + seed_files.each do |f| + seed_record(f, MiqReport.find_by(:filename => seed_filename(f))) + end + + # now remove Default reports which are not suplied as yaml anymore reports = where(:rpt_type => 'Default').where.not(:filename => nil).index_by do |f| seed_filename(f.filename) end - seed_files.each do |f| - seed_record(f, reports.delete(seed_filename(f))) + reports.delete(seed_filename(f)) end - if reports.any? _log.info("Deleting the following MiqReport(s) as they no longer exist: #{reports.keys.sort.collect(&:inspect).join(", ")}") @@ -67,6 +71,9 @@ def seed_record(path, report) duplicate = find_by(:name => name) if duplicate&.rpt_type == "Custom" _log.warn("A custom report already exists with the name #{duplicate.name.inspect}. Skipping...") + elsif duplicate + _log.warn("A standard report named '#{duplicate.name.inspect}' loaded from '#{duplicate.filename}' already exists. Updating attributes of existing report...") + duplicate.update!(attrs) else raise end From cb98a3727384d662a3545915345b1f1a8986a916 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Thu, 1 Aug 2019 10:09:45 -0400 Subject: [PATCH 2/5] added rspec when yaml file renamed --- spec/models/miq_report/seeding_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/models/miq_report/seeding_spec.rb b/spec/models/miq_report/seeding_spec.rb index 3d4dfc21928..2f7b1d23a16 100644 --- a/spec/models/miq_report/seeding_spec.rb +++ b/spec/models/miq_report/seeding_spec.rb @@ -108,6 +108,20 @@ expect(described_class.where(:name => custom_compare.name).count).to eq(1) expect(custom_compare.reload.rpt_type).to eq("Custom") end + + it "updates attributes of existing record if yaml renamed" do + old_yaml_file = "520_Events - Policy/110_Policy Events.yaml" + new_yaml_file = "520_Events - Policy/some_new_name.yaml" + described_class.seed + report = MiqReport.find_by(:name => "Policy Events for Last Week") + expect(report.filename).to eq(old_yaml_file) + + FileUtils.mv(reports_dir.join(old_yaml_file), reports_dir.join(new_yaml_file)) + described_class.seed + + report = MiqReport.find_by(:name => "Policy Events for Last Week") + expect(report.filename).to eq(new_yaml_file) + end end end end From 161336faf5bbcc2b696e40f6033d61ee1bbac875 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Thu, 1 Aug 2019 11:26:11 -0400 Subject: [PATCH 3/5] optimize performance --- app/models/miq_report/seeding.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/miq_report/seeding.rb b/app/models/miq_report/seeding.rb index c1ec1d6b935..33622b07b80 100644 --- a/app/models/miq_report/seeding.rb +++ b/app/models/miq_report/seeding.rb @@ -7,9 +7,12 @@ module MiqReport::Seeding module ClassMethods def seed transaction do + reports = where(:rpt_type => 'Default').where.not(:filename => nil).index_by do |f| + seed_filename(f.filename) + end # seeding from files, :filename attribute of existing record may be changed in this process seed_files.each do |f| - seed_record(f, MiqReport.find_by(:filename => seed_filename(f))) + seed_record(f, reports[seed_filename(f)]) end # now remove Default reports which are not suplied as yaml anymore From 6b269b6a82c7ec7a4b0eddf4887b921abcea2527 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Thu, 1 Aug 2019 14:29:52 -0400 Subject: [PATCH 4/5] typo corrected Co-Authored-By: Jason Frey --- app/models/miq_report/seeding.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/miq_report/seeding.rb b/app/models/miq_report/seeding.rb index 33622b07b80..67456978ef7 100644 --- a/app/models/miq_report/seeding.rb +++ b/app/models/miq_report/seeding.rb @@ -15,7 +15,7 @@ def seed seed_record(f, reports[seed_filename(f)]) end - # now remove Default reports which are not suplied as yaml anymore + # now remove Default reports which are not supplied as yaml anymore reports = where(:rpt_type => 'Default').where.not(:filename => nil).index_by do |f| seed_filename(f.filename) end From 2f25071b999cc9dac99d8c90d4cd8e1dfc695db0 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Thu, 1 Aug 2019 14:30:59 -0400 Subject: [PATCH 5/5] use 'Default' word instead 'standard' in log message Co-Authored-By: Jason Frey --- app/models/miq_report/seeding.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/miq_report/seeding.rb b/app/models/miq_report/seeding.rb index 67456978ef7..94004c3729a 100644 --- a/app/models/miq_report/seeding.rb +++ b/app/models/miq_report/seeding.rb @@ -75,7 +75,7 @@ def seed_record(path, report) if duplicate&.rpt_type == "Custom" _log.warn("A custom report already exists with the name #{duplicate.name.inspect}. Skipping...") elsif duplicate - _log.warn("A standard report named '#{duplicate.name.inspect}' loaded from '#{duplicate.filename}' already exists. Updating attributes of existing report...") + _log.warn("A default report named '#{duplicate.name.inspect}' loaded from '#{duplicate.filename}' already exists. Updating attributes of existing report...") duplicate.update!(attrs) else raise