Create denormalized columns in merge requests compliance violations table
Description
To reduce the overall complexity of database queries by eliminating multiple joins we want to go ahead with denormalization database and store redundant columns. This is a planned decision after trying several other options.
Implementation Plan
- Add columns
merged_at
(frommerge_request_metrics
table),target_project_id
,title
,target_branch
(frommerge_requests
table) tomerge_requests_compliance_violations
. We needtarget_branch
because we plan to use it in &7879 (closed) - Don't make
target_project_id
as a foreign key toprojects
as it's not required. - While creating a record for
merge_requests_compliance_violations
populate the above columns by making changes in theexecute
method of all the modules insideGitlab::ComplianceManagement::Violations
- Whenever the
title
of a merge request is updated, update thetitle
inmerge_requests_compliance_violations
table as well to prevent any data inconsistency.
# inside ee/app/models/ee/merge_request.rb
after_update :sync_merge_request_compliance_violation, if: :saved_change_to_title?
def sync_merge_request_compliance_violation
compliance_violations.update_all(title: title)
end
Edited by Huzaifa Iftikhar