2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class LfsObjectsProject < ApplicationRecord
|
2020-03-23 08:09:47 -04:00
|
|
|
include ::EachBatch
|
|
|
|
|
2015-10-12 10:42:14 -04:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :lfs_object
|
|
|
|
|
|
|
|
validates :lfs_object_id, presence: true
|
2019-06-06 02:51:50 -04:00
|
|
|
validates :lfs_object_id, uniqueness: { scope: [:project_id, :repository_type], message: "already exists in repository" }
|
2015-10-12 10:42:14 -04:00
|
|
|
validates :project_id, presence: true
|
2016-11-22 11:58:10 -05:00
|
|
|
|
2017-06-01 17:36:04 -04:00
|
|
|
after_commit :update_project_statistics, on: [:create, :destroy]
|
2016-11-22 11:58:10 -05:00
|
|
|
|
2019-06-06 02:51:50 -04:00
|
|
|
enum repository_type: {
|
|
|
|
project: 0,
|
|
|
|
wiki: 1,
|
2020-07-16 08:09:22 -04:00
|
|
|
design: 2
|
2019-06-06 02:51:50 -04:00
|
|
|
}
|
|
|
|
|
2020-02-13 13:09:00 -05:00
|
|
|
scope :project_id_in, ->(ids) { where(project_id: ids) }
|
2020-09-07 08:08:27 -04:00
|
|
|
scope :lfs_object_in, -> (lfs_objects) { where(lfs_object: lfs_objects) }
|
2020-02-13 13:09:00 -05:00
|
|
|
|
2021-12-09 19:13:05 -05:00
|
|
|
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
|
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def update_project_statistics
|
2021-12-09 19:13:05 -05:00
|
|
|
self.class.update_statistics_for_project_id(project_id)
|
2016-11-22 11:58:10 -05:00
|
|
|
end
|
2015-10-12 10:42:14 -04:00
|
|
|
end
|