Class: MergeRequestDiffCommit
Constant Summary
collapse
- CouldNotCreateMetadataError =
Class.new(StandardError)
- TRIM_USER_KEYS =
A list of keys of which their values need to be trimmed before they can be inserted into the merge_request_diff_commit_users table.
i[author_name author_email committer_name committer_email].freeze
BulkInsertSafe::ALLOWED_CALLBACKS, BulkInsertSafe::DEFAULT_BATCH_SIZE, BulkInsertSafe::MethodNotAllowedError, BulkInsertSafe::PrimaryKeySetError
ApplicationRecord::MAX_PLUCK
HasCheckConstraints::NOT_NULL_CHECK_PATTERN
ResetOnColumnErrors::MAX_RESET_PERIOD
Class Method Summary
collapse
Instance Method Summary
collapse
#extended_trailers, #parent_ids, #referenced_by
===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, 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
#sharding_organization
#reset_on_union_error, #reset_on_unknown_attribute_error
#serializable_hash
Class Method Details
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'app/models/merge_request_diff_commit.rb', line 160
def self.commit_rows_with_metadata(project_id, merge_request_diff_id, rows)
commits_metadata_mapping = MergeRequest::CommitsMetadata.bulk_find_or_create(
project_id,
rows
)
rows.each do |row|
row[:merge_request_commits_metadata_id] = commits_metadata_mapping[row[:raw_sha]]
row.delete(:raw_sha)
end
rows_without_metadata = rows.select { |row| row[:merge_request_commits_metadata_id].nil? }
if rows_without_metadata.any?
Gitlab::ErrorTracking.track_exception(
CouldNotCreateMetadataError.new,
message: 'Failed to create metadata',
failed_count: rows_without_metadata.size,
total_count: rows.size,
merge_request_diff_id: merge_request_diff_id,
project_id: project_id,
relative_orders: rows_without_metadata.filter_map { |r| r[:relative_order] }
)
end
rows
end
|
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'app/models/merge_request_diff_commit.rb', line 139
def self.commit_shas_from_metadata(project_id:, limit:)
metadata_join_sql = " LEFT JOIN merge_request_commits_metadata\n ON merge_request_commits_metadata.id = merge_request_diff_commits.merge_request_commits_metadata_id\n AND merge_request_commits_metadata.project_id = ?\n SQL\n\n # raw SQL in pluck() bypass ActiveRecord's type casting, so encode() is needed to convert bytea to hex\n shas_sql = Arel.sql(\"encode(COALESCE(merge_request_commits_metadata.sha, merge_request_diff_commits.sha), 'hex')\")\n\n relation = self.joins(self.sanitize_sql_array([metadata_join_sql, project_id]))\n .order(:relative_order)\n\n relation = relation.limit(limit) if limit\n\n # rubocop:disable Database/AvoidUsingPluckWithoutLimit -- limit may be applied in the caller\n relation.pluck(shas_sql)\n # rubocop:enable Database/AvoidUsingPluckWithoutLimit\nend\n".squish
|
.create_bulk(merge_request_diff_id, commits, project, skip_commit_data: false) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'app/models/merge_request_diff_commit.rb', line 51
def self.create_bulk(merge_request_diff_id, commits, project, skip_commit_data: false)
organization_id = project.organization_id
dedup_enabled = Feature.enabled?(:merge_request_diff_commits_dedup, project)
partition_enabled = Feature.enabled?(:merge_request_diff_commits_partition, project)
commit_hashes, user_triples = prepare_commits_for_bulk_insert(commits, organization_id)
users = MergeRequest::DiffCommitUser.bulk_find_or_create(user_triples)
rows = commit_hashes.map.with_index do |commit_hash, index|
raw_sha = commit_hash.delete(:id)
trailers = commit_hash.fetch(:trailers, {})
author = users[[commit_hash[:author_name], commit_hash[:author_email], organization_id]]
committer = users[[commit_hash[:committer_name], commit_hash[:committer_email], organization_id]]
commit_hash = commit_hash
.except(:author_name, :author_email, :committer_name, :committer_email, :extended_trailers)
commit_hash = commit_hash.merge(
commit_author_id: author.id,
committer_id: committer.id,
merge_request_diff_id: merge_request_diff_id,
relative_order: index,
sha: Gitlab::Database::ShaAttribute.serialize(raw_sha),
authored_date: Gitlab::Database.sanitize_timestamp(commit_hash[:authored_date]),
committed_date: Gitlab::Database.sanitize_timestamp(commit_hash[:committed_date]),
trailers: Gitlab::Json.dump(trailers)
)
commit_hash[:raw_sha] = raw_sha if dedup_enabled
commit_hash[:project_id] = project.id if partition_enabled
commit_hash = commit_hash.merge(message: '') if skip_commit_data
commit_hash
end
rows = commit_rows_with_metadata(project.id, merge_request_diff_id, rows) if dedup_enabled
ApplicationRecord.legacy_bulk_insert(self.table_name, rows)
end
|
.oldest_merge_request_id_per_commit(project_id, shas) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'app/models/merge_request_diff_commit.rb', line 120
def self.oldest_merge_request_id_per_commit(project_id, shas)
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
|
.prepare_commits_for_bulk_insert(commits, organization_id) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'app/models/merge_request_diff_commit.rb', line 102
def self.prepare_commits_for_bulk_insert(commits, organization_id)
user_triples = Set.new
hashes = commits.map do |commit|
hash = commit.to_hash.except(:parent_ids, :referenced_by)
TRIM_USER_KEYS.each do |key|
hash[key] = MergeRequest::DiffCommitUser.prepare(hash[key])
end
user_triples << [hash[:author_name], hash[:author_email], organization_id]
user_triples << [hash[:committer_name], hash[:committer_email], organization_id]
hash
end
[hashes, user_triples]
end
|
Instance Method Details
#author_email ⇒ Object
195
196
197
|
# File 'app/models/merge_request_diff_commit.rb', line 195
def author_email
commit_author&.email
end
|
#author_name ⇒ Object
191
192
193
|
# File 'app/models/merge_request_diff_commit.rb', line 191
def author_name
commit_author&.name
end
|
#authored_date ⇒ Object
222
223
224
|
# File 'app/models/merge_request_diff_commit.rb', line 222
def authored_date
has_commit_metadata? ? merge_request_commits_metadata.authored_date : super
end
|
#commit_author ⇒ Object
234
235
236
|
# File 'app/models/merge_request_diff_commit.rb', line 234
def commit_author
has_commit_metadata? ? merge_request_commits_metadata.commit_author : super
end
|
#committed_date ⇒ Object
226
227
228
|
# File 'app/models/merge_request_diff_commit.rb', line 226
def committed_date
has_commit_metadata? ? merge_request_commits_metadata.committed_date : super
end
|
#committer ⇒ Object
238
239
240
|
# File 'app/models/merge_request_diff_commit.rb', line 238
def committer
has_commit_metadata? ? merge_request_commits_metadata.committer : super
end
|
#committer_email ⇒ Object
203
204
205
|
# File 'app/models/merge_request_diff_commit.rb', line 203
def committer_email
committer&.email
end
|
#committer_name ⇒ Object
199
200
201
|
# File 'app/models/merge_request_diff_commit.rb', line 199
def committer_name
committer&.name
end
|
#message ⇒ Object
207
208
209
|
# File 'app/models/merge_request_diff_commit.rb', line 207
def message
fetch_message
end
|
#project_id ⇒ Object
218
219
220
|
# File 'app/models/merge_request_diff_commit.rb', line 218
def project_id
project.id
end
|
#sha ⇒ Object
230
231
232
|
# File 'app/models/merge_request_diff_commit.rb', line 230
def sha
has_commit_metadata? ? merge_request_commits_metadata.sha : super
end
|
#to_hash ⇒ Object
211
212
213
214
215
216
|
# File 'app/models/merge_request_diff_commit.rb', line 211
def to_hash
super(exclude_keys: [:message]).merge({
'id' => sha,
message: fetch_message
})
end
|