Skip to content

Commit

Permalink
Merge pull request #19080 from yrudman/fixed-miq-report-seeding-when-…
Browse files Browse the repository at this point in the history
…filename-changed

Fixed seeding failure if yaml file containing report was renamed
  • Loading branch information
Fryguy authored Aug 1, 2019
2 parents e9eb138 + 2f25071 commit 44c47fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/models/miq_report/seeding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ def seed
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, reports.delete(seed_filename(f)))
seed_record(f, reports[seed_filename(f)])
end

# 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
seed_files.each do |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(", ")}")

Expand Down Expand Up @@ -67,6 +74,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 default report named '#{duplicate.name.inspect}' loaded from '#{duplicate.filename}' already exists. Updating attributes of existing report...")
duplicate.update!(attrs)
else
raise
end
Expand Down
14 changes: 14 additions & 0 deletions spec/models/miq_report/seeding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 44c47fc

Please sign in to comment.