blib/lib/App/Netdisco/Util/PostgresFloor.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 109 CC 9 SCAR 22.4
CC
9
Cov
0%
_probe_connect_info
20.0
postgres_floor_warning
12.0
warn_postgres_floor_all_tenants
12.0
CRAP 9.4
1 package App::Netdisco::Util::PostgresFloor;
2  
3 use strict;
4 use warnings;
5  
6 use Dancer ':syntax';
7 use Try::Tiny;
8  
9 use App::Netdisco::DB;
10 use App::Netdisco::Util::Statistics 'postgres_version';
11  
12 use base 'Exporter';
13 our @EXPORT_OK = qw/
14 $POSTGRES_FLOOR_MAJOR
15 postgres_floor_warning
16 warn_postgres_floor_all_tenants
17 /;
18  
19 our $POSTGRES_FLOOR_MAJOR = 14;
20  
21 =head1 NAME
22  
23 App::Netdisco::Util::PostgresFloor - Warn when PostgreSQL is older than a future release will accept
24  
25 =head2 postgres_floor_warning( $server_version_num, $label )
26  
27 Returns warning text for a server older than C<$POSTGRES_FLOOR_MAJOR>, naming
28 the database C<$label>. Returns C<undef> for a supported or unreadable version.
29  
30 =head2 warn_postgres_floor_all_tenants( $log_coderef, \%override )
31  
32 Calls C<$log_coderef> with a warning for the main database and for each
33 configured tenant database that is below the floor. A database that cannot be
34 reached is skipped silently. C<%override> (C<labels>, C<version_for>) replaces
35 the configured databases, for tests.
36  
37 =cut
38  
39 sub postgres_floor_warning {
40 my ($server_version_num, $label) = @_;
41 my $display = postgres_version($server_version_num) or return undef;
42 return undef if $server_version_num < 90000;
43 return undef if int($server_version_num / 10000) >= $POSTGRES_FLOOR_MAJOR;
44  
45 return sprintf 'PostgreSQL %s for database "%s" is older than %d. '
46 . 'A future release of Netdisco will require PostgreSQL %d or later. '
47 . 'See https://github.com/netdisco/netdisco/wiki/Release-Notes',
48 $display, $label, $POSTGRES_FLOOR_MAJOR, $POSTGRES_FLOOR_MAJOR;
49 }
50  
51 sub warn_postgres_floor_all_tenants {
52 my ($log, $override) = @_;
53 my @labels = $override ? @{ $override->{labels} }
54 : ('netdisco', map { $_->{tag} } grep { $_->{tag} and $_->{tag} ne 'netdisco' }
55 @{ setting('tenant_databases') || [] });
56 my $version_for = $override ? $override->{version_for} : \&_live_version;
57  
58 foreach my $label (@labels) {
59 my $server_version_num = try { $version_for->($label) } catch { undef };
60 my $text = postgres_floor_warning($server_version_num, $label) or next;
61 $log->($text);
62 }
63 }
64  
65 sub _live_version {
66 my $label = shift;
67 # a tenant host that drops packets must not stall boot for the TCP timeout
68 local $ENV{PGCONNECT_TIMEOUT} = $ENV{PGCONNECT_TIMEOUT} || 3;
69 my $dbic = setting('plugins')->{DBIC};
70 my $options = $dbic->{$label} || {};
71 $options = $dbic->{ $options->{alias} } if $options->{alias};
72 # schema() would cache this in the master process, and every forked
73 # poller would then hold it as well as its own 'default' connection
74 my $storage = App::Netdisco::DB->connect(_probe_connect_info($options))->storage;
75 my $version = $storage->dbh->{pg_server_version};
76 # the web master forks workers, which must not inherit this connection
77 $storage->disconnect;
78 return $version;
79 }
80  
81 # the DBIC plugin's connection settings, plus ignore_version: DBIC's
82 # version check would carp at every boot
83 sub _probe_connect_info {
84 my $options = shift;
85 my @info = $options->{connect_info}
86 ? @{ $options->{connect_info} }
87 : @$options{qw/dsn user password options/};
88 $info[2] = $options->{pass} if exists $options->{pass};
89 my $attributes = (ref $info[-1] eq 'HASH') ? pop @info : {};
90 return (@info, { %$attributes, ignore_version => 1 });
91 }
92  
93 true;