Class: Katello::Rpm

Inherits:
Model
  • Object
show all
Includes:
Concerns::PulpDatabaseUnit
Defined in:
app/models/katello/rpm.rb

Constant Summary collapse

CONTENT_TYPE =
'rpm'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::PulpDatabaseUnit

#backend_data, #library_repositories, #remove_from_repository

Methods inherited from Model

#destroy!

Class Method Details

.applicable_to_hosts(hosts) ⇒ Object



213
214
215
216
# File 'app/models/katello/rpm.rb', line 213

def self.applicable_to_hosts(hosts)
  self.joins(:content_facets).
    where("#{Katello::Host::ContentFacet.table_name}.host_id" => hosts).distinct
end

.content_facet_association_classObject



31
32
33
# File 'app/models/katello/rpm.rb', line 31

def self.content_facet_association_class
  ContentFacetApplicableRpm
end

.default_sortObject



27
28
29
# File 'app/models/katello/rpm.rb', line 27

def self.default_sort
  order(:name).order(:epoch).order(:version_sortable).order(:release_sortable)
end

.latest(relation) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'app/models/katello/rpm.rb', line 225

def self.latest(relation)
  # There are several different ways to implement this:

  # 1) Use a LEFT OUTER JOIN to join the query with itself on name=name,
  #    arch=arch, evr<evr, then select unmatched rows.
  #    In PostgreSQL 9.2 populated with real data, EXPLAIN shows
  #    cost=339901.07..381834.02 for the SQL generated by this when a
  #    package search is performed with a content_view_version_id filter.
  #    Unfortunately, this requires writing raw SQL for the JOIN and using
  #    to_sql, which may be somewhat brittle.  For example, be aware of
  #    quirks like https://github.com/rails/rails/issues/18379
  return relation.joins(
    "LEFT OUTER JOIN (#{relation.to_sql}) AS katello_rpms2 ON " \
    'katello_rpms.name = katello_rpms2.name AND katello_rpms.arch = katello_rpms2.arch AND ' \
    '(CAST(katello_rpms.epoch AS INT) < CAST(katello_rpms2.epoch AS INT) OR ' \
    ' (CAST(katello_rpms.epoch AS INT) = CAST(katello_rpms2.epoch AS INT) AND ' \
    '  (katello_rpms.version_sortable COLLATE "C" < katello_rpms2.version_sortable COLLATE "C" OR ' \
    '   (katello_rpms.version_sortable = katello_rpms2.version_sortable AND ' \
    '    katello_rpms.release_sortable COLLATE "C" < katello_rpms2.release_sortable COLLATE "C"))))'
  ).where('katello_rpms2.release_sortable IS NULL')

  # 2) Use GROUP BY with MAX() and several INNER JOINs to identify the
  #    latest evr, then INNER JOIN again to get complete rows.
  #    Like #1, this must also be written using raw SQL and to_sql.
  #    cost=677566.03..704889.89
  #max_epochs = relation.
  #  group(:name, :arch).
  #  select(:name, :arch, 'MAX(CAST(katello_rpms.epoch AS INT)) AS max_epoch')
  #max_versions = relation.
  #  joins(
  #    "INNER JOIN (#{max_epochs.to_sql}) katello_rpms2 ON "    #    'katello_rpms.name = katello_rpms2.name AND katello_rpms.arch = katello_rpms2.arch AND '    #    'CAST(katello_rpms.epoch AS INT) = katello_rpms2.max_epoch'
  #  ).
  #  group(:name, :arch, :epoch).
  #  select(:name, :arch, :epoch, 'MAX(katello_rpms.version_sortable COLLATE "C") AS max_version')
  #max_releases = relation.
  #  joins(
  #    "INNER JOIN (#{max_versions.to_sql}) katello_rpms2 ON "    #    'katello_rpms.name = katello_rpms2.name AND katello_rpms.arch = katello_rpms2.arch AND '    #    'CAST(katello_rpms.epoch AS INT) = CAST(katello_rpms2.epoch AS INT) AND '    #    'katello_rpms.version_sortable = katello_rpms2.max_version'
  #  ).
  #  group(:name, :arch, :epoch, :version_sortable).
  #  select(:name, :arch, :epoch, :version_sortable, 'MAX(katello_rpms.release_sortable COLLATE "C") AS max_release')
  #return relation.
  #  joins(
  #    "INNER JOIN (#{max_releases.to_sql}) katello_rpms2 ON "    #    'katello_rpms.name = katello_rpms2.name AND katello_rpms.arch = katello_rpms2.arch AND '    #    'CAST(katello_rpms.epoch AS INT) = CAST(katello_rpms2.epoch AS INT) AND '    #    'katello_rpms.version_sortable = katello_rpms2.version_sortable AND '    #    'katello_rpms.release_sortable = katello_rpms2.max_release'
  #  )

  # 3) Like #1, but use a Common Table Expression instead of a
  #    LEFT OUTER JOIN to join the query with itself.
  #    This seems to be faster than #1 in some cases and slower in others
  #    depending on optimizer behavior.
  #    For the same test case as used for #1, cost=404240.71..405796.40
  #    However, this is not supported in all versions of all databases
  #    (e.g. it only works in MySQL >=8.0).
  #    This can be written using either raw SQL or Arel.
  #
  # 3a) Raw SQL
  #cte_sql = relation.select('katello_rpms.*, CAST(katello_rpms.epoch AS INT) AS epoch_sortable').to_sql
  #return Rpm.where('katello_rpms.id IN ('    #  "WITH katello_rpms AS (#{cte_sql}) "    #  'SELECT katello_rpms.id from katello_rpms LEFT OUTER JOIN katello_rpms AS katello_rpms2 ON '    #  'katello_rpms.name = katello_rpms2.name AND katello_rpms.arch = katello_rpms2.arch AND '    #  '(katello_rpms.epoch_sortable < katello_rpms2.epoch_sortable OR '    #  ' (katello_rpms.epoch_sortable = katello_rpms2.epoch_sortable AND '    #  '  (katello_rpms.version_sortable COLLATE "C" < katello_rpms2.version_sortable COLLATE "C" OR '    #  '   (katello_rpms.version_sortable = katello_rpms2.version_sortable AND '    #  '    katello_rpms.release_sortable COLLATE "C" < katello_rpms2.release_sortable COLLATE "C")))) '    #  'WHERE katello_rpms2.release_sortable IS NULL'    #')')
  #
  # 3b) Arel
  #     Unlike #1, #2, and #3a, this does not require raw SQL or to_sql.
  #     Unfortunately this doesn't work reliably in Rails 4.2 because
  #     relation.unscoped.where(relation.arel_table[:id].in(relation.arel))
  #     looses the prepared statement parameter values.  See:
  #     https://github.com/rails/rails/issues/13686
  ## Convert current query to Arel for use in a CTE
  #orig_query = relation.arel
  ## Add another column to the original query: CAST(epoch AS INT) AS epoch_sortable
  #orig_table = relation.arel_table
  #orig_query = orig_query.project(Arel::Nodes::NamedFunction.new('CAST', [orig_table[:epoch].as('INT')], 'epoch_sortable'))
  ## Create two names that will both refer to the existing query in the CTE
  #cte1 = Arel::Table.new(:katello_rpms)
  #cte2 = Arel::Table.new(:katello_rpms2)
  ## Create a new Arel query and use a CTE to JOIN the original query with
  ## itself and select the latest evr for each name/arch
  #new_query = cte1.project(cte1[:id]).with(Arel::Nodes::As.new(cte1, orig_query)).
  #  join(Arel::Nodes::As.new(cte1, cte2), Arel::Nodes::OuterJoin).on(
  #    cte1[:name].eq(cte2[:name]).and(cte1[:arch].eq(cte2[:arch])).and(
  #      cte1[:epoch_sortable].lt(cte2[:epoch_sortable]).or(
  #        cte1[:epoch_sortable].eq(cte2[:epoch_sortable]).and(
  #          Arel::Nodes::SqlLiteral.new('katello_rpms.version_sortable COLLATE "C"').lt(Arel::Nodes::SqlLiteral.new('katello_rpms2.version_sortable COLLATE "C"')).or(
  #            cte1[:version_sortable].eq(cte2[:version_sortable]).and(
  #              Arel::Nodes::SqlLiteral.new('katello_rpms.release_sortable COLLATE "C"').lt(Arel::Nodes::SqlLiteral.new('katello_rpms2.release_sortable COLLATE "C"'))
  #            )
  #          )
  #        )
  #      )
  #    )
  #  ).
  #  where(cte2[:release_sortable].eq(nil))
  ## Wrap the new Arel query in a new ActiveRecord::Relation
  #return relation.unscoped.where(orig_table[:id].in(new_query))
