forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection_handling.rb
61 lines (53 loc) · 1.77 KB
/
connection_handling.rb
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
class MiqPglogical
module ConnectionHandling
extend ActiveSupport::Concern
included do
delegate :logical_replication_supported?, :to => :class
end
module ClassMethods
def logical_replication_supported?
return @logical_replication_supported if defined?(@logical_replication_supported)
is_superuser = ActiveRecord::Base.connection.select_value("SELECT usesuper FROM pg_user WHERE usename = CURRENT_USER")
unless is_superuser
warn_bookends = ["\e[33m", "\e[0m"]
msg = "WARNING: Current user is NOT a superuser, logical replication will not function."
if $stderr.tty?
warn(warn_bookends.join(msg))
else
warn msg
end
_log.warn msg
end
@logical_replication_supported = is_superuser
end
def with_connection_error_handling
retry_attempted ||= false
if logical_replication_supported?
yield
else
# Silently do no harm since logical replication is not supported.
nil
end
rescue PG::ConnectionBad
raise if retry_attempted
pglogical(true)
retry_attempted = true
retry
end
def pglogical(_refresh = false)
# TODO: Review if the reasons behind the previous caching / refreshing
# of the PG::LogicalReplication::Client
#
# @pglogical = nil if refresh
# @pglogical ||= PG::LogicalReplication::Client.new(pg_connection)
#
# is still relevant as it caused segfaults with rails 6 when the
# caching was in place.
PG::LogicalReplication::Client.new(pg_connection)
end
end
def pglogical(refresh = false)
self.class.pglogical(refresh)
end
end
end