debian-mirror-gitlab/app/models/lfs_objects_project.rb

40 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class LfsObjectsProject < ApplicationRecord
2020-04-22 19:07:51 +05:30
include ::EachBatch
2015-11-26 14:37:03 +05:30
belongs_to :project
belongs_to :lfs_object
validates :lfs_object_id, presence: true
2019-09-04 21:01:54 +05:30
validates :lfs_object_id, uniqueness: { scope: [:project_id, :repository_type], message: "already exists in repository" }
2015-11-26 14:37:03 +05:30
validates :project_id, presence: true
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
after_commit :update_project_statistics, on: [:create, :destroy]
2017-08-17 22:00:37 +05:30
2019-09-04 21:01:54 +05:30
enum repository_type: {
project: 0,
wiki: 1,
2020-07-28 23:09:34 +05:30
design: 2
2019-09-04 21:01:54 +05:30
}
2020-03-13 15:44:24 +05:30
scope :project_id_in, ->(ids) { where(project_id: ids) }
2020-11-24 15:15:51 +05:30
scope :lfs_object_in, -> (lfs_objects) { where(lfs_object: lfs_objects) }
2020-03-13 15:44:24 +05:30
2022-01-26 12:08:38 +05:30
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
2017-08-17 22:00:37 +05:30
private
def update_project_statistics
2022-01-26 12:08:38 +05:30
self.class.update_statistics_for_project_id(project_id)
2017-08-17 22:00:37 +05:30
end
2015-11-26 14:37:03 +05:30
end