summaryrefslogtreecommitdiff
path: root/db/post_migrate/20211209103048_backfill_project_namespaces_for_group.rb
blob: 44dffc798d3817cc2f111bd6d72740a2b8389323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

class BackfillProjectNamespacesForGroup < Gitlab::Database::Migration[1.0]
  MIGRATION = 'ProjectNamespaces::BackfillProjectNamespaces'
  DELAY_INTERVAL = 2.minutes
  GROUP_ID = 9970 # picking gitlab-org group.

  disable_ddl_transaction!

  def up
    return unless Gitlab.com? || Gitlab.staging?

    projects_table = ::Gitlab::BackgroundMigration::ProjectNamespaces::Models::Project.arel_table
    hierarchy_cte_sql = Arel.sql(::Gitlab::BackgroundMigration::ProjectNamespaces::BackfillProjectNamespaces.hierarchy_cte(GROUP_ID))
    group_projects = ::Gitlab::BackgroundMigration::ProjectNamespaces::Models::Project.where(projects_table[:namespace_id].in(hierarchy_cte_sql))

    min_id = group_projects.minimum(:id)
    max_id = group_projects.maximum(:id)

    return if min_id.blank? || max_id.blank?

    queue_batched_background_migration(
      MIGRATION,
      :projects,
      :id,
      GROUP_ID,
      'up',
      job_interval: DELAY_INTERVAL,
      batch_min_value: min_id,
      batch_max_value: max_id,
      sub_batch_size: 25,
      batch_class_name: 'BackfillProjectNamespacePerGroupBatchingStrategy'
    )
  end

  def down
    return unless Gitlab.com? || Gitlab.staging?

    delete_batched_background_migration(MIGRATION, :projects, :id, [GROUP_ID, 'up'])
  end
end