Skip to content

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

  1. Add columns merged_at (from merge_request_metrics table), target_project_id, title, target_branch (from merge_requests table) to merge_requests_compliance_violations. We need target_branch because we plan to use it in &7879 (closed)
  2. Don't make target_project_idas a foreign key to projects as it's not required.
  3. While creating a record for merge_requests_compliance_violations populate the above columns by making changes in the execute method of all the modules inside Gitlab::ComplianceManagement::Violations
  4. Whenever the title of a merge request is updated, update the title in merge_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