summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200204113223_schedule_recalculate_project_authorizations.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-13 21:08:59 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-13 21:08:59 +0000
commitd466ee5042520ad078fe050cb078d81dc2ebe196 (patch)
tree5648eb1aee8aeff5b5c5ff4669a184fd7676f778 /db/post_migrate/20200204113223_schedule_recalculate_project_authorizations.rb
parent6a9d7c009e4e5975a89bcc3e458da4b3ec484bd1 (diff)
downloadgitlab-ce-d466ee5042520ad078fe050cb078d81dc2ebe196.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'db/post_migrate/20200204113223_schedule_recalculate_project_authorizations.rb')
-rw-r--r--db/post_migrate/20200204113223_schedule_recalculate_project_authorizations.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/db/post_migrate/20200204113223_schedule_recalculate_project_authorizations.rb b/db/post_migrate/20200204113223_schedule_recalculate_project_authorizations.rb
new file mode 100644
index 00000000000..83b58300115
--- /dev/null
+++ b/db/post_migrate/20200204113223_schedule_recalculate_project_authorizations.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+class ScheduleRecalculateProjectAuthorizations < ActiveRecord::Migration[5.1]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ MIGRATION = 'RecalculateProjectAuthorizations'
+ BATCH_SIZE = 2_500
+ DELAY_INTERVAL = 2.minutes.to_i
+
+ disable_ddl_transaction!
+
+ class Namespace < ActiveRecord::Base
+ include ::EachBatch
+
+ self.table_name = 'namespaces'
+ end
+
+ class ProjectAuthorization < ActiveRecord::Base
+ include ::EachBatch
+
+ self.table_name = 'project_authorizations'
+ end
+
+ def up
+ say "Scheduling #{MIGRATION} jobs"
+
+ max_group_id = Namespace.where(type: 'Group').maximum(:id)
+ project_authorizations = ProjectAuthorization.where('project_id <= ?', max_group_id)
+ .select(:user_id)
+ .distinct
+
+ project_authorizations.each_batch(of: BATCH_SIZE, column: :user_id) do |authorizations, index|
+ delay = index * DELAY_INTERVAL
+ user_ids = authorizations.map(&:user_id)
+ BackgroundMigrationWorker.perform_in(delay, MIGRATION, [user_ids])
+ end
+ end
+
+ def down
+ end
+end