2017-11-23 12:56:51 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# rubocop:disable Metrics/LineLength
|
|
|
|
# rubocop:disable Style/Documentation
|
|
|
|
|
2017-09-29 17:02:02 +00:00
|
|
|
module Gitlab
|
|
|
|
module BackgroundMigration
|
|
|
|
class CreateForkNetworkMembershipsRange
|
|
|
|
RESCHEDULE_DELAY = 15
|
|
|
|
|
|
|
|
class ForkedProjectLink < ActiveRecord::Base
|
|
|
|
self.table_name = 'forked_project_links'
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(start_id, end_id)
|
|
|
|
log("Creating memberships for forks: #{start_id} - #{end_id}")
|
|
|
|
|
2018-02-08 08:11:00 +00:00
|
|
|
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)
|
2017-09-29 17:02:02 +00:00
|
|
|
ActiveRecord::Base.connection.execute <<~INSERT_MEMBERS
|
|
|
|
INSERT INTO fork_network_members (fork_network_id, project_id, forked_from_project_id)
|
|
|
|
|
|
|
|
SELECT fork_network_members.fork_network_id,
|
|
|
|
forked_project_links.forked_to_project_id,
|
|
|
|
forked_project_links.forked_from_project_id
|
|
|
|
|
|
|
|
FROM forked_project_links
|
|
|
|
|
|
|
|
INNER JOIN fork_network_members
|
|
|
|
ON forked_project_links.forked_from_project_id = fork_network_members.project_id
|
|
|
|
|
|
|
|
WHERE forked_project_links.id BETWEEN #{start_id} AND #{end_id}
|
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM fork_network_members existing_members
|
|
|
|
WHERE existing_members.project_id = forked_project_links.forked_to_project_id
|
|
|
|
)
|
|
|
|
INSERT_MEMBERS
|
2018-02-08 08:11:00 +00:00
|
|
|
rescue ActiveRecord::RecordNotUnique => e
|
|
|
|
# `fork_network_member` was created concurrently in another migration
|
|
|
|
log(e.message)
|
2017-09-29 17:02:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def missing_members?(start_id, end_id)
|
|
|
|
count_sql = <<~MISSING_MEMBERS
|
|
|
|
SELECT COUNT(*)
|
|
|
|
|
|
|
|
FROM forked_project_links
|
|
|
|
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM fork_network_members
|
|
|
|
WHERE fork_network_members.project_id = forked_project_links.forked_to_project_id
|
|
|
|
)
|
2017-10-08 16:16:04 +00:00
|
|
|
AND EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM projects
|
|
|
|
WHERE forked_project_links.forked_from_project_id = projects.id
|
|
|
|
)
|
2017-11-14 09:33:20 +00:00
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM forked_project_links AS parent_links
|
|
|
|
WHERE parent_links.forked_to_project_id = forked_project_links.forked_from_project_id
|
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM projects
|
|
|
|
WHERE parent_links.forked_from_project_id = projects.id
|
|
|
|
)
|
|
|
|
)
|
2017-09-29 17:02:02 +00:00
|
|
|
AND forked_project_links.id BETWEEN #{start_id} AND #{end_id}
|
|
|
|
MISSING_MEMBERS
|
|
|
|
|
2017-11-14 09:33:20 +00:00
|
|
|
ForkedProjectLink.count_by_sql(count_sql) > 0
|
2017-09-29 17:02:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def log(message)
|
|
|
|
Rails.logger.info("#{self.class.name} - #{message}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|