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
|
# frozen_string_literal: true
module CrossDatabaseIgnoredTables
extend ActiveSupport::Concern
class_methods do
def cross_database_ignore_tables(tables, options = {})
raise "missing issue url" if options[:url].blank?
options[:on] = %I[save destroy] if options[:on].blank?
events = Array.wrap(options[:on])
tables = Array.wrap(tables)
events.each do |event|
register_ignored_cross_database_event(tables, event, options)
end
end
private
def register_ignored_cross_database_event(tables, event, options)
case event
when :save
around_save(prepend: true) { |_, blk| temporary_ignore_cross_database_tables(tables, options, &blk) }
when :create
around_create(prepend: true) { |_, blk| temporary_ignore_cross_database_tables(tables, options, &blk) }
when :update
around_update(prepend: true) { |_, blk| temporary_ignore_cross_database_tables(tables, options, &blk) }
when :destroy
around_destroy(prepend: true) { |_, blk| temporary_ignore_cross_database_tables(tables, options, &blk) }
else
raise "Unknown #{event}"
end
end
end
private
def temporary_ignore_cross_database_tables(tables, options, &blk)
return yield unless options[:if].nil? || instance_eval(&options[:if])
url = options[:url]
Gitlab::Database::QueryAnalyzers::PreventCrossDatabaseModification.temporary_ignore_tables_in_transaction(
tables, url: url, &blk
)
end
end
|