Class: Packages::Dependency

Inherits:
ApplicationRecord show all
Includes:
EachBatch
Defined in:
app/models/packages/dependency.rb

Constant Summary collapse

NAME_VERSION_PATTERN_TUPLE_MATCHING =
'(name, version_pattern) = (?, ?)'
MAX_STRING_LENGTH =
255
MAX_CHUNKED_QUERIES_COUNT =
10

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, nullable_column?, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.for_package_project_id_names_and_version_patterns(project_id, names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200) ⇒ Object



38
39
40
41
42
43
44
# File 'app/models/packages/dependency.rb', line 38

def self.for_package_project_id_names_and_version_patterns(project_id, names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200)
  ids = ids_for_package_project_id_names_and_version_patterns(project_id, names_and_version_patterns, chunk_size, max_rows_limit)

  return none if ids.empty?

  id_in(ids)
end

.ids_for_package_project_id_names_and_version_patterns(project_id, names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/packages/dependency.rb', line 18

def self.ids_for_package_project_id_names_and_version_patterns(project_id, names_and_version_patterns = {}, chunk_size = 50, max_rows_limit = 200)
  names_and_version_patterns.reject! { |key, value| key.size > MAX_STRING_LENGTH || value.size > MAX_STRING_LENGTH }
  raise ArgumentError, 'Too many names_and_version_patterns' if names_and_version_patterns.size > MAX_CHUNKED_QUERIES_COUNT * chunk_size

  matched_ids = []
  names_and_version_patterns.each_slice(chunk_size) do |tuples|
    where_statement = Array.new(tuples.size, NAME_VERSION_PATTERN_TUPLE_MATCHING)
                           .join(' OR ')
    ids = where(where_statement, *tuples.flatten)
            .where(project_id: project_id)
            .limit(max_rows_limit + 1)
            .pluck(:id)
    matched_ids.concat(ids)

    raise ArgumentError, 'Too many Dependencies selected' if matched_ids.size > max_rows_limit
  end

  matched_ids
end

.orphanedObject



50
51
52
53
# File 'app/models/packages/dependency.rb', line 50

def self.orphaned
  subquery = Packages::DependencyLink.where(Packages::DependencyLink.arel_table[:dependency_id].eq(Packages::Dependency.arel_table[:id]))
  where_not_exists(subquery)
end

.pluck_ids_and_namesObject



46
47
48
# File 'app/models/packages/dependency.rb', line 46

def self.pluck_ids_and_names
  pluck(:id, :name)
end

Instance Method Details

#orphaned?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/packages/dependency.rb', line 55

def orphaned?
  self.dependency_links.empty?
end