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_metricstable),target_project_id,title,target_branch(frommerge_requeststable) tomerge_requests_compliance_violations. We needtarget_branchbecause we plan to use it in &7879 (closed) - Don't make
target_project_idas a foreign key toprojectsas it's not required. - While creating a record for
merge_requests_compliance_violationspopulate the above columns by making changes in theexecutemethod of all the modules insideGitlab::ComplianceManagement::Violations - Whenever the
titleof a merge request is updated, update thetitleinmerge_requests_compliance_violationstable 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