From c0c48ea7d1b5550de1873f77482769888ddc146c Mon Sep 17 00:00:00 2001
From: Reuben Pereira <rpereira@gitlab.com>
Date: Thu, 1 Apr 2021 19:56:58 +0530
Subject: [PATCH] Reschedule background migration to copy
 container_registry_enabled

The previous background migration missed 10 jobs, so this time we
will be tracking the jobs by passing in the track_jobs: true
parameter.
---
 .../18792-reschedule-background-migration.yml |  6 +++++
 ...er_registry_enabled_to_project_features.rb |  5 +++-
 ...r_registry_enabled_to_project_features2.rb | 27 +++++++++++++++++++
 db/schema_migrations/20210401131948           |  1 +
 ...ner_registry_enabled_to_project_feature.rb |  2 +-
 ...stry_enabled_to_project_features2_spec.rb} |  8 ++++--
 6 files changed, 45 insertions(+), 4 deletions(-)
 create mode 100644 changelogs/unreleased/18792-reschedule-background-migration.yml
 create mode 100644 db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb
 create mode 100644 db/schema_migrations/20210401131948
 rename spec/migrations/{move_container_registry_enabled_to_project_features_spec.rb => move_container_registry_enabled_to_project_features2_spec.rb} (75%)

diff --git a/changelogs/unreleased/18792-reschedule-background-migration.yml b/changelogs/unreleased/18792-reschedule-background-migration.yml
new file mode 100644
index 000000000000..044a362ffc1e
--- /dev/null
+++ b/changelogs/unreleased/18792-reschedule-background-migration.yml
@@ -0,0 +1,6 @@
+---
+title: Reschedule background migration to copy projects.container_registry_enabled
+  to project_features.container_registry_access_level
+merge_request: 58360
+author:
+type: added
diff --git a/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb b/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb
index 7bc7a0e49f7e..fce311108660 100644
--- a/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb
+++ b/db/post_migrate/20210226120851_move_container_registry_enabled_to_project_features.rb
@@ -16,7 +16,10 @@ class Project < ActiveRecord::Base
   end
 
   def up
-    queue_background_migration_jobs_by_range_at_intervals(Project, MIGRATION, 2.minutes, batch_size: BATCH_SIZE)
+    # no-op
+    # Superceded by db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb
+
+    # queue_background_migration_jobs_by_range_at_intervals(Project, MIGRATION, 2.minutes, batch_size: BATCH_SIZE)
   end
 
   def down
diff --git a/db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb b/db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb
new file mode 100644
index 000000000000..6e3f7ae34d14
--- /dev/null
+++ b/db/post_migrate/20210401131948_move_container_registry_enabled_to_project_features2.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+class MoveContainerRegistryEnabledToProjectFeatures2 < ActiveRecord::Migration[6.0]
+  include Gitlab::Database::MigrationHelpers
+
+  DOWNTIME = false
+
+  BATCH_SIZE = 21_000
+  MIGRATION = 'MoveContainerRegistryEnabledToProjectFeature'
+
+  disable_ddl_transaction!
+
+  class Project < ActiveRecord::Base
+    include EachBatch
+    self.table_name = 'projects'
+  end
+
+  def up
+    delete_queued_jobs('MoveContainerRegistryEnabledToProjectFeature')
+
+    queue_background_migration_jobs_by_range_at_intervals(Project, MIGRATION, 2.minutes, batch_size: BATCH_SIZE, track_jobs: true)
+  end
+
+  def down
+    # no-op
+  end
+end
diff --git a/db/schema_migrations/20210401131948 b/db/schema_migrations/20210401131948
new file mode 100644
index 000000000000..8b9950cd32c0
--- /dev/null
+++ b/db/schema_migrations/20210401131948
@@ -0,0 +1 @@
+cbc1cd66cdbe08ac9edee14da255343acdcd8adaea6748ee82980462ae4bb88f
\ No newline at end of file
diff --git a/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb b/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb
index 4eaef26c9c67..9ecf53317d0a 100644
--- a/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb
+++ b/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb
@@ -6,7 +6,7 @@ module BackgroundMigration
     # project_features.container_registry_access_level for the projects within
     # the given range of ids.
     class MoveContainerRegistryEnabledToProjectFeature
-      MAX_BATCH_SIZE = 1_000
+      MAX_BATCH_SIZE = 300
 
       module Migratable
         # Migration model namespace isolated from application code.
diff --git a/spec/migrations/move_container_registry_enabled_to_project_features_spec.rb b/spec/migrations/move_container_registry_enabled_to_project_features2_spec.rb
similarity index 75%
rename from spec/migrations/move_container_registry_enabled_to_project_features_spec.rb
rename to spec/migrations/move_container_registry_enabled_to_project_features2_spec.rb
index c7b07f3ef37b..11d43a36bc90 100644
--- a/spec/migrations/move_container_registry_enabled_to_project_features_spec.rb
+++ b/spec/migrations/move_container_registry_enabled_to_project_features2_spec.rb
@@ -1,9 +1,9 @@
 # frozen_string_literal: true
 
 require 'spec_helper'
-require Rails.root.join('db', 'post_migrate', '20210226120851_move_container_registry_enabled_to_project_features.rb')
+require Rails.root.join('db', 'post_migrate', '20210401131948_move_container_registry_enabled_to_project_features2.rb')
 
-RSpec.describe MoveContainerRegistryEnabledToProjectFeatures, :migration do
+RSpec.describe MoveContainerRegistryEnabledToProjectFeatures2, :migration do
   let(:namespace) { table(:namespaces).create!(name: 'gitlab', path: 'gitlab-org') }
 
   let!(:projects) do
@@ -30,6 +30,10 @@
   it 'schedules jobs for ranges of projects' do
     migrate!
 
+    # Since track_jobs is true, each job should have an entry in the background_migration_jobs
+    # table.
+    expect(table(:background_migration_jobs).count).to eq(2)
+
     expect(described_class::MIGRATION)
       .to be_scheduled_delayed_migration(2.minutes, projects[0].id, projects[2].id)
 
-- 
GitLab