Module: Katello::Concerns::PulpDatabaseUnit::ClassMethods
- Defined in:
- app/models/katello/concerns/pulp_database_unit.rb
Instance Method Summary collapse
- #association_name ⇒ Object
- #backend_identifier_field ⇒ Object
- #content_type ⇒ Object
- #content_unit_association_id ⇒ Object
- #content_unit_class ⇒ Object
- #content_units_name ⇒ Object
- #copy_repository_associations(source_repo, dest_repo) ⇒ Object
- #db_columns_copy ⇒ Object
- #db_values_copy(source_repo, dest_repo) ⇒ Object
- #immutable_unit_types ⇒ Object
- #import_all(unit_ids, repository = nil, options = {}) ⇒ Object
- #import_for_repository(repo, options = {}) ⇒ Object
- #in_repositories(repos) ⇒ Object
- #installable_for_content_facet(facet, env = nil, content_view = nil) ⇒ Object
- #installable_for_hosts(hosts = nil) ⇒ Object
- #many_repository_associations ⇒ Object
- #pulp_data(pulp_id) ⇒ Object
- #repository_association ⇒ Object
- #repository_association_class ⇒ Object
- #repository_association_class_name ⇒ Object
- #repository_association_units ⇒ Object
- #unit_id_field ⇒ Object
- #with_identifiers(ids) ⇒ Object
- #with_pulp_id(unit_pulp_ids) ⇒ Object
Instance Method Details
#association_name ⇒ Object
30 31 32 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 30 def association_name self.name.demodulize.underscore end |
#backend_identifier_field ⇒ Object
46 47 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 46 def backend_identifier_field end |
#content_type ⇒ Object
42 43 44 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 42 def content_type self::CONTENT_TYPE end |
#content_unit_association_id ⇒ Object
65 66 67 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 65 def content_unit_association_id "#{self.name.demodulize.underscore}_id".to_sym #Rpm => rpm_id end |
#content_unit_class ⇒ Object
49 50 51 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 49 def content_unit_class "::Katello::Pulp::#{self.name.demodulize}".constantize end |
#content_units_name ⇒ Object
73 74 75 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 73 def content_units_name self.name.demodulize.pluralize.underscore.to_sym end |
#copy_repository_associations(source_repo, dest_repo) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 88 def copy_repository_associations(source_repo, dest_repo) if many_repository_associations 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 else columns = column_names - ["id", "pulp_id", "created_at", "updated_at", "repository_id"] queries = [] queries << "delete from #{self.table_name} where repository_id = #{dest_repo.id} and pulp_id not in (select pulp_id from #{self.table_name} where repository_id = #{source_repo.id})" queries << "insert into #{self.table_name} (repository_id, pulp_id, #{columns.join(',')}) select #{dest_repo.id} as repository_id, pulp_id, #{columns.join(',')} from #{self.table_name} where repository_id = #{source_repo.id} and pulp_id not in (select pulp_id from #{self.table_name} where repository_id = #{dest_repo.id})" ActiveRecord::Base.transaction do queries.each do |query| ActiveRecord::Base.connection.execute(query) end end end end |
#db_columns_copy ⇒ Object
113 114 115 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 113 def db_columns_copy [unit_id_field, backend_identifier_field, :repository_id].compact end |
#db_values_copy(source_repo, dest_repo) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 117 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_types ⇒ Object
61 62 63 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 61 def immutable_unit_types [Katello::Rpm, Katello::Srpm] end |
#import_all(unit_ids, repository = nil, options = {}) ⇒ Object
82 83 84 85 86 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 82 def import_all(unit_ids, repository = nil, = {}) content_type = [:content_type] || self.content_type filtered_indexing = [: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
77 78 79 80 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 77 def import_for_repository(repo, = {}) content_type = [: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
170 171 172 173 174 175 176 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 170 def in_repositories(repos) if many_repository_associations where(:id => repository_association_class.where(:repository_id => repos).select(unit_id_field)) else where(:repository_id => repos) end end |
#installable_for_content_facet(facet, env = nil, content_view = nil) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 133 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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 142 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 |
#many_repository_associations ⇒ Object
53 54 55 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 53 def many_repository_associations self != YumMetadataFile end |
#pulp_data(pulp_id) ⇒ Object
178 179 180 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 178 def pulp_data(pulp_id) content_unit_class.new(pulp_id) end |
#repository_association ⇒ Object
57 58 59 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 57 def repository_association repository_association_class_name.demodulize.pluralize.underscore end |
#repository_association_class ⇒ Object
38 39 40 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 38 def repository_association_class repository_association_class_name.constantize end |
#repository_association_class_name ⇒ Object
34 35 36 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 34 def repository_association_class_name "::Katello::Repository#{self.name.demodulize}" end |
#repository_association_units ⇒ Object
69 70 71 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 69 def repository_association_units repository_association_class.name.demodulize.pluralize.underscore.to_sym end |
#unit_id_field ⇒ Object
186 187 188 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 186 def unit_id_field "#{self.name.demodulize.underscore}_id" end |
#with_identifiers(ids) ⇒ Object
163 164 165 166 167 168 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 163 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
182 183 184 |
# File 'app/models/katello/concerns/pulp_database_unit.rb', line 182 def with_pulp_id(unit_pulp_ids) where('pulp_id in (?)', unit_pulp_ids) end |