提交 4c4109e1 编写于 作者: B Bob Van Landuyt

Create fork networks for forks for which the source was deleted.

That way we can join forks-of-forks into the same network even if
their original source was deleted.

Consider the following:

  `awesome-oss/badger` is forked to `coolstuff/badger`, which is
  forked to `user-a/badger` which in turn is forked to
  `user-b/badger`.

  When `awesome-oss/badger` is deleted, we will now create a fork
  network with `coolstuff/badger` as the root. The `user-a/badger`
  and `user-b/badger` projects will be added to the network.
上级 a2fea928
---
title: Create a fork network for forks with a deleted source
merge_request: 15595
author:
type: fixed
# frozen_string_literal: true # frozen_string_literal: true
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/LineLength
# rubocop:disable Style/Documentation
module Gitlab module Gitlab
module BackgroundMigration module BackgroundMigration
# This background migration is going to create all `fork_networks` and
# the `fork_network_members` for the roots of fork networks based on the
# existing `forked_project_links`.
#
# When the source of a fork is deleted, we will create the fork with the
# target project as the root. This way, when there are forks of the target
# project, they will be joined into the same fork network.
#
# When the `fork_networks` and memberships for the root projects are created
# the `CreateForkNetworkMembershipsRange` migration is scheduled. This
# migration will create the memberships for all remaining forks-of-forks
class PopulateForkNetworksRange class PopulateForkNetworksRange
def perform(start_id, end_id) def perform(start_id, end_id)
log("Creating fork networks for forked project links: #{start_id} - #{end_id}") create_fork_networks_for_existing_projects(start_id, end_id)
create_fork_networks_for_missing_projects(start_id, end_id)
create_fork_networks_memberships_for_root_projects(start_id, end_id)
delay = BackgroundMigration::CreateForkNetworkMembershipsRange::RESCHEDULE_DELAY # rubocop:disable Metrics/LineLength
BackgroundMigrationWorker.perform_in(
delay, "CreateForkNetworkMembershipsRange", [start_id, end_id]
)
end
def create_fork_networks_for_existing_projects(start_id, end_id)
log("Creating fork networks: #{start_id} - #{end_id}")
ActiveRecord::Base.connection.execute <<~INSERT_NETWORKS ActiveRecord::Base.connection.execute <<~INSERT_NETWORKS
INSERT INTO fork_networks (root_project_id) INSERT INTO fork_networks (root_project_id)
SELECT DISTINCT forked_project_links.forked_from_project_id SELECT DISTINCT forked_project_links.forked_from_project_id
FROM forked_project_links FROM forked_project_links
-- Exclude the forks that are not the first level fork of a project
WHERE NOT EXISTS ( WHERE NOT EXISTS (
SELECT true SELECT true
FROM forked_project_links inner_links FROM forked_project_links inner_links
WHERE inner_links.forked_to_project_id = forked_project_links.forked_from_project_id WHERE inner_links.forked_to_project_id = forked_project_links.forked_from_project_id
) )
/* Exclude the ones that are already created, in case the fork network
was already created for another fork of the project.
*/
AND NOT EXISTS ( AND NOT EXISTS (
SELECT true SELECT true
FROM fork_networks FROM fork_networks
WHERE forked_project_links.forked_from_project_id = fork_networks.root_project_id WHERE forked_project_links.forked_from_project_id = fork_networks.root_project_id
) )
-- Only create a fork network for a root project that still exists
AND EXISTS ( AND EXISTS (
SELECT true SELECT true
FROM projects FROM projects
...@@ -32,7 +57,45 @@ module Gitlab ...@@ -32,7 +57,45 @@ module Gitlab
) )
AND forked_project_links.id BETWEEN #{start_id} AND #{end_id} AND forked_project_links.id BETWEEN #{start_id} AND #{end_id}
INSERT_NETWORKS INSERT_NETWORKS
end
def create_fork_networks_for_missing_projects(start_id, end_id)
log("Creating fork networks with missing root: #{start_id} - #{end_id}")
ActiveRecord::Base.connection.execute <<~INSERT_NETWORKS
INSERT INTO fork_networks (root_project_id)
SELECT DISTINCT forked_project_links.forked_to_project_id
FROM forked_project_links
-- Exclude forks that are not the root forks
WHERE NOT EXISTS (
SELECT true
FROM forked_project_links inner_links
WHERE inner_links.forked_to_project_id = forked_project_links.forked_from_project_id
)
/* Exclude the ones that are already created, in case this migration is
re-run
*/
AND NOT EXISTS (
SELECT true
FROM fork_networks
WHERE forked_project_links.forked_to_project_id = fork_networks.root_project_id
)
/* Exclude projects for which the project still exists, those are
Processed in the previous step of this migration
*/
AND NOT EXISTS (
SELECT true
FROM projects
WHERE projects.id = forked_project_links.forked_from_project_id
)
AND forked_project_links.id BETWEEN #{start_id} AND #{end_id}
INSERT_NETWORKS
end
def create_fork_networks_memberships_for_root_projects(start_id, end_id)
log("Creating memberships for root projects: #{start_id} - #{end_id}") log("Creating memberships for root projects: #{start_id} - #{end_id}")
ActiveRecord::Base.connection.execute <<~INSERT_ROOT ActiveRecord::Base.connection.execute <<~INSERT_ROOT
...@@ -41,8 +104,12 @@ module Gitlab ...@@ -41,8 +104,12 @@ module Gitlab
FROM fork_networks FROM fork_networks
/* Joining both on forked_from- and forked_to- so we could create the
memberships for forks for which the source was deleted
*/
INNER JOIN forked_project_links INNER JOIN forked_project_links
ON forked_project_links.forked_from_project_id = fork_networks.root_project_id ON forked_project_links.forked_from_project_id = fork_networks.root_project_id
OR forked_project_links.forked_to_project_id = fork_networks.root_project_id
WHERE NOT EXISTS ( WHERE NOT EXISTS (
SELECT true SELECT true
...@@ -51,9 +118,6 @@ module Gitlab ...@@ -51,9 +118,6 @@ module Gitlab
) )
AND forked_project_links.id BETWEEN #{start_id} AND #{end_id} AND forked_project_links.id BETWEEN #{start_id} AND #{end_id}
INSERT_ROOT INSERT_ROOT
delay = BackgroundMigration::CreateForkNetworkMembershipsRange::RESCHEDULE_DELAY
BackgroundMigrationWorker.perform_in(delay, "CreateForkNetworkMembershipsRange", [start_id, end_id])
end end
def log(message) def log(message)
......
...@@ -62,12 +62,15 @@ describe Gitlab::BackgroundMigration::PopulateForkNetworksRange, :migration, sch ...@@ -62,12 +62,15 @@ describe Gitlab::BackgroundMigration::PopulateForkNetworksRange, :migration, sch
expect(base2_membership).not_to be_nil expect(base2_membership).not_to be_nil
end end
it 'skips links that had their source project deleted' do it 'creates a fork network for the fork of which the source was deleted' do
forked_project_links.create(id: 6, forked_from_project_id: 99999, forked_to_project_id: create(:project).id) fork = create(:project)
forked_project_links.create(id: 6, forked_from_project_id: 99999, forked_to_project_id: fork.id)
migration.perform(5, 8) migration.perform(5, 8)
expect(fork_networks.find_by(root_project_id: 99999)).to be_nil expect(fork_networks.find_by(root_project_id: 99999)).to be_nil
expect(fork_networks.find_by(root_project_id: fork.id)).not_to be_nil
expect(fork_network_members.find_by(project_id: fork.id)).not_to be_nil
end end
it 'schedules a job for inserting memberships for forks-of-forks' do it 'schedules a job for inserting memberships for forks-of-forks' do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册