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

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

45 lines
1.3 KiB
Ruby
Raw Normal View History

2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Class to populate project_id for timelogs
class UpdateTimelogsProjectId
BATCH_SIZE = 1000
def perform(start_id, stop_id)
(start_id..stop_id).step(BATCH_SIZE).each do |offset|
update_issue_timelogs(offset, offset + BATCH_SIZE)
update_merge_request_timelogs(offset, offset + BATCH_SIZE)
end
end
def update_issue_timelogs(batch_start, batch_stop)
execute(<<~SQL)
UPDATE timelogs
SET project_id = issues.project_id
FROM issues
WHERE issues.id = timelogs.issue_id
AND timelogs.id BETWEEN #{batch_start} AND #{batch_stop}
AND timelogs.project_id IS NULL;
SQL
end
def update_merge_request_timelogs(batch_start, batch_stop)
execute(<<~SQL)
UPDATE timelogs
SET project_id = merge_requests.target_project_id
FROM merge_requests
WHERE merge_requests.id = timelogs.merge_request_id
AND timelogs.id BETWEEN #{batch_start} AND #{batch_stop}
AND timelogs.project_id IS NULL;
SQL
end
def execute(sql)
2022-07-16 23:28:13 +05:30
@connection ||= ApplicationRecord.connection
2021-06-08 01:23:25 +05:30
@connection.execute(sql)
end
end
end
end