Skip to content

Commit

Permalink
Merge pull request #15596 from kbrock/seeding_timeout_roles
Browse files Browse the repository at this point in the history
[Performance] MiqUserRole.seed speedup
  • Loading branch information
bdunne authored Feb 8, 2018
2 parents 82fc8d9 + 3c5a245 commit b5d6684
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 66 deletions.
13 changes: 8 additions & 5 deletions app/models/miq_user_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,29 @@ def self.with_allowed_roles_for(user_or_group)
end

def self.seed
seed_from_array(YAML.load_file(FIXTURE_YAML))
roles = all.index_by(&:name)
seed_from_array(roles, YAML.load_file(FIXTURE_YAML))

# NOTE: typically there are no extra fixtures (so merge_features is typically false)
Dir.glob(File.join(FIXTURE_PATH, "*.yml")).each do |fixture|
seed_from_array(YAML.load_file(fixture), true)
seed_from_array(roles, YAML.load_file(fixture), true)
end
end

def self.seed_from_array(array, merge_features = false)
def self.seed_from_array(roles, array, merge_features = false)
array.each do |hash|
feature_ids = hash.delete(:miq_product_feature_identifiers)

hash[:miq_product_features] = MiqProductFeature.where(:identifier => feature_ids).to_a
role = find_by(:name => hash[:name]) || new(hash.except(:id))
role = roles[hash[:name]] ||= new(hash.except(:id))
new_role = role.new_record?
hash[:miq_product_features] &&= role.miq_product_features if !new_role && merge_features
unless role.settings.nil? # Makse sure existing settings are merged in with the new ones.
new_settings = hash.delete(:settings) || {}
role.settings.merge!(new_settings)
end
role.update_attributes(hash.except(:id))
role.attributes = hash.except(:id)
role.save if role.changed?
end
end

Expand Down
Loading

0 comments on commit b5d6684

Please sign in to comment.