end

.scoped_search_evr(_key, operator, value) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/katello/rpm.rb', line 69

def self.scoped_search_evr(_key, operator, value)
  if ['=', '<>'].include?(operator)
    return self.scoped_search_evr_equal(operator, value)
  elsif ['IN', 'NOT IN'].include?(operator)
    return self.scoped_search_evr_in(operator, value)
  elsif ['LIKE', 'NOT LIKE'].include?(operator)
    return self.scoped_search_evr_like(operator, value)
  elsif ['>', '>=', '<', '<='].include?(operator)
    return self.scoped_search_evr_compare(operator, value)
  else
    return {}
  end
end

.scoped_search_evr_compare(operator, value) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/katello/rpm.rb', line 149

def self.scoped_search_evr_compare(operator, value)
  evr = Util::Package.parse_evr(value)
  (e, v, r) = [evr[:epoch], evr[:version], evr[:release]]
  e = e.to_i # nil or blank becomes 0
  conditions = ''
  if v.nil? || v.blank?
    conditions = "CAST(#{self.table_name}.epoch AS INT) #{operator} ?"
    parameters = [e]
  else
    sv = Util::Package.sortable_version(v)
    if r.nil? || r.blank?
      conditions = "#{self.table_name}.version_sortable COLLATE \"C\" #{operator} ? COLLATE \"C\""
      parameters = [sv]
    else
      conditions =
        "#{self.table_name}.version_sortable COLLATE \"C\" #{operator[0]} ? COLLATE \"C\" OR " \
        "(#{self.table_name}.version_sortable = ? AND " \
         "#{self.table_name}.release_sortable COLLATE \"C\" #{operator} ? COLLATE \"C\")"
      sv = Util::Package.sortable_version(v)
      parameters = [sv, sv, Util::Package.sortable_version(r)]
    end
    conditions = "CAST(#{self.table_name}.epoch AS INT) #{operator[0]} ? OR (CAST(#{self.table_name}.epoch AS INT) = ? AND (#{conditions}))"
    parameters = [e, e] + parameters
  end
  return { :conditions => conditions, :parameter => parameters }
