Class: Gitlab::BackgroundMigration::ProjectNamespaces::BackfillProjectNamespaces

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb

Overview

Back-fill project namespaces for projects that do not yet have a namespace.

rubocop: disable Metrics/ClassLength

Constant Summary collapse

SUB_BATCH_SIZE =
25
PROJECT_NAMESPACE_STI_NAME =
'Project'
IsolatedModels =
::Gitlab::BackgroundMigration::ProjectNamespaces::Models

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#project_idsObject

Returns the value of attribute project_ids.



10
11
12
# File 'lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb', line 10

def project_ids
  @project_ids
end

#sub_batch_sizeObject

Returns the value of attribute sub_batch_size.



10
11
12
# File 'lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb', line 10

def sub_batch_size
  @sub_batch_size
end

Instance Method Details

#backfill_project_namespacesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb', line 33

def backfill_project_namespaces
  project_ids.each_slice(sub_batch_size) do |project_ids|
    # cleanup gin indexes on namespaces table
    cleanup_gin_index('namespaces')

    # cleanup gin indexes on projects table
    cleanup_gin_index('projects')

    # We need to lock these project records for the period when we create project namespaces
    # and link them to projects so that if a project is modified in the time between creating
    # project namespaces `batch_insert_namespaces` and linking them to projects `batch_update_projects`
    # we do not get them out of sync.
    #
    # see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/72527#note_730679469
    Project.transaction do
      Project.where(id: project_ids).select(:id).lock!('FOR UPDATE').load

      batch_insert_namespaces(project_ids)
      batch_update_projects(project_ids)
      batch_update_project_namespaces_traversal_ids(project_ids)
    end
  end
end

#cleanup_gin_index(table_name) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb', line 57

def cleanup_gin_index(table_name)
  index_names = ApplicationRecord.connection.select_values("select indexname::text from pg_indexes where tablename = '#{table_name}' and indexdef ilike '%using gin%'")

  index_names.each do |index_name|
    ApplicationRecord.connection.execute("select gin_clean_pending_list('#{index_name}')")
  end
end

#perform(start_id, end_id, migration_table_name, migration_column_name, sub_batch_size, pause_ms, namespace_id, migration_type = 'up') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb', line 17

def perform(start_id, end_id, migration_table_name, migration_column_name, sub_batch_size, pause_ms, namespace_id, migration_type = 'up')
  @sub_batch_size = sub_batch_size || SUB_BATCH_SIZE
  load_project_ids(start_id, end_id, namespace_id)

  case migration_type
  when 'up'
    backfill_project_namespaces
    mark_job_as_succeeded(start_id, end_id, namespace_id, 'up')
  when 'down'
    cleanup_backfilled_project_namespaces(namespace_id)
    mark_job_as_succeeded(start_id, end_id, namespace_id, 'down')
  else
    raise "Unknown migration type"
  end
end