Add unique key so only one agent config can be created per agent

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

MR: Pending

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:

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:

  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
  2. 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.
  3. 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.
  2. Convert the index index_remote_development_agent_configs_on_cluster_agent_id into a UNIQUE index.
  3. 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.

Edited by 🤖 GitLab Bot 🤖