« blib/lib/App/Netdisco/Web/Plugin/AdminTask/Topology.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/Device/SNMP.pm »
| 1 | package App::Netdisco::DB::SchemaVersioned; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'DBIx::Class::Schema::Versioned'; | ||
| 7 | |||
| 8 | use Try::Tiny; | ||
| 9 | use DBIx::Class::Carp; | ||
| 10 | use File::Basename (); | ||
| 11 | use JSON::PP (); | ||
| 12 | |||
| 13 | use App::Netdisco::DB::StepFile qw(parse_step_file legacy_statements); | ||
| 14 | use App::Netdisco::DB::UpgradeMessage qw(is_expected_sqlstate); | ||
| 15 | |||
| 16 | our $BASELINE = 38; | ||
| 17 | |||
| 18 | our $SCHEMA_STEP_FORCED_DDL = 'CREATE TABLE IF NOT EXISTS schema_step_forced (' | ||
| 19 | . 'step integer NOT NULL, ' | ||
| 20 | . 'forced_at timestamp NOT NULL DEFAULT LOCALTIMESTAMP, ' | ||
| 21 | . 'failed_count integer NOT NULL, ' | ||
| 22 | . "failures jsonb NOT NULL DEFAULT '[]')"; | ||
| 23 | |||
| 24 | sub step_mode { | ||
| 25 | my ($from, $options) = @_; | ||
| 26 | $options ||= {}; | ||
| 27 | return 'forced' | ||
| 28 | if defined $options->{force_step} and $options->{force_step} == $from; | ||
| 29 | return 'tolerant' if $options->{redeploy_all}; | ||
| 30 | return $from < $BASELINE ? 'tolerant' : 'strict'; | ||
| 31 | } | ||
| 32 | |||
| 33 | my %VALID_STEP_MODES = map { $_ => 1 } qw/strict tolerant forced/; | ||
| 34 | |||
| 35 | # DBIC's own upgrade() calls this with two arguments, hence the default mode. | ||
| 36 | sub upgrade_single_step { | ||
| 37 | my ($self, $from, $to, $mode) = @_; | ||
| 38 | $mode ||= step_mode($from, {}); | ||
| 39 | die "upgrade_single_step was given mode '$mode' for step $from;" | ||
| 40 | . " pass one of strict, tolerant or forced, or leave it out to let step_mode choose\n" | ||
| 41 | unless $VALID_STEP_MODES{$mode}; | ||
| 42 | |||
| 43 | if ($from eq $to) { | ||
| 44 | carp 'Upgrade not necessary'; | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $step_file = $self->ddl_filename( | ||
| 49 | $self->storage->sqlt_type, $to, $self->upgrade_directory, $from); | ||
| 50 | unless (-f $step_file) { | ||
| 51 | carp "Upgrade not possible, no upgrade file found ($step_file), please create one"; | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | # Netdisco's configured connections already set auto_savepoint; this covers | ||
| 56 | # one made without it, whose first nested rollback would otherwise abort | ||
| 57 | # the outer txn_scope_guard of an ND2_DB_ROLLBACK run. | ||
| 58 | $self->storage->auto_savepoint(1) if $ENV{ND2_DB_ROLLBACK}; | ||
| 59 | |||
| 60 | return $self->_strict_step($step_file, $to) if $mode eq 'strict'; | ||
| 61 | return $self->_tolerant_step($step_file, $to) if $mode eq 'tolerant'; | ||
| 62 | return $self->_tolerant_step($step_file, $to, | ||
| 63 | sub { $self->record_forced_step($from, shift) }, $from >= $BASELINE); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _new_step_result { | ||
| 67 | my $step_file = shift; | ||
| 68 | return { ok => 0, expected => 0, unexpected => [], skipped => [], | ||
| 69 | failed => undef, file => File::Basename::basename($step_file) }; | ||
| 70 | } | ||
| 71 | |||
| 72 | # DBD::Pg's state and errstr must be read before DBIC rolls back: afterwards | ||
| 73 | # they describe the rollback, and the rethrown text carries no SQLSTATE. | ||
| 74 | sub _do_capturing_failure { | ||
| 75 | my ($self, $statement, $failure_ref) = @_; | ||
| 76 | my $dbh = $self->storage->dbh; | ||
| 77 | try { $dbh->do($statement) } | ||
| 78 | catch { | ||
| 79 | $$failure_ref = { statement => $statement, state => $dbh->state, | ||
| 80 | error => ($dbh->errstr // "$_") }; | ||
| 81 | die $_; | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _strict_step { | ||
| 86 | my ($self, $step_file, $to) = @_; | ||
| 87 | my $result = _new_step_result($step_file); | ||
| 88 | my $parsed = parse_step_file($step_file); | ||
| 89 | my $failure; | ||
| 90 | |||
| 91 | my $apply_and_stamp = sub { | ||
| 92 | $self->_do_capturing_failure($_, \$failure) for @{ $parsed->{statements} }; | ||
| 93 | $self->_set_db_version({version => $to}); | ||
| 94 | }; | ||
| 95 | |||
| 96 | try { | ||
| 97 | $parsed->{no_transaction} | ||
| 98 | ? $self->_in_quiet_session($apply_and_stamp) | ||
| 99 | : $self->_in_quiet_transaction($apply_and_stamp); | ||
| 100 | $result->{ok} = 1; | ||
| 101 | } | ||
| 102 | catch { | ||
| 103 | $result->{failed} = $failure | ||
| 104 | || { statement => undef, state => undef, error => "$_" }; | ||
| 105 | }; | ||
| 106 | return $result; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Whole file first, so steps with dollar-quoted bodies replay cleanly; then, | ||
| 110 | # only if that failed, per statement. The old split breaks DO blocks apart, | ||
| 111 | # so it is kept only for the steps that always ran that way. | ||
| 112 | sub _tolerant_step { | ||
| 113 | my ($self, $step_file, $to, $before_stamp, $split_as_parsed) = @_; | ||
| 114 | my $strict_result = $self->_strict_step($step_file, $to); | ||
| 115 | return $strict_result if $strict_result->{ok}; | ||
| 116 | |||
| 117 | my $parsed = parse_step_file($step_file); | ||
| 118 | my $no_transaction = $parsed->{no_transaction}; | ||
| 119 | my $statements = $split_as_parsed | ||
| 120 | ? $parsed->{statements} : legacy_statements($step_file); | ||
| 121 | my $result = _new_step_result($step_file); | ||
| 122 | my $replay = sub { | ||
| 123 | $self->_replay_statement($_, $result, $no_transaction) for @$statements; | ||
| 124 | }; | ||
| 125 | $no_transaction ? $self->_in_quiet_session($replay) : $replay->(); | ||
| 126 | |||
| 127 | $self->_in_quiet_transaction(sub { | ||
| 128 | $before_stamp->($result->{skipped}) if $before_stamp and @{ $result->{skipped} }; | ||
| 129 | $self->_set_db_version({version => $to}); | ||
| 130 | }); | ||
| 131 | $result->{ok} = 1; | ||
| 132 | return $result; | ||
| 133 | } | ||
| 134 | |||
| 135 | # PostgreSQL refuses statements such as CREATE INDEX CONCURRENTLY inside a | ||
| 136 | # transaction, so a no-transaction step replays them bare. | ||
| 137 | sub _replay_statement { | ||
| 138 | my ($self, $statement, $result, $no_transaction) = @_; | ||
| 139 | my $failure; | ||
| 140 | my $run = sub { $self->_do_capturing_failure($statement, \$failure) }; | ||
| 141 | try { $no_transaction ? $run->() : $self->_in_quiet_transaction($run) } | ||
| 142 | catch { _tally_tolerated_failure($result, $failure, $statement, $_) }; | ||
| 143 | } | ||
| 144 | |||
| 145 | # The IF [NOT] EXISTS forms in old steps raise NOTICE "skipping" lines that | ||
| 146 | # read as faults. A step can still set it back to notice for a NOTICE it | ||
| 147 | # means the user to see, as 101-102 does. | ||
| 148 | sub _in_quiet_transaction { | ||
| 149 | my ($self, $code) = @_; | ||
| 150 | return $self->storage->txn_do(sub { | ||
| 151 | $self->storage->dbh->do('SET LOCAL client_min_messages = warning'); | ||
| 152 | return $code->(); | ||
| 153 | }); | ||
| 154 | } | ||
| 155 | |||
| 156 | # SET LOCAL outside a transaction does nothing, so quiet the session instead | ||
| 157 | # and put the old level back. | ||
| 158 | sub _in_quiet_session { | ||
| 159 | my ($self, $code) = @_; | ||
| 160 | my $dbh = $self->storage->dbh; | ||
| 161 | my ($previous) = $dbh->selectrow_array('SHOW client_min_messages'); | ||
| 162 | $dbh->do('SET client_min_messages = warning'); | ||
| 163 | my $failure; | ||
| 164 | try { $code->() } catch { $failure = $_ }; | ||
| 165 | # Fails only in an aborted ND2_DB_ROLLBACK transaction, whose rollback | ||
| 166 | # restores the level anyway. | ||
| 167 | try { $dbh->do('SET client_min_messages = ' . $dbh->quote($previous)) }; | ||
| 168 | die $failure if defined $failure; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _tally_tolerated_failure { | ||
| 172 | my ($result, $failure, $statement, $exception) = @_; | ||
| 173 | carp "SQL was: $statement" if $ENV{DBIC_TRACE}; | ||
| 174 | $failure ||= { statement => $statement, state => undef, error => "$exception" }; | ||
| 175 | push @{ $result->{skipped} }, $failure; | ||
| 176 | if (is_expected_sqlstate($failure->{state})) { | ||
| 177 | $result->{expected}++; | ||
| 178 | return; | ||
| 179 | } | ||
| 180 | push @{ $result->{unexpected} }, $failure; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub record_forced_step { | ||
| 184 | my ($self, $from, $skipped) = @_; | ||
| 185 | $skipped ||= []; | ||
| 186 | my $failures_json = JSON::PP->new->canonical->encode([ | ||
| 187 | map { +{ statement => $_->{statement}, state => $_->{state}, | ||
| 188 | error => $_->{error} } } @$skipped | ||
| 189 | ]); | ||
| 190 | |||
| 191 | $self->storage->dbh_do(sub { | ||
| 192 | my (undef, $dbh) = @_; | ||
| 193 | $dbh->do($SCHEMA_STEP_FORCED_DDL); | ||
| 194 | $dbh->do('INSERT INTO schema_step_forced (step, failed_count, failures)' | ||
| 195 | . ' VALUES (?, ?, ?::jsonb)', undef, $from, scalar @$skipped, $failures_json); | ||
| 196 | }); | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; |