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

Fixed seeding failure if yaml file containing report was renamed #19080

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
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 suplied as yaml anymore
yrudman marked this conversation as resolved.
Show resolved Hide resolved
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 standard report named '#{duplicate.name.inspect}' loaded from '#{duplicate.filename}' already exists. Updating attributes of existing report...")
yrudman marked this conversation as resolved.
Show resolved Hide resolved
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