Skip to content

Commit

Permalink
Merge pull request #19151 from yrudman/fix-seed-miq-search
Browse files Browse the repository at this point in the history
Fixed MiqSearch.seed  when search name was changed and corrected typo miq_search.yml

(cherry picked from commit 0e86685)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1767777
  • Loading branch information
Fryguy authored and simaishi committed Nov 1, 2019
1 parent 279b104 commit f89f6c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/models/miq_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def self.seed
name = attrs['name']
db = attrs['db']

rec = searches["#{name}-#{db}"]
rec = searches.delete("#{name}-#{db}")
if rec.nil?
_log.info("Creating [#{name}]")
searches["#{name}-#{db}"] = create!(attrs)
create!(attrs)
else
# Avoid undoing user changes made to enable/disable default searches which is held in the search_key column
attrs.delete('search_key')
Expand All @@ -109,6 +109,10 @@ def self.seed
rec.save! if rec.changed?
end
end
if searches.any?
_log.warn("Deleting the following MiqSearch(es) as they no longer exist: #{searches.keys.sort.collect(&:inspect).join(", ")}")
MiqSearch.destroy(searches.values.map(&:id))
end
end

def self.display_name(number = 1)
Expand Down
4 changes: 2 additions & 2 deletions db/fixtures/miq_searches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@
search_type: default
db: Host
- attributes:
name: default_Status / Orhaned
description: Status / Orhaned
name: default_Status / Orphaned
description: Status / Orphaned
filter: !ruby/object:MiqExpression
exp:
"=":
Expand Down
44 changes: 44 additions & 0 deletions spec/models/miq_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,48 @@
expect(search.errors[:base][0]).to eq("Search is referenced in a schedule and cannot be deleted")
end
end

# This test is intentionally long winded instead of breaking it up into
# multiple tests per concern because of how long a full seed may take.
describe ".seed" do
let(:tmpdir) { Pathname.new(Dir.mktmpdir) }
let(:fixture_dir) { tmpdir.join("db/fixtures") }
let(:search_yml) { fixture_dir.join("miq_searches.yml") }

before do
FileUtils.mkdir_p(fixture_dir)
FileUtils.cp_r(Rails.root.join('db', 'fixtures', 'miq_searches.yml'), search_yml)
stub_const("MiqSearch::FIXTURE_DIR", fixture_dir)
described_class.seed
end

after do
FileUtils.rm_rf(tmpdir)
end

it "seeds miq_search table from db/fixtures/miq_search.yml and keeps custom searches" do
yml = YAML.load_file(search_yml)

# check if all supplied default searches were loaded
expect(MiqSearch.count).to eq(yml.size)

# check if custom searches were not removed
custom_search = "some search"
FactoryBot.create(:miq_search, :name => custom_search)
described_class.seed
expect(MiqSearch.count).to eq(yml.size + 1)
expect(MiqSearch.where(:name => custom_search)).to exist

# check that default search removed from DB if name-db of that search was not present in miq_search_yml
old_name = yml[0]["attributes"]["name"]
db = yml[0]["attributes"]["db"]
new_name = "default_Absolutely New Name"
yml[0]["attributes"]["name"] = new_name
File.write(search_yml, yml.to_yaml)
described_class.seed
expect(MiqSearch.count).to eq(yml.size + 1)
expect(MiqSearch.where(:name => new_name, :db => db)).to exist
expect(MiqSearch.where(:name => old_name, :db => db)).to be_empty
end
end
end

0 comments on commit f89f6c3

Please sign in to comment.