summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180529152628_schedule_to_archive_legacy_traces.rb
blob: 965cd3a8714b13e947d00436551a95a29089e92a (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
class ScheduleToArchiveLegacyTraces < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  BATCH_SIZE = 5000
  BACKGROUND_MIGRATION_CLASS = 'ArchiveLegacyTraces'

  disable_ddl_transaction!

  class Build < ActiveRecord::Base
    include EachBatch
    self.table_name = 'ci_builds'
    self.inheritance_column = :_type_disabled # Disable STI

    scope :type_build, -> { where(type: 'Ci::Build') }

    scope :finished, -> { where(status: [:success, :failed, :canceled]) }

    scope :without_archived_trace, -> do
      where('NOT EXISTS (SELECT 1 FROM ci_job_artifacts WHERE ci_builds.id = ci_job_artifacts.job_id AND ci_job_artifacts.file_type = 3)')
    end
  end

  def up
    queue_background_migration_jobs_by_range_at_intervals(
      ::ScheduleToArchiveLegacyTraces::Build.type_build.finished.without_archived_trace,
      BACKGROUND_MIGRATION_CLASS,
      5.minutes,
      batch_size: BATCH_SIZE)
  end

  def down
    # noop
  end
end