Skip to content

Commit

Permalink
MiqSearch.seed
Browse files Browse the repository at this point in the history
- remove N+1
- remove duplicate records
- display warning for duplicate records
  • Loading branch information
kbrock committed Sep 21, 2017
1 parent f8e6ef2 commit f3b00fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
16 changes: 12 additions & 4 deletions app/models/miq_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,32 @@ def self.descriptions

FIXTURE_DIR = File.join(Rails.root, "db/fixtures")
def self.seed
searches = where("name like 'default%'").index_by { |ms| "#{ms.name}-#{ms.db}" }
fixture_file = File.join(FIXTURE_DIR, "miq_searches.yml")
slist = YAML.load_file(fixture_file) if File.exist?(fixture_file)
slist ||= []

slist.group_by { |s| [s['attributes']['name'], s['attributes']['db']] }.each do |(name, db), dups|
_log.warn("duplicate entry for db=#{db} name=#{name}") if dups.size > 1
end
slist.each do |search|
attrs = search['attributes']
name = attrs['name']
db = attrs['db']

rec = find_by(:name => name, :db => db)
rec = searches["#{name}-#{db}"]
if rec.nil?
_log.info("Creating [#{name}]")
create!(attrs)
searches["#{name}-#{db}"] = 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')

# properly compare filter
filter = attrs.delete('filter')
rec.filter = filter if rec.filter.exp != filter.exp
rec.attributes = attrs
rec.save!

rec.save! if rec.changed?
end
end
end
Expand Down
21 changes: 0 additions & 21 deletions db/fixtures/miq_searches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,6 @@
value: "on"
search_type: default
db: Host
- attributes:
name: default_Status / Running
description: Status / Running
filter: !ruby/object:MiqExpression
exp:
INCLUDES:
field: Host-power_state
value: "on"
search_type: default
db: Host
- attributes:
name: default_Status / Stopped
description: Status / Stopped
Expand Down Expand Up @@ -379,17 +369,6 @@
value: "on"
search_type: default
db: Host
- attributes:
name: default_Status / Stopped
description: Status / Stopped
filter: !ruby/object:MiqExpression
exp:
not:
INCLUDES:
field: Host-power_state
value: "on"
search_type: default
db: Host
- attributes:
name: default_Store Type / NFS
description: Store Type / NFS
Expand Down

0 comments on commit f3b00fc

Please sign in to comment.