Module: Katello::Concerns::PulpDatabaseUnit::ClassMethods

Defined in:
app/models/katello/concerns/pulp_database_unit.rb

Instance Method Summary collapse

Instance Method Details

#association_nameObject



28
29
30
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 28

def association_name
  self.name.demodulize.underscore
end

#backend_identifier_fieldObject



44
45
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 44

def backend_identifier_field
end

#content_typeObject



40
41
42
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 40

def content_type
  self::CONTENT_TYPE
end

#content_unit_association_idObject



59
60
61
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 59

def content_unit_association_id
  "#{self.name.demodulize.underscore}_id".to_sym #Rpm => rpm_id
end

#content_unit_classObject



47
48
49
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 47

def content_unit_class
  "::Katello::Pulp::#{self.name.demodulize}".constantize
end

#content_units_nameObject



67
68
69
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 67

def content_units_name
  self.name.demodulize.pluralize.underscore.to_sym
end

#copy_repository_associations(source_repo, dest_repo) ⇒ Object



82
83
84
85
86
87
88
89
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 82

def copy_repository_associations(source_repo, dest_repo)
  delete_query = "delete from #{repository_association_class.table_name} where repository_id = #{dest_repo.id} and
                 #{unit_id_field} not in (select #{unit_id_field} from #{repository_association_class.table_name} where repository_id = #{source_repo.id})"
  ActiveRecord::Base.transaction do
    ActiveRecord::Base.connection.execute(delete_query)
    self.repository_association_class.import(db_columns_copy, db_values_copy(source_repo, dest_repo), validate: false)
  end
end

#db_columns_copyObject



91
92
93
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 91

def db_columns_copy
  [unit_id_field, backend_identifier_field, :repository_id].compact
end

#db_values_copy(source_repo, dest_repo) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 95

def db_values_copy(source_repo, dest_repo)
  db_values = []
  existing_unit_ids = self.repository_association_class.where(repository: dest_repo).pluck(unit_id_field)
  if existing_unit_ids.empty?
    new_units = self.repository_association_class.where(repository: source_repo)
  else
    new_units = self.repository_association_class.where(repository: source_repo).where.not("#{unit_id_field} in (?) ", existing_unit_ids)
  end
  unit_backend_identifier_field = backend_identifier_field
  unit_identifier_filed = unit_id_field
  new_units.each do |unit|
    db_values << [unit[unit_identifier_filed], unit[unit_backend_identifier_field], dest_repo.id].compact
  end
  db_values
end

#immutable_unit_typesObject



55
56
57
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 55

def immutable_unit_types
  [Katello::Rpm, Katello::Srpm]
end

#import_all(unit_ids, repository = nil, options = {}) ⇒ Object



76
77
78
79
80
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 76

def import_all(unit_ids, repository = nil, options = {})
  content_type = options[:content_type] || self.content_type
  filtered_indexing = options[:filtered_indexing] || false
  Katello::ContentUnitIndexer.new(content_type: Katello::RepositoryTypeManager.find_content_type(content_type), repository: repository, pulp_content_ids: unit_ids).import_all(filtered_indexing)
end

#import_for_repository(repo, options = {}) ⇒ Object



71
72
73
74
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 71

def import_for_repository(repo, options = {})
  content_type = options[:content_type] || self.content_type
  Katello::ContentUnitIndexer.new(content_type: Katello::RepositoryTypeManager.find_content_type(content_type), repository: repo).import_all
end

#in_repositories(repos) ⇒ Object



152
153
154
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 152

def in_repositories(repos)
  where(:id => repository_association_class.where(:repository_id => repos).select(unit_id_field))
end

#installable_for_content_facet(facet, env = nil, content_view = nil) ⇒ Object



111
112
113
114
115
116
117
118
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 111

def installable_for_content_facet(facet, env = nil, content_view = nil)
  repos = if env && content_view
            Katello::Repository.in_environment(env).in_content_views([content_view])
          else
            facet.bound_repositories.pluck(:id)
          end
  facet.send("applicable_#{content_units_name}".to_sym).in_repositories(repos)
end

#installable_for_hosts(hosts = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 120

def installable_for_hosts(hosts = nil)
  # Main goal of this query
  # 1) Get me the applicable content units for these set of hosts
  # 2) Now further prune this list. Only include units from repos that have been "enabled" on those hosts.
  #    In other words, prune the list to only include the units in the "bound" repositories signified by
  #    the inner join between ContentFacetRepository and Repository<Unit>

  facet_repos = Katello::ContentFacetRepository.joins(:content_facet => :host).select(:repository_id)
  facet_content_units = content_facet_association_class.joins(:content_facet => :host).select(content_unit_association_id)

  if hosts
    hosts = ::Host.where(id: hosts) if hosts.is_a?(Array)
    facet_repos = facet_repos.where(hosts: { id: hosts }).reorder(nil)
    facet_content_units = facet_content_units.where(hosts: { id: hosts }).reorder(nil)
  end

  self.joins(repository_association_units).
    where(repository_association_class.table_name => { :repository_id => facet_repos,
                                                       content_unit_association_id => facet_content_units }).distinct
end

#orphanedObject



148
149
150
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 148

def orphaned
  left_joins(repository_association.to_sym).where("#{repository_association_class.table_name}.#{unit_id_field}" => nil)
end

#pulp_data(pulp_id) ⇒ Object



156
157
158
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 156

def pulp_data(pulp_id)
  content_unit_class.new(pulp_id)
end

#repository_associationObject



51
52
53
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 51

def repository_association
  repository_association_class_name.demodulize.pluralize.underscore
end

#repository_association_classObject



36
37
38
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 36

def repository_association_class
  repository_association_class_name.constantize
end

#repository_association_class_nameObject



32
33
34
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 32

def repository_association_class_name
  "::Katello::Repository#{self.name.demodulize}"
end

#repository_association_unitsObject



63
64
65
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 63

def repository_association_units
  repository_association_class.name.demodulize.pluralize.underscore.to_sym
end

#unit_id_fieldObject



164
165
166
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 164

def unit_id_field
  "#{self.name.demodulize.underscore}_id"
end

#with_identifiers(ids) ⇒ Object



141
142
143
144
145
146
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 141

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}.pulp_id in (?)", id_integers, ids)
end

#with_pulp_id(unit_pulp_ids) ⇒ Object



160
161
162
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 160

def with_pulp_id(unit_pulp_ids)
  where('pulp_id in (?)', unit_pulp_ids)
end