summaryrefslogtreecommitdiff
path: root/db/post_migrate/20171114104051_remove_empty_fork_networks.rb
blob: 2fe99a1b9c17a1886a71ff8ae687d8e570326718 (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
class RemoveEmptyForkNetworks < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  BATCH_SIZE = 10_000

  class MigrationForkNetwork < ActiveRecord::Base
    include EachBatch

    self.table_name = 'fork_networks'
  end

  class MigrationForkNetworkMembers < ActiveRecord::Base
    self.table_name = 'fork_network_members'
  end

  disable_ddl_transaction!

  def up
    say 'Deleting empty ForkNetworks in batches'

    has_members = MigrationForkNetworkMembers
                    .where('fork_network_members.fork_network_id = fork_networks.id')
                    .select(1)
    MigrationForkNetwork.where('NOT EXISTS (?)', has_members)
      .each_batch(of: BATCH_SIZE) do |networks|
      deleted = networks.delete_all

      say "Deleted #{deleted} rows in batch"
    end
  end

  def down
    # nothing
  end
end