summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180604123514_cleanup_stages_position_migration.rb
blob: 3adff21855263cf910a6e935a48f198b65ac69d8 (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
class CleanupStagesPositionMigration < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  TMP_INDEX_NAME = 'tmp_id_stage_position_partial_null_index'.freeze

  disable_ddl_transaction!

  class Stages < ActiveRecord::Base
    include EachBatch
    self.table_name = 'ci_stages'
  end

  def up
    disable_statement_timeout

    Gitlab::BackgroundMigration.steal('MigrateStageIndex')

    unless index_exists_by_name?(:ci_stages, TMP_INDEX_NAME)
      add_concurrent_index(:ci_stages, :id, where: 'position IS NULL', name: TMP_INDEX_NAME)
    end

    Stages.where('position is NULL').each_batch(of: 500) do |batch|
      range = batch.pluck('MIN(id)', 'MAX(id)').first

      Gitlab::BackgroundMigration::MigrateStageIndex.new.perform(*range)
    end

    remove_concurrent_index_by_name(:ci_stages, TMP_INDEX_NAME)
  end

  def down
    if index_exists_by_name?(:ci_stages, TMP_INDEX_NAME)
      remove_concurrent_index_by_name(:ci_stages, TMP_INDEX_NAME)
    end
  end
end