Add unique key so only one agent config can be created per agent
<!--IssueSummary start--> <details> <summary> Everyone can contribute. [Help move this issue forward](https://handbook.gitlab.com/handbook/marketing/developer-relations/contributor-success/community-contributors-workflows/#contributor-links) while earning points, leveling up and collecting rewards. </summary> - [Work on this issue](https://contributors.gitlab.com/manage-issue?action=work&projectId=278964&issueIid=435635) - [Close this issue](https://contributors.gitlab.com/manage-issue?action=close&projectId=278964&issueIid=435635) </details> <!--IssueSummary end--> MR: Pending <!-- The first line of the MR must be one of the following: 1. `MR: Pending` 2. `MR: <MR link with trailing +>`, and the first description line of the MR should be `Issue: <Issue link with trailing +>` 3. `MR: No MR` For more context, see: https://about.gitlab.com/handbook/engineering/development/dev/create/ide/index.html#1-to-1-relationship-of-issues-to-mrs --> <!-- The following sections should be filled out as part of the refinement process before the issue is prioritized. For more context, see: https://about.gitlab.com/handbook/engineering/development/dev/create/ide/#2-pre-iteration-planning-meeting --> ## Description Prevent the possibility of duplicate `remote_development_agent_configs` records being associated to a single `cluster_agents` record in the database. See related internal slack threads: - https://gitlab.slack.com/archives/C015U1TKV4M/p1702072451390339 - https://gitlab.slack.com/archives/C03KE0L9NC9/p1702405687911589 ## Acceptance Criteria TODO: Fill out (required) - [ ] Identify proper fix in database indexes - [ ] Implement necessary migrations ## Technical Requirements Comments from @a_akgun in [internal Slack thread](https://gitlab.slack.com/archives/C015U1TKV4M/p1702408115564099?thread_ts=1702072451.390339&cid=C015U1TKV4M): > 1. In ActiveRecord model the RemoteDevelopmentConfig has a belongs_to : agent, class_name: 'Clusters::Agent', foreign_key: 'cluster_agent_id', inverse_of: :remote_development_agent_config > 1. So ActiveRecord assumes one agent can have multiple configs; we rely on find_or_initialize_by here https://gitlab.com/gitlab-org/gitlab/-/blob/master/ee/lib/remote_development/agent_config/updater.rb#L31 ; but since it's a ruby new and save later, there might be a situation where the same agent config is updated created concurrently twice. > 1. Between ClusterAgent and RemoteDevelopmentConfig there is a one-to-many relationship in the database > > Solution: a one-to-one relationship by making agent_id unique for remote_development_agent_configs > > 1. In database de-duplicate existing configs. > 1. Convert the index index_remote_development_agent_configs_on_cluster_agent_id into a UNIQUE index. > 1. Additionally, in rails validate that agent id is unique. > > I think this must be fixed while we have few data as network/update operations. can create a lot of such duplicate records. ## Design Requirements No design requirements or UI impact. ## Impact Assessment This should have no UI impact, as we now de-duplicate agents shown in the workspace create UI. But it should be a priority to preserve data integrity and prevent duplicate records we have to clean up in the future. <!-- Replace with other type, e.g. bug or maintenance, if appropriate --> <!-- Replace with other subtype if appropriate --> <!-- By default, all issues start in the unprioritized status. See https://about.gitlab.com/handbook/engineering/development/dev/create/ide/#-remote-development-planning-process --> <!-- For simplicity and to avoid triage bot warnings about missing workflow labels, we will default to issues starting at the refinement phase -->
issue