File: lfs_objects_project.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (50 lines) | stat: -rw-r--r-- 1,612 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

class LfsObjectsProject < ApplicationRecord
  include ::EachBatch

  belongs_to :project
  belongs_to :lfs_object

  before_validation :ensure_uniqueness
  validates :lfs_object_id, presence: true
  validates :lfs_object_id, uniqueness: { scope: [:project_id, :repository_type], message: "already exists in repository" }
  validates :project_id, presence: true

  after_commit :update_project_statistics, on: [:create, :destroy]

  enum repository_type: {
    project: 0,
    wiki: 1,
    design: 2
  }

  scope :project_id_in, ->(ids) { where(project_id: ids) }
  scope :lfs_object_in, ->(lfs_objects) { where(lfs_object: lfs_objects) }

  def self.link_to_project!(lfs_object, project)
    # We can't use an upsert here because there is no uniqueness constraint:
    # https://gitlab.com/gitlab-org/gitlab/-/issues/347466
    self.safe_find_or_create_by!(lfs_object_id: lfs_object.id, project_id: project.id) # rubocop:disable Performance/ActiveRecordSubtransactionMethods
  end

  def self.update_statistics_for_project_id(project_id)
    ProjectCacheWorker.perform_async(project_id, [], [:lfs_objects_size]) # rubocop:disable CodeReuse/Worker
  end

  private

  def ensure_uniqueness
    return if project_id.nil? || lfs_object_id.nil?

    lock_key = [project_id, lfs_object_id, repository_type.presence || 'null'].join('-')

    lock_expression = "hashtext(#{connection.quote(lock_key)})"

    connection.execute("SELECT pg_advisory_xact_lock(#{lock_expression})")
  end

  def update_project_statistics
    self.class.update_statistics_for_project_id(project_id)
  end
end