blib/lib/App/Netdisco/DB/UpgradeMessage.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 114 CC 17 SCAR 31.5
CC
17
Cov
0%
tolerant_summary
56.0
strict_failure_message
42.0
is_expected_sqlstate
6.0
CRAP 23.2
1 package App::Netdisco::DB::UpgradeMessage;
2  
3 use strict;
4 use warnings;
5  
6 use base 'Exporter';
7 our @EXPORT_OK = qw(
8 @EXPECTED_SQLSTATES is_expected_sqlstate strict_failure_message
9 force_refusal_message force_already_applied_message tolerant_summary
10 unexpected_error_message forced_step_summary forced_step_clean_message
11 );
12  
13 our @EXPECTED_SQLSTATES = qw/42P07 42701 42703 42704 42P01 42P16 42601 42883/;
14 my %EXPECTED = map { $_ => 1 } @EXPECTED_SQLSTATES;
15  
16 sub is_expected_sqlstate {
17 my $state = shift;
18 return (defined $state and $EXPECTED{$state}) ? 1 : 0;
19 }
20  
21 sub _indent { my $text = shift // ''; $text =~ s/^/ /mg; return $text }
22  
23 # Restarting the db-init container alone starts neither backend nor web, and
24 # helm runs its db-init job only on install or upgrade.
25 my $CONTAINER_RERUN = 'start the upgrade again: rerun your docker compose up command,'
26 . ' which reruns netdisco-db-init, or run helm upgrade, which reruns the db-init job.';
27  
28 # A container user cannot run a command, so the same action becomes a setting.
29 sub _action {
30 my ($container, $arguments) = @_;
31 return $container
32 ? "Set NETDISCO_DB_DEPLOY_ARGS=$arguments in .env for docker compose,"
33 . qq{ or dbInit.deployArgs: "$arguments" in your helm values. Then $CONTAINER_RERUN}
34 : " netdisco-db-deploy $arguments";
35 }
36  
37 sub _force_line {
38 return 'Forcing skips the failing statements in that step, stamps the new '
39 . 'version, and writes a row to the schema_step_forced table so the skip is recorded.';
40 }
41  
42 sub strict_failure_message {
43 my %arg = @_;
44 my ($database, $container) = @arg{qw/database container/};
45 return join "\n",
46 "The database upgrade stopped at step $arg{from}, in $arg{file}.",
47 '',
48 'This statement failed:',
49 '',
50 _indent($arg{statement}),
51 '',
52 'PostgreSQL said:',
53 '',
54 _indent($arg{error}),
55 '',
56 'Nothing from this step was applied: it was rolled back.'
57 . ' Any steps before it in this run were applied and stay applied.',
58 '',
59 ($container
60 ? ("Before trying anything below, back up the $database database with pg_dump,"
61 . ' run from wherever the database server can be reached.')
62 : ('Before trying anything below, make a backup:', '',
63 " pg_dump -Fc $database > netdisco-backup.dump")),
64 '',
65 ($container
66 ? "Fix the cause, then $CONTAINER_RERUN No setting is needed."
67 : 'Fix the cause, then run netdisco-db-deploy again:'),
68 '',
69 ($container ? () : (' netdisco-db-deploy', '')),
70 'If you cannot fix the cause and want to carry on anyway, you can force this step. '
71 . _force_line() . ($container ? '' : ' Run:'),
72 '',
73 _action($container, "--force-step $arg{from}"),
74 '',
75 ($container ? 'Remove the setting again after the upgrade has finished.' : ()),
76 '';
77 }
78  
79 sub force_refusal_message {
80 my %arg = @_;
81 return join "\n",
82 "Cannot force step $arg{requested}: steps are applied in order, and step $arg{next_pending} has not been applied yet.",
83 '',
84 "Force step $arg{next_pending} instead, if you mean to skip it" . ($arg{container} ? '.' : ':'),
85 '',
86 _action($arg{container}, "--force-step $arg{next_pending}"),
87 '';
88 }
89  
90 sub force_already_applied_message {
91 my %arg = @_;
92 my $removal = $arg{container}
93 ? 'The upgrade carries on without it. Remove --force-step from'
94 . ' NETDISCO_DB_DEPLOY_ARGS (dbInit.deployArgs with helm) before the next start.'
95 : 'The upgrade carries on without it; leave --force-step off next time.';
96 return join "\n",
97 "Step $arg{requested} has already been applied, so there is nothing to force.",
98 '',
99 $removal,
100 '';
101 }
102  
103 sub _check_hint {
104 my ($container, $purpose) = @_;
105 return $container
106 ? ". To $purpose, " . lcfirst _action(1, '--check')
107 : "; run netdisco-db-deploy --check to $purpose.";
108 }
109  
110 sub tolerant_summary {
111 my %arg = @_;
112 my $unexpected = $arg{unexpected} || 0;
113 my $shown = $unexpected == 1 ? 'is shown above' : 'are shown above';
114 # under --redeploy-all the erroring step may be the newest, not an older one
115 my ($one, $many) = $arg{replayed}
116 ? ('a replayed step', 'replayed steps') : ('an older step', 'older steps');
117 my $summary = !$arg{expected}
118 ? ($unexpected == 1 ? "An error in $one" : "$unexpected errors in $many") . " $shown"
119 : "Expected errors on $arg{expected} statements across $arg{steps} $many were ignored"
120 . ($unexpected ? ", and $unexpected other " . ($unexpected == 1 ? 'error' : 'errors') . " $shown" : '');
121 return $summary . _check_hint($arg{container}, 'see the result');
122 }
123  
124 sub forced_step_summary {
125 my %arg = @_;
126 return "Step $arg{from} was forced: $arg{skipped} failing statements were skipped "
127 . 'and recorded in the schema_step_forced table' . _check_hint($arg{container}, 'see what is missing');
128 }
129  
130 sub forced_step_clean_message {
131 my %arg = @_;
132 return "Step $arg{from} applied without errors, so nothing was skipped or recorded.";
133 }
134  
135 sub unexpected_error_message {
136 my %arg = @_;
137 return join "\n",
138 "An older upgrade step, $arg{file}, hit an error that is not one of the usual harmless ones (SQLSTATE $arg{state}).",
139 '',
140 'This statement failed:',
141 '',
142 _indent($arg{statement}),
143 '',
144 'PostgreSQL said:',
145 '',
146 _indent($arg{error}),
147 '';
148 }
149  
150 1;