Fix container_repositories index repair to handle 1-to-1 relationship

What does this MR do and why?

This backports !217242 (merged) to 18-6-stable-ee.

The container_repository_states table has a primary key constraint on container_repository_id, enforcing a one-to-one relationship with container_repositories. When duplicate container_repositories entries exist, previously the repair task attempted to update all container_repository_states references to point to the "good" ID, which violated the primary key constraint when a state entry already existed for that ID.

For example, suppose the container_repositories table:

id project_id name
1 1000 test
2 1000 test
3 1000 test

With this container_repository_states table:

container_repository_id verified_at
1 2025-12-01
2 2025-12-02
3 2025-12-03

The old approach tried to fix the duplication in container_repository_states by remapping the container_repository_id:

  UPDATE container_repository_states SET container_repository_id = 3
  WHERE container_repository_id IN (1)

However, this failed with:

  PG::UniqueViolation: duplicate key value violates unique constraint
  "container_repository_states_pkey"

The fix adds a new delete_orphaned option to reference configurations. When set to true, the repair task deletes all references pointing to the duplicate (bad) IDs before deleting the duplicate records. This approach:

  1. Deletes the container_repository_states rows for duplicate IDs.
  2. Deletes the duplicate container_repository rows.
  3. Preserves the state entry for the good ID.

This is simpler and more appropriate than using deduplication_column for one-to-one relationships where we don't need to preserve the orphaned references.

MR acceptance checklist

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

  • This MR is backporting a bug fix, documentation update, or spec fix, previously merged in the default branch.
  • The MR that fixed the bug on the default branch has been deployed to GitLab.com (not applicable for documentation or spec changes).
  • The MR title is descriptive (e.g. "Backport of 'title of default branch MR'"). This is important, since the title will be copied to the patch blog post.
  • Required labels have been applied to this merge request
  • This MR has been approved by a maintainer (only one approval is required).
  • Ensure the e2e:test-on-omnibus-ee job has succeeded, or if it has failed, investigate the failures. If you determine the failures are unrelated, you may proceed. If you need assistance investigating, reach out to a Software Engineer in Test in #s_developer_experience.

Note to the merge request author and maintainer

If you have questions about the patch release process, please:

Edited by Stan Hu

Merge request reports

Loading