Skip to content

WIP: Add a unique index on namespaces.owner_id

What does this MR do?

Prevents data inconsistencies by putting a unique index on the namespaces.owner_id column

Are there points in the code the reviewer needs to double check?

Perhaps this should be a foreign key instead?

Do we need a data migration to sort out any existing bad rows?

We can get a list of duplicated owner_ids with SQL like this:

SELECT owner_id, count(1)
FROM namespaces
WHERE owner_id IS NOT NULL
GROUP BY owner_id
HAVING count(owner_id) > 1

If GitLab.com is fine, we can assume everywhere else is. It's not immediately obvious what strategy we would take when resolving duplicates - oldest wins? Oldest loses?

Why was this MR needed?

A user has_one :namespace, foreign_key: :owner_id. Having multiple rows in the namespaces table with the same owner_id means that we get a random record back from the database when we follow that association. This breaks tests, and could be nasty in production too.

Screenshots (if relevant)

Does this MR meet the acceptance criteria?

What are the relevant issue numbers?

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/43292

Edited by Nick Thomas

Merge request reports