Commit 0478ff7c authored by Bob Van Landuyt's avatar Bob Van Landuyt

Handle uniqueness on fork_network_member creation

Since the migration might be queued already and be rescheduled when it
fails on a uniqueness error, this should help clearing the background
migration queue faster.
parent 7e00adcc
...@@ -14,6 +14,14 @@ module Gitlab ...@@ -14,6 +14,14 @@ module Gitlab
def perform(start_id, end_id) def perform(start_id, end_id)
log("Creating memberships for forks: #{start_id} - #{end_id}") log("Creating memberships for forks: #{start_id} - #{end_id}")
insert_members(start_id, end_id)
if missing_members?(start_id, end_id)
BackgroundMigrationWorker.perform_in(RESCHEDULE_DELAY, "CreateForkNetworkMembershipsRange", [start_id, end_id])
end
end
def insert_members(start_id, end_id)
ActiveRecord::Base.connection.execute <<~INSERT_MEMBERS ActiveRecord::Base.connection.execute <<~INSERT_MEMBERS
INSERT INTO fork_network_members (fork_network_id, project_id, forked_from_project_id) INSERT INTO fork_network_members (fork_network_id, project_id, forked_from_project_id)
...@@ -33,10 +41,9 @@ module Gitlab ...@@ -33,10 +41,9 @@ module Gitlab
WHERE existing_members.project_id = forked_project_links.forked_to_project_id WHERE existing_members.project_id = forked_project_links.forked_to_project_id
) )
INSERT_MEMBERS INSERT_MEMBERS
rescue ActiveRecord::RecordNotUnique => e
if missing_members?(start_id, end_id) # `fork_network_member` was created concurrently in another migration
BackgroundMigrationWorker.perform_in(RESCHEDULE_DELAY, "CreateForkNetworkMembershipsRange", [start_id, end_id]) log(e.message)
end
end end
def missing_members?(start_id, end_id) def missing_members?(start_id, end_id)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment