Skip to content

Commit

Permalink
Merge pull request #14722 from isimluk/speed-up-miq_event_definition-…
Browse files Browse the repository at this point in the history
…seeds-2

Avoid dozens of extra selects in seed_default_events
  • Loading branch information
kbrock authored Apr 20, 2017
2 parents a8afcb8 + 76d4220 commit 4ac1708
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/models/miq_event_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def self.seed
end

def self.seed_default_events
event_sets = MiqEventDefinitionSet.all.index_by(&:name)
fname = File.join(FIXTURE_DIR, "#{to_s.pluralize.underscore}.csv")
CSV.foreach(fname, :headers => true, :skip_lines => /^#/, :skip_blanks => true) do |csv_row|
event = csv_row.to_hash
Expand All @@ -139,7 +140,7 @@ def self.seed_default_events
end
end

es = MiqEventDefinitionSet.find_by(:name => set_type)
es = event_sets[set_type]
rec.memberof.each { |old_set| rec.make_not_memberof(old_set) unless old_set == es } # handle changes in set membership
es.add_member(rec) if es && !es.members.include?(rec)
end
Expand Down
31 changes: 18 additions & 13 deletions spec/models/miq_event_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,29 @@ def create_set!(name)
expect(event.definition).to be_nil
end

it "won't update an event with a definition (keyed as a string)" do
described_class.seed_default_events
name = described_class.first.name
attributes = {"name" => name, "definition" => {:event => {:message => "`rm -rf /super/secret/file`"}}}
context 'with defaults in db' do
before do
MiqEventDefinitionSet.seed
described_class.seed_default_events
end

event, = described_class.import_from_hash(attributes)
it "won't update an event with a definition (keyed as a string)" do
name = described_class.first.name
attributes = {"name" => name, "definition" => {:event => {:message => "`rm -rf /super/secret/file`"}}}

expect(event.definition).to be_nil
end
event, = described_class.import_from_hash(attributes)

it "won't update an event with a definition (keyed as a symbol)" do
described_class.seed_default_events
name = described_class.first.name
attributes = {"name" => name, :definition => {:event => {:message => "`rm -rf /super/secret/file`"}}}
expect(event.definition).to be_nil
end

event, = described_class.import_from_hash(attributes)
it "won't update an event with a definition (keyed as a symbol)" do
name = described_class.first.name
attributes = {"name" => name, :definition => {:event => {:message => "`rm -rf /super/secret/file`"}}}

expect(event.definition).to be_nil
event, = described_class.import_from_hash(attributes)

expect(event.definition).to be_nil
end
end
end
end

0 comments on commit 4ac1708

Please sign in to comment.