Remove subtransactions in UserInteractedProject
UserInteractedProject
creates a subtransaction here https://gitlab.com/gitlab-org/gitlab/blob/b003363ed283e805e1871f5ba1961565bbd63a7c/app/models/user_interacted_project.rb#L15-37:
def track(event)
# For events without a project, we simply don't care.
# An example of this is the creation of a snippet (which
# is not related to any project).
return unless event.project_id
attributes = {
project_id: event.project_id,
user_id: event.author_id
}
cached_exists?(**attributes) do
transaction(requires_new: true) do
where(attributes).select(1).first || create!(attributes)
true # not caching the whole record here for now
rescue ActiveRecord::RecordNotUnique
# Note, above queries are not atomic and prone
# to race conditions (similar like #find_or_create!).
# In the case where we hit this, the record we want
# already exists - shortcut and return.
true
end
end
The most common case here would be an existing record because once a user has interacted with a project that record will always exist.
I think we can avoid the subtransaction by doing ON CONFLICT DO NOTHING
instead of starting a new savepoint and rescuing the error