summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170502101023_cleanup_namespaceless_pending_delete_projects.rb
blob: 76ed109898b9d1b70e98bf9383d424df355c5de9 (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
42
43
44
45
46
47
48
49
50
# This is the counterpart of RequeuePendingDeleteProjects and cleans all
# projects with `pending_delete = true` and that do not have a namespace.
class CleanupNamespacelessPendingDeleteProjects < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    admin = User.find_by(admin: true)
    return unless admin

    @offset = 0

    loop do
      ids = pending_delete_batch

      break if ids.rows.count.zero?

      args = ids.map { |id| [id['id'], admin.id, {}] }

      NamespacelessProjectDestroyWorker.bulk_perform_async(args)

      @offset += 1
    end
  end

  def down
    # noop
  end

  private

  def pending_delete_batch
    connection.exec_query(find_batch)
  end

  BATCH_SIZE = 5000

  def find_batch
    projects = Arel::Table.new(:projects)
    projects.project(projects[:id]).
      where(projects[:pending_delete].eq(true)).
      where(projects[:namespace_id].eq(nil)).
      skip(@offset * BATCH_SIZE).
      take(BATCH_SIZE).
      to_sql
  end
end