Members lacks database uniqueness constraint to match model
The Member
class specifies a uniqueness constraint on [:user_id, :source_type, :source_id]
. This constraint is not reflected in the database. We should add a database migration that adds a unique index so the model is in sync with the database constraint.
Per https://docs.gitlab.com/ee/development/migration_style_guide.html#adding-indexes, we will have to check for duplicates before adding the unique index to avoid bad data. Hopefully there is no bad data present given the model uniqueness constraint. It's always a possibility, though.
Follow-up from !26704 (merged)
@stanhu mentioned:
Hmm, I don't see anything uniqueness constraints in the members
table for (source_type, source_id, user_id
). Is it possible to get into a situation where duplicate entries are created for a member if multiple SCIM requests arrive in close proximity?
@dblessing replied:
You're right, there isn't a unique constraint in the database. But there is in the model - https://gitlab.com/gitlab-org/gitlab/-/blob/b46a063f56ac5fdcdd1218b1cdc8c914d49533fa/app/models/member.rb#L23. This should probably be addressed in a migration.