Skip to content

Draft: Schedule migration to deduplicate invalid MR approval rule names

What does this MR do and why?

Related to: #247119

In !88395 (closed) we fixed the validation logic for MR approval rule names. Previously the logic allowed the code to bypass scoped uniqueness validator for ApprovalMergeRequestRule#name. Now that is fixed we need to update the invalid records before adding a uniqueness constraint to the database.

This MR includes a Post Deployment Migration which will find all of the non-distinct names in batches of 100 and then schedule background migrations in 2 minute intervals (roughly 1275 invalid records on production).

To speed up the initial query I have also added an index on the columns used in the uniqueness scope. In a following release we will add a finalizing migration and then add a uniqueness constraint to the database index.

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

Ensure gdk is up to date and running. Ensure you have some MRs in your local gdk Run the following code to generate some duplicates.

(MergeRequest.count * 2).times do
  ApprovalMergeRequestRule.new(
    name: 'Approval Required',
    approvals_required: 1,
    merge_request_id: MergeRequest.order('random()').limit(1).take.id
  ).save(validate: false)
end

After running this code you can run bin/rails db:migrate to run the migration, then check the records in the db to ensure the duplicates have been updated.

Find all duplicates and group by name and aggregated array of ids. If there are no rows returned then there are no longer any duplicates.

    duplicate_groups = ApprovalMergeRequestRule.connection.execute(<<~SQL)
      SELECT
        name,
        array_agg(id) as ids
      FROM
        "approval_merge_request_rules"
      GROUP BY
        "approval_merge_request_rules"."name",
        "approval_merge_request_rules"."merge_request_id",
        "approval_merge_request_rules"."rule_type",
        "approval_merge_request_rules"."section"
      HAVING (COUNT(*) > 1)
      ORDER BY "ids"
    SQL

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Joe Woodward

Merge request reports