end

.scoped_search_evr_equal(operator, value) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/katello/rpm.rb', line 83

def self.scoped_search_evr_equal(operator, value)
  joiner = (operator == '=' ? 'AND' : 'OR')
  evr = Util::Package.parse_evr(value)
  (e, v, r) = [evr[:epoch], evr[:version], evr[:release]]
  e = e.to_i # nil or blank becomes 0
  conditions = "CAST(#{self.table_name}.epoch AS INT) #{operator} ?"
  parameters = [e]
  unless v.nil? || v.blank?
    conditions += " #{joiner} #{self.table_name}.version #{operator} ?"
    parameters += [v]
  end
  unless r.nil? || r.blank?
    conditions += " #{joiner} #{self.table_name}.release #{operator} ?"
    parameters += [r]
  end
  return { :conditions => conditions, :parameter => parameters }
end

.scoped_search_evr_in(operator, value) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/katello/rpm.rb', line 101

def self.scoped_search_evr_in(operator, value)
  op = (operator == 'IN' ? '=' : '<>')
  joiner1 = (operator == 'IN' ? 'AND' : 'OR')
  joiner2 = (operator == 'IN' ? 'OR' : 'AND')
  conditions = []
  parameters = []
  value.split(',').collect { |v| v.strip }.each do |val|
    evr = Util::Package.parse_evr(val)
    (e, v, r) = [evr[:epoch], evr[:version], evr[:release]]
    e = e.to_i # nil or blank becomes 0
    condition = "CAST(#{self.table_name}.epoch AS INT) #{op} ?"
    parameters += [e]
    unless v.nil? || v.blank?
      condition += " #{joiner1} #{self.table_name}.version #{op} ?"
      parameters += [v]
    end
    unless r.nil? || r.blank?
      condition += " #{joiner1} #{self.table_name}.release #{op} ?"
      parameters += [r]
    end
    conditions += ["(#{condition})"]
  end
  return { :conditions => conditions.join(" #{joiner2} "), :parameter => parameters }
end

.scoped_search_evr_like(operator, value) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/katello/rpm.rb', line 126

def self.scoped_search_evr_like(operator, value)
  val = (value !~ /^\%|\*/ && value !~ /\%|\*$/) ? "%#{value}%" : value.tr_s('%*', '%')
  evr = Util::Package.parse_evr(val)
  (e, v, r) = [evr[:epoch], evr[:version], evr[:release]]
  conditions = []
  parameters = []
  unless e.nil? || e.blank?
    conditions += ["CAST(#{self.table_name}.epoch AS VARCHAR(10)) #{operator} ?"]
    parameters += [e]
  end
  unless v.nil? || v.blank?
    conditions += ["#{self.table_name}.version #{operator} ?"]
    parameters += [v]
  end
  unless r.nil? || r.blank?
    conditions += ["#{self.table_name}.release #{operator} ?"]
    parameters += [r]
  end
  return {} if conditions.empty?
  joiner = (operator == 'LIKE' ? 'AND' : 'OR')
  return { :conditions => conditions.join(" #{joiner} "), :parameter => parameters }
end

.scoped_search_release(_key, operator, value) ⇒ Object



39
40
41
# File 'app/models/katello/rpm.rb', line 39

def self.scoped_search_release(_key, operator, value)
  self.scoped_search_sortable('release', operator, value)
end

.scoped_search_sortable(column, operator, value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/katello/rpm.rb', line 43

def self.scoped_search_sortable(column, operator, value)
  if ['>', '>=', '<', '<='].include?(operator)
    conditions = "#{self.table_name}.#{column}_sortable COLLATE \"C\" #{operator} ? COLLATE \"C\""
    parameter = Util::Package.sortable_version(value)
    return { :conditions => conditions, :parameter => [parameter] }
  end

  # Use the default behavior for all other operators.
  # Unfortunately there is no way to call the default behavior from here, so
  # we replicate the default behavior from sql_test() in
  # https://github.com/wvanbergen/scoped_search/blob/master/lib/scoped_search/query_builder.rb
  if ['LIKE', 'NOT LIKE'].include?(operator)
    conditions = "#{self.table_name}.#{column} #{operator} ?"
    parameter = (value !~ /^\%|\*/ && value !~ /\%|\*$/) ? "%#{value}%" : value.tr_s('%*', '%')
    return { :conditions => conditions, :parameter => [parameter] }
  elsif ['IN', 'NOT IN'].include?(operator)
    conditions = "#{self.table_name}.#{column} #{operator} (#{value.split(',').collect { '?' }.join(',')})"
    parameters = value.split(',').collect { |v| v.strip }
    return { :conditions => conditions, :parameter => parameters }
  else
    conditions = "#{self.table_name}.#{column} #{operator} ?"
    parameter = value
    return { :conditions => conditions, :parameter => [parameter] }
  end
end

.scoped_search_version(_key, operator, value) ⇒ Object



35
36
37
# File 'app/models/katello/rpm.rb', line 35

def self.scoped_search_version(_key, operator, value)
  self.scoped_search_sortable('version', operator, value)
end

.search_version_equal(version) ⇒ Object



183
184
185
# File 'app/models/katello/rpm.rb', line 183

def self.search_version_equal(version)
  Katello::Util::PackageFilter.new(self, version, Katello::Util::PackageFilter::EQUAL).results
end

.search_version_range(min = nil, max = nil) ⇒ Object



176
177
178
179
180
181
# File 'app/models/katello/rpm.rb', line 176

def self.search_version_range(min = nil, max = nil)
  query = self.all
  query = Katello::Util::PackageFilter.new(query, min, Katello::Util::PackageFilter::GREATER_THAN).results if min.present?
  query = Katello::Util::PackageFilter.new(query, max, Katello::Util::PackageFilter::LESS_THAN).results if max.present?
  query
end

.total_for_repositories(repos) ⇒ Object



187
188
189
# File 'app/models/katello/rpm.rb', line 187

def self.total_for_repositories(repos)
  self.in_repositories(repos).count
end

.yum_installable_for_host(host) ⇒ Object

Return RPMs that are not installed on a host, but could be installed the word ‘installable’ has a different meaning here than elsewhere



220
221
222
223
# File 'app/models/katello/rpm.rb', line 220

def self.yum_installable_for_host(host)
  repos = host.content_facet.bound_repositories.pluck(:id)
  Katello::Rpm.in_repositories(repos).where.not(name: host.installed_packages.pluck(:name)).order(:name)
end

Instance Method Details

#build_nvraObject



195
196
197
# File 'app/models/katello/rpm.rb', line 195

def build_nvra
  Util::Package.build_nvra(self.attributes.with_indifferent_access)
end

#hosts_applicable(org_id = nil) ⇒ Object



199
200
201
202
203
204
205
# File 'app/models/katello/rpm.rb', line 199

def hosts_applicable(org_id = nil)
  if org_id.present?
    self.content_facets.joins(:host).where("#{::Host.table_name}.organization_id" => org_id)
  else
    self.content_facets.joins(:host)
  end
end

#hosts_available(org_id = nil) ⇒ Object



207
208
209
210
211
# File 'app/models/katello/rpm.rb', line 207

def hosts_available(org_id = nil)
  self.hosts_applicable(org_id).joins("INNER JOIN #{Katello::RepositoryRpm.table_name} on \
    #{Katello::RepositoryRpm.table_name}.rpm_id = #{self.id}").joins(:content_facet_repositories).
    where("#{Katello::ContentFacetRepository.table_name}.repository_id = #{Katello::RepositoryRpm.table_name}.repository_id").uniq
end

#nvreaObject



191
192
193
# File 'app/models/katello/rpm.rb', line 191

def nvrea
  Util::Package.build_nvrea(self.attributes.with_indifferent_access, false)
end