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

58 lines
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 MergeRequestDiffCommit < ApplicationRecord
2021-03-11 19:13:27 +05:30
extend SuppressCompositePrimaryKeyWarning
2020-03-13 15:44:24 +05:30
include BulkInsertSafe
2017-09-10 17:25:29 +05:30
include ShaAttribute
2020-03-13 15:44:24 +05:30
include CachedCommit
2017-09-10 17:25:29 +05:30
belongs_to :merge_request_diff
sha_attribute :sha
alias_attribute :id, :sha
2021-09-04 01:27:46 +05:30
serialize :trailers, Serializers::Json # rubocop:disable Cop/ActiveRecordSerialize
2021-03-11 19:13:27 +05:30
validates :trailers, json_schema: { filename: 'git_trailers' }
2020-04-08 14:13:33 +05:30
# Deprecated; use `bulk_insert!` from `BulkInsertSafe` mixin instead.
# cf. https://gitlab.com/gitlab-org/gitlab/issues/207989 for progress
2017-09-10 17:25:29 +05:30
def self.create_bulk(merge_request_diff_id, commits)
rows = commits.map.with_index do |commit, index|
# See #parent_ids.
commit_hash = commit.to_hash.except(:parent_ids)
sha = commit_hash.delete(:id)
commit_hash.merge(
merge_request_diff_id: merge_request_diff_id,
relative_order: index,
2020-03-13 15:44:24 +05:30
sha: Gitlab::Database::ShaAttribute.serialize(sha), # rubocop:disable Cop/ActiveRecordSerialize
2018-03-17 18:26:18 +05:30
authored_date: Gitlab::Database.sanitize_timestamp(commit_hash[:authored_date]),
2021-03-11 19:13:27 +05:30
committed_date: Gitlab::Database.sanitize_timestamp(commit_hash[:committed_date]),
trailers: commit_hash.fetch(:trailers, {}).to_json
2017-09-10 17:25:29 +05:30
)
end
2020-06-23 00:09:42 +05:30
Gitlab::Database.bulk_insert(self.table_name, rows) # rubocop:disable Gitlab/BulkInsert
2017-09-10 17:25:29 +05:30
end
2021-03-11 19:13:27 +05:30
def self.oldest_merge_request_id_per_commit(project_id, shas)
# This method is defined here and not on MergeRequest, otherwise the SHA
# values used in the WHERE below won't be encoded correctly.
select(['merge_request_diff_commits.sha AS sha', 'min(merge_requests.id) AS merge_request_id'])
.joins(:merge_request_diff)
.joins(
'INNER JOIN merge_requests ' \
'ON merge_requests.latest_merge_request_diff_id = merge_request_diffs.id'
)
.where(sha: shas)
.where(
merge_requests: {
target_project_id: project_id,
state_id: MergeRequest.available_states[:merged]
}
)
.group(:sha)
end
2017-09-10 17:25:29 +05:30
end