Add merge_request_diff_id to approval_policy_merge_request_bypass_events

What does this MR do and why?

Adds a nullable merge_request_diff_id column to approval_policy_merge_request_bypass_events, so a bypass event records which merge request diff it was made against.

  • One migration adds the column, an index (index_approval_policy_mr_bypass_events_on_mr_diff_id), and a foreign key to merge_request_diffs with ON DELETE SET NULL. When an MR diff is deleted, we want service logic to handle deleting the bypass events tied to it.
  • Security::ApprovalPolicyMergeRequestBypassEvent now has belongs_to :merge_request_diff, optional: true, with a matching model spec.

Database notes:

  • Both tables are in gitlab_main_org, so a real foreign key is allowed.
  • approval_policy_merge_request_bypass_events is small. The new column is all NULL, so creating the index and validating the foreign key are cheap.
  • merge_request_diffs is high-traffic. add_concurrent_foreign_key takes a short SHARE ROW EXCLUSIVE lock on it, with lock retries.
  • The migration runs outside a transaction (disable_ddl_transaction!). add_column uses if_not_exists so a partial run can be retried. down removes the column, which drops the index and foreign key with it.

References

Screenshots or screen recordings

Not applicable. This MR has no UI changes.

How to set up and validate locally

This MR has no user-facing change. It only changes the schema and the model association.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Jans Rautenbach

Merge request reports

Loading
Loading