blob: 8700276c982bb0e6c211d151de4cd5eeb3bf0e5b (
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# frozen_string_literal: true
module LooseForeignKeys
class ProcessDeletedRecordsService
BATCH_SIZE = 1000
def initialize(connection:)
@connection = connection
end
def execute
raised_error = false
modification_tracker = ModificationTracker.new
tracked_tables.cycle do |table|
records = load_batch_for_table(table)
if records.empty?
tracked_tables.delete(table)
next
end
break if modification_tracker.over_limit?
loose_foreign_key_definitions = Gitlab::Database::LooseForeignKeys.definitions_by_table[table]
next if loose_foreign_key_definitions.empty?
LooseForeignKeys::BatchCleanerService
.new(
parent_table: table,
loose_foreign_key_definitions: loose_foreign_key_definitions,
deleted_parent_records: records,
modification_tracker: modification_tracker)
.execute
break if modification_tracker.over_limit?
end
::Gitlab::Metrics::LooseForeignKeysSlis.record_apdex(
success: !modification_tracker.over_limit?,
db_config_name: db_config_name
)
modification_tracker.stats
rescue StandardError
raised_error = true
raise
ensure
::Gitlab::Metrics::LooseForeignKeysSlis.record_error_rate(
error: raised_error,
db_config_name: db_config_name
)
end
private
attr_reader :connection
def db_config_name
::Gitlab::Database.db_config_name(connection)
end
def load_batch_for_table(table)
fully_qualified_table_name = "#{current_schema}.#{table}"
LooseForeignKeys::DeletedRecord.load_batch_for_table(fully_qualified_table_name, BATCH_SIZE)
end
def current_schema
@current_schema = connection.current_schema
end
def tracked_tables
@tracked_tables ||= Gitlab::Database::LooseForeignKeys.definitions_by_table.keys.shuffle
end
end
end
|