blob: 69bb5cf6e6d1b231f654ed3be35b1e2d17378d97 (
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
|
# 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)
@connection ||= ApplicationRecord.connection
@connection.execute(sql)
end
end
end
end
|