debian-mirror-gitlab/lib/gitlab/background_migration/update_timelogs_null_spent_at.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1,020 B
Ruby
Raw Normal View History

2022-01-26 12:08:38 +05:30
# 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)
2022-04-04 11:22:00 +05:30
define_batchable_model('timelogs', connection: connection)
.where(spent_at: nil, id: start_id..stop_id)
.each_batch(of: 100) do |subbatch|
2022-01-26 12:08:38 +05:30
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
2022-04-04 11:22:00 +05:30
def connection
2022-07-16 23:28:13 +05:30
@connection ||= ApplicationRecord.connection
2022-04-04 11:22:00 +05:30
end
def execute(sql)
connection.execute(sql)
2022-01-26 12:08:38 +05:30
end
end
end
end