Race condition when creating approval rules

Currently the ApprovalRules::BaseService doesn't account for these races when calling ApprovalProjectRule#update for a new record.

I think we should handle those races by creating the relations manually


https://sentry.gitlab.net/gitlab/gitlabcom/issues/870584/

PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_approval_project_rules_users_1"
DETAIL:  Key (approval_project_rule_id, user_id)=(67846, 4076765) already exists.

  /opt/gitlab/embedded/service/gitlab-rails/config/initializers/peek.rb:18:in `async_exec_params'
    super(*args)
  /opt/gitlab/embedded/service/gitlab-rails/config/initializers/peek.rb:18:in `exec_params'
    super(*args)
  active_record/connection_adapters/postgresql_adapter.rb:611:in `block (2 levels) in exec_no_cache'
    @connection.exec_params(sql, type_casted_binds)
  active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
    yield
  active_support/concurrency/share_lock.rb:187:in `yield_shares'
    yield
...
(243 additional frame(s) were not displayed)

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_approval_project_rules_users_1"
DETAIL:  Key (approval_project_rule_id, user_id)=(67846, 4076765) already exists.
: INSERT INTO "approval_project_rules_users" ("approval_project_rule_id", "user_id") VALUES (67846, 4076765) RETURNING "id"

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_approval_project_rules_users_1"
DETAIL:  Key (approval_project_rule_id, user_id)=(67846, 4076765) already exists.
: INSERT INTO "approval_project_rules_users" ("approval_project_rule_id", "user_id") VALUES (67846, 4076765) RETURNING "id"
Assignee Loading
Time tracking Loading