20171121135738_clean_up_from_merge_request_diffs_and_commits.rb 1.08 KB
Newer Older
1
class CleanUpFromMergeRequestDiffsAndCommits < ActiveRecord::Migration[4.2]
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
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  class MergeRequestDiff < ActiveRecord::Base
    self.table_name = 'merge_request_diffs'

    include ::EachBatch
  end

  disable_ddl_transaction!

  def up
    Gitlab::BackgroundMigration.steal('DeserializeMergeRequestDiffsAndCommits')

    # The literal '--- []\n' value is created by the import process and treated
    # as null by the application, so we can ignore those - even if we were
    # migrating, it wouldn't create any rows.
    literal_prefix = Gitlab::Database.postgresql? ? 'E' : ''
    non_empty = "
      (st_commits IS NOT NULL AND st_commits != #{literal_prefix}'--- []\n')
      OR
      (st_diffs IS NOT NULL AND st_diffs != #{literal_prefix}'--- []\n')
    ".squish

    MergeRequestDiff.where(non_empty).each_batch(of: 500) do |relation, index|
      range = relation.pluck('MIN(id)', 'MAX(id)').first

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

  def down
  end
end