Adds helper for `find_or_create_by` in transaction
What does this MR do?
This ports the helper used in https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/9398 to CE.
The helper can be used to avoid race conditions when creating records that have a unique constraint.