summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/remove_all_trace_expiration_dates.rb
blob: d47aa76f24ba2e79a85121774b8b84a48149a008 (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
51
52
53
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Removing expire_at timestamps that shouldn't have
    # been written to traces on gitlab.com.
    class RemoveAllTraceExpirationDates
      include Gitlab::Database::MigrationHelpers

      BATCH_SIZE = 1_000

      # Stubbed class to connect to the CI database
      # connects_to has to be called in abstract classes.
      class MultiDbAdaptableClass < ActiveRecord::Base
        self.abstract_class = true

        if Gitlab::Database.has_config?(:ci)
          connects_to database: { writing: :ci, reading: :ci }
        end
      end

      # Stubbed class to access the ci_job_artifacts table
      class JobArtifact < MultiDbAdaptableClass
        include EachBatch

        self.table_name = 'ci_job_artifacts'

        TARGET_TIMESTAMPS = [
          Date.new(2021, 04, 22).midnight.utc,
          Date.new(2021, 05, 22).midnight.utc,
          Date.new(2021, 06, 22).midnight.utc,
          Date.new(2022, 01, 22).midnight.utc,
          Date.new(2022, 02, 22).midnight.utc,
          Date.new(2022, 03, 22).midnight.utc,
          Date.new(2022, 04, 22).midnight.utc
        ].freeze

        scope :traces, -> { where(file_type: 3) }
        scope :between, -> (start_id, end_id) { where(id: start_id..end_id) }
        scope :in_targeted_timestamps, -> { where(expire_at: TARGET_TIMESTAMPS) }
      end

      def perform(start_id, end_id)
        return unless Gitlab.com?

        JobArtifact.traces
          .between(start_id, end_id)
          .in_targeted_timestamps
          .each_batch(of: BATCH_SIZE) { |batch| batch.update_all(expire_at: nil) }
      end
    end
  end
end