Module: Katello::Concerns::PulpDatabaseUnit::ClassMethods
- Defined in:
- app/models/katello/concerns/pulp_database_unit.rb
Instance Method Summary collapse
- #content_unit_class ⇒ Object
-
#import_all(uuids = nil, additive = false) ⇒ Object
Import all units of a single type and refresh their repository associations.
- #in_repositories(repos) ⇒ Object
- #pulp_data(uuid) ⇒ Object
- #repository_association ⇒ Object
- #sync_repository_associations(repository, unit_uuids, additive = false) ⇒ Object
- #update_repository_associations(units_json, additive = false) ⇒ Object
- #with_identifiers(ids) ⇒ Object
- #with_uuid(unit_uuids) ⇒ Object
Instance Method Details
#content_unit_class ⇒ Object
15 16 17 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 15 def content_unit_class "::Katello::Pulp::#{self.name.demodulize}".constantize end |
#import_all(uuids = nil, additive = false) ⇒ Object
Import all units of a single type and refresh their repository associations
39 40 41 42 43 44 45 46 47 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 39 def import_all(uuids = nil, additive = false) all_items = uuids ? content_unit_class.fetch_by_uuids(uuids) : content_unit_class.fetch_all all_items.each do |item_json| item = self.where(:uuid => item_json['_id']).first_or_create item.update_from_json(item_json) end update_repository_associations(all_items, additive) all_items.count end |
#in_repositories(repos) ⇒ Object
30 31 32 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 30 def in_repositories(repos) self.joins(repository_association.to_sym).where("#{repository_association_class.table_name}.repository_id" => repos) end |
#pulp_data(uuid) ⇒ Object
34 35 36 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 34 def pulp_data(uuid) content_unit_class.new(uuid) end |
#repository_association ⇒ Object
19 20 21 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 19 def repository_association repository_association_class.name.demodulize.pluralize.underscore end |
#sync_repository_associations(repository, unit_uuids, additive = false) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 49 def sync_repository_associations(repository, unit_uuids, additive = false) associated_ids = with_uuid(unit_uuids).pluck(:id) table_name = self.repository_association_class.table_name attribute_name = "#{self.name.demodulize.underscore}_id" existing_ids = self.repository_association_class.uncached do self.repository_association_class.where(:repository_id => repository).pluck(attribute_name) end new_ids = associated_ids - existing_ids delete_ids = existing_ids - associated_ids queries = [] if delete_ids.any? && !additive queries << "DELETE FROM #{table_name} WHERE repository_id=#{repository.id} AND #{attribute_name} IN (#{delete_ids.join(', ')})" end unless new_ids.empty? inserts = new_ids.map { |unit_id| "(#{unit_id.to_i}, #{repository.id.to_i}, '#{Time.now.utc}', '#{Time.now.utc}')" } queries << "INSERT INTO #{table_name} (#{attribute_name}, repository_id, created_at, updated_at) VALUES #{inserts.join(', ')}" end ActiveRecord::Base.transaction do queries.each do |query| ActiveRecord::Base.connection.execute(query) end end end |
#update_repository_associations(units_json, additive = false) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 82 def update_repository_associations(units_json, additive = false) ActiveRecord::Base.transaction do repo_unit_id = {} units_json.each do |unit_json| unit_json['repository_memberships'].each do |repo_pulp_id| if Repository.exists?(:pulp_id => repo_pulp_id) repo_unit_id[repo_pulp_id] ||= [] repo_unit_id[repo_pulp_id] << unit_json['_id'] end end end repo_unit_id.each do |repo_pulp_id, unit_uuids| sync_repository_associations(Repository.find_by(:pulp_id => repo_pulp_id), unit_uuids, additive) end end end |
#with_identifiers(ids) ⇒ Object
23 24 25 26 27 28 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 23 def with_identifiers(ids) ids = [ids] unless ids.is_a?(Array) ids.map!(&:to_s) id_integers = ids.map { |string| Integer(string) rescue -1 } where("#{self.table_name}.id in (?) or #{self.table_name}.uuid in (?)", id_integers, ids) end |
#with_uuid(unit_uuids) ⇒ Object
78 79 80 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 78 def with_uuid(unit_uuids) where(:uuid => unit_uuids) end |