blob: 402fa7541b35402516a561b15f15b1d7cad46510 (
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
|
# frozen_string_literal: true
module Database
class ConsistencyFixService
def initialize(source_model:, target_model:, sync_event_class:, source_sort_key:, target_sort_key:)
@source_model = source_model
@target_model = target_model
@sync_event_class = sync_event_class
@source_sort_key = source_sort_key
@target_sort_key = target_sort_key
end
attr_accessor :source_model, :target_model, :sync_event_class, :source_sort_key, :target_sort_key
def execute(ids:)
ids.each do |id|
if source_object(id) && target_object(id)
create_sync_event_for(id)
elsif target_object(id)
target_object(id).destroy!
end
end
sync_event_class.enqueue_worker
end
private
# rubocop: disable CodeReuse/ActiveRecord
def source_object(id)
source_model.find_by(source_sort_key => id)
end
def target_object(id)
target_model.find_by(target_sort_key => id)
end
# rubocop: enable CodeReuse/ActiveRecord
def create_sync_event_for(id)
if source_model == Namespace
sync_event_class.create!(namespace_id: id)
elsif source_model == Project
sync_event_class.create!(project_id: id)
else
raise("Unknown Source Model #{source_model.name}")
end
end
end
end
|