Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_timelogs_null_spent_at.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c95ef9f5515a4ad178c1b68d1218d1bcfc0763ba (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Class to populate spent_at for timelogs
    class UpdateTimelogsNullSpentAt
      include Gitlab::Database::DynamicModelHelpers

      BATCH_SIZE = 100

      def perform(start_id, stop_id)
        define_batchable_model('timelogs').where(spent_at: nil, id: start_id..stop_id).each_batch(of: 100) do |subbatch|
          batch_start, batch_end = subbatch.pluck('min(id), max(id)').first

          update_timelogs(batch_start, batch_end)
        end
      end

      def update_timelogs(batch_start, batch_stop)
        execute(<<~SQL)
          UPDATE timelogs
          SET spent_at = created_at
          WHERE spent_at IS NULL
          AND timelogs.id BETWEEN #{batch_start} AND #{batch_stop};
        SQL
      end

      def execute(sql)
        @connection ||= ::ActiveRecord::Base.connection
        @connection.execute(sql)
      end
    end
  end
end