| 1 | package DBIO::DB2::Deploy; | ||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Deploy and upgrade DB2 schemas via test-deploy-and-compare | ||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 34 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use base 'DBIO::Deploy::Base'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | 34 | ▶ | use DBIO::DB2::DDL qw(_db2_quote_ident _db2_quote_table _split_table_name); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 34 | ▶ | use DBIO::DB2::Introspect (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 34 | ▶ | use DBIO::DB2::Diff (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | 34 | ▶ | use DBIO::SQL::Util (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | 34 | ▶ | use DBIO::DB2::Diff::Reorg (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | 34 | ▶ | use Carp (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 17 | 34 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | # --- class-name hooks for DBIO::Deploy::Base ------------------------------- | ||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | sub _ddl_class { 'DBIO::DB2::DDL' } | ||||||||||||||||||||||||||||
|
Subroutine
_ddl_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub _introspect_class { 'DBIO::DB2::Introspect' } | ||||||||||||||||||||||||||||
|
Subroutine
_introspect_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub _diff_class { 'DBIO::DB2::Diff' } | ||||||||||||||||||||||||||||
|
Subroutine
_diff_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub _new_introspect { | ||||||||||||||||||||||||||||
|
Subroutine
_new_introspect: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 28 | 0 | my ($self, $dbh, $schema) = @_; | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | return $self->_introspect_class->new( | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 30 | dbh => $dbh, | ||||||||||||||||||||||||||||||
| 31 | (defined $schema ? (schema => $schema) : ()), | ||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub _introspect_current { | ||||||||||||||||||||||||||||
|
Subroutine
_introspect_current: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 38 | 0 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||||
| 39 | 0 | $self->_refuse_cross_schema_keys($self->schema); | |||||||||||||||||||||||||||||
| 40 | 0 | my $model = $self->_new_introspect($dbh, $self->_current_schema($dbh))->model; | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | # The schemas of the tables named schema.table (karr k57): read as well, the | ||||||||||||||||||||||||||||||
| 43 | # declared tables of them only, under the name schema.table. A schema the | ||||||||||||||||||||||||||||||
| 44 | # server does not have is an empty model; a table of a schema that the | ||||||||||||||||||||||||||||||
| 45 | # declaration does not name is in no model, so no diff ever drops it. | ||||||||||||||||||||||||||||||
| 46 | 0 | for my $schema_name ($self->_qualified_schemas) { | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my %declared = map { $_->[0] =~ /\A\Q$schema_name\E\.(.*)\z/s ? ($1 => 1) : () } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | DBIO::DB2::DDL->qualified_tables($self->schema); | ||||||||||||||||||||||||||||||
| 49 | 0 | my $other = $self->_new_introspect($dbh, $schema_name)->model; | |||||||||||||||||||||||||||||
| 50 | 0 | $self->_merge_schema_model($model, $other, $schema_name, \%declared); | |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||
| 52 | 0 | return $model; | |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | sub _qualified_schemas { | ||||||||||||||||||||||||||||
|
Subroutine
_qualified_schemas: called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||||
| 57 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 58 | 1 | my %seen; | |||||||||||||||||||||||||||||
| 59 | 0 | return sort grep { !$seen{$_}++ } map { $_->[1] } DBIO::DB2::DDL->qualified_tables($self->schema); | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | sub _merge_schema_model { | ||||||||||||||||||||||||||||
|
Subroutine
_merge_schema_model: called CC=11 SCAR=25.6
| |||||||||||||||||||||||||||||||
| 64 | 1 | my ($self, $model, $other, $schema_name, $declared) = @_; | |||||||||||||||||||||||||||||
| 65 | 1 | my %keep = map { $_ => 1 } | |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | grep { $declared->{$_} && (($other->{tables}{$_}{kind}) // 'table') eq 'table' } | ||||||||||||||||||||||||||||
|
Condition60%3/5
$declared->{$_} && ($other->{'tables'}{$_}{'kind'} // 'table') eq 'table'
$other->{"tables"}{$_}{"kind"} // "table"
MC/DC50%1/2
$declared->{$_} && ($other->{'tables'}{$_}{'kind'} // 'table') eq 'table'
$declared->{$_}
$other->{"tables"}{$_}{"kind"}
Truth table66%2/3
A && B
A= $declared->{$_}B= $other->{"tables"}{$_}{"kind"}
| |||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | keys %{ $other->{tables} || {} }; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$other->{'tables'} || {}
MC/DC0%0/1
$other->{'tables'} || {}
$other->{'tables'}
Truth table50%1/2
A
A= $other->{'tables'}
| |||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | my $name = sub { "$schema_name.$_[0]" }; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||
| 70 | 1 | for my $table (sort keys %keep) { | |||||||||||||||||||||||||||||
| 71 | 1 | my $q = $name->($table); | |||||||||||||||||||||||||||||
| 72 | 1 | $model->{tables}{$q} = { %{ $other->{tables}{$table} }, table_name => $q }; | |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $model->{columns}{$q} = $other->{columns}{$table} if $other->{columns}{$table}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | $model->{indexes}{$q} = $other->{indexes}{$table} if $other->{indexes}{$table}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 75 | $model->{unique_constraints}{$q} = $other->{unique_constraints}{$table} | ||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | if $other->{unique_constraints}{$table}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 77 | $model->{foreign_keys}{$q} = [ map { | ||||||||||||||||||||||||||||||
| 78 | 1 | my %fk = %$_; | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | $fk{to_table} = $name->($fk{to_table}) if defined $fk{to_table} && $declared->{ $fk{to_table} }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $fk{"to_table"} and $$declared{$fk{"to_table"}}
MC/DC0%0/2
defined $fk{"to_table"} and $$declared{$fk{"to_table"}}
defined $fk{"to_table"}
$$declared{$fk{"to_table"}}
Truth table33%1/3
A and B
A= defined $fk{"to_table"}B= $$declared{$fk{"to_table"}}
| |||||||||||||||||||||||||||||||
| 80 | 1 | \%fk; | |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | } @{ $other->{foreign_keys}{$table} } ] if $other->{foreign_keys}{$table}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||
| 83 | 1 | return $model; | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | sub _refuse_cross_schema_keys { | ||||||||||||||||||||||||||||
|
Subroutine
_refuse_cross_schema_keys: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||
| 88 | 2 | my ($self, $schema) = @_; | |||||||||||||||||||||||||||||
| 89 | 2 | for my $moniker (sort $schema->sources) { | |||||||||||||||||||||||||||||
| 90 | 4 | my $source = $schema->source($moniker); | |||||||||||||||||||||||||||||
| 91 | 4 | ▶ | next if $source->isa('DBIO::ResultSource::View'); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 92 | 4 | my $table = DBIO::DB2::DDL::_resolve_table_name($source->name); | |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | next unless defined $table; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 94 | 4 | my ($from_schema) = _split_table_name($table); | |||||||||||||||||||||||||||||
| 95 | 4 | for my $fk (DBIO::DB2::DDL::_table_foreign_keys($schema, $source)) { | |||||||||||||||||||||||||||||
| 96 | 2 | my ($to_schema) = _split_table_name($fk->{to_table}); | |||||||||||||||||||||||||||||
| 97 | 2 | ▶ | next if ($from_schema // '') eq ($to_schema // ''); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$from_schema // ""
$to_schema // ""
MC/DC100%1/1
$from_schema // ""
$from_schema
MC/DC0%0/1
$to_schema // ""
$to_schema
Truth table100%2/2
A
A= $from_schema
Truth table50%1/2
A
A= $to_schema
| |||||||||||||||||||||||||||||||
| 98 | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||||
| 99 | 'DBIO::DB2: the diff is refused: the foreign key %s from the table "%s" to the table "%s" ' | ||||||||||||||||||||||||||||||
| 100 | . 'crosses schemas, and the compare deploy holds the tables of one schema at a time; ' | ||||||||||||||||||||||||||||||
| 101 | . 'nothing has been executed', | ||||||||||||||||||||||||||||||
| 102 | 1 | $fk->{constraint_name}, $table, $fk->{to_table}); | |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||
| 105 | 1 | return; | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | # Read the connection's CURRENT SCHEMA. Shared by _introspect_current (which | ||||||||||||||||||||||||||||||
| 109 | # scopes the source introspection to it) and _build_target_model (which saves it | ||||||||||||||||||||||||||||||
| 110 | # to put back). CURRENT SCHEMA is CHAR(128), space-padded; right-trim before use | ||||||||||||||||||||||||||||||
| 111 | # -- SYSCAT stores schema names unpadded, and the padding would also travel into | ||||||||||||||||||||||||||||||
| 112 | # the string constant _set_current_schema builds. | ||||||||||||||||||||||||||||||
| 113 | 34 | ▶ | sub _current_schema { | ||||||||||||||||||||||||||||
|
Subroutine
_current_schema: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 114 | 34 | my ($self, $dbh) = @_; | |||||||||||||||||||||||||||||
| 115 | 34 | return DBIO::DB2::DDL::current_schema($dbh); | |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | # Point CURRENT SCHEMA at $schema. The value is passed as a STRING CONSTANT, not | ||||||||||||||||||||||||||||||
| 119 | # as a bare identifier: a bare name folds to uppercase, which would silently | ||||||||||||||||||||||||||||||
| 120 | # rewrite a saved lower/mixed-case schema on the way back. | ||||||||||||||||||||||||||||||
| 121 | 4 | ▶ | sub _set_current_schema { | ||||||||||||||||||||||||||||
|
Subroutine
_set_current_schema: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 122 | 4 | my ($self, $dbh, $schema) = @_; | |||||||||||||||||||||||||||||
| 123 | 4 | (my $literal = $schema) =~ s/'/''/g; | |||||||||||||||||||||||||||||
| 124 | 4 | $dbh->do("SET CURRENT SCHEMA = '$literal'"); | |||||||||||||||||||||||||||||
| 125 | 4 | return; | |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub _install_ddl { | ||||||||||||||||||||||||||||
|
Subroutine
_install_ddl: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 130 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 131 | 0 | return $self->_ddl_class->install_ddl($self->schema, $self->_current_schema($self->_dbh)); | |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | sub _build_target_model_for { | ||||||||||||||||||||||||||||
|
Subroutine
_build_target_model_for: called CC=9 SCAR=22.6
| |||||||||||||||||||||||||||||||
| 136 | 3 | my ($self, $opt) = @_; | |||||||||||||||||||||||||||||
| 137 | 3 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||||
| 138 | # Must start with a letter and be uppercase: DB2 rejects a leading-underscore | ||||||||||||||||||||||||||||||
| 139 | # unquoted identifier (CREATE SCHEMA _dbio_test_<pid> => SQL20521N), and SYSCAT | ||||||||||||||||||||||||||||||
| 140 | # stores schema names uppercased, so a lowercase name would never match the | ||||||||||||||||||||||||||||||
| 141 | # introspect filter (WHERE tabschema = ?). $$ is digits, so DBIO_TEST_<pid> is a | ||||||||||||||||||||||||||||||
| 142 | # valid bare uppercase identifier that round-trips through SYSCAT unchanged. | ||||||||||||||||||||||||||||||
| 143 | 3 | my $test_schema = 'DBIO_TEST_' . $$; | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||
| 145 | # Read CURRENT SCHEMA BEFORE the first side effect. The compare deploy runs | ||||||||||||||||||||||||||||||
| 146 | # with the register pointed at the throwaway schema (see the head of this | ||||||||||||||||||||||||||||||
| 147 | # method), and this is the value that has to go back onto the SHARED handle | ||||||||||||||||||||||||||||||
| 148 | # afterwards -- so a value we cannot read is a run we must not start. Refusing | ||||||||||||||||||||||||||||||
| 149 | # here costs nothing; refusing after CREATE SCHEMA would leak one. | ||||||||||||||||||||||||||||||
| 150 | 3 | my $saved_schema = $self->_current_schema($dbh); | |||||||||||||||||||||||||||||
| 151 | 3 | ▶ | die ref($self) . ": cannot read CURRENT SCHEMA -- refusing to run the compare " | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $saved_schema and length $saved_schema
MC/DC50%1/2
defined $saved_schema and length $saved_schema
defined $saved_schema
length $saved_schema
Truth table66%2/3
A and B
A= defined $saved_schemaB= length $saved_schema
| |||||||||||||||||||||||||||||||
| 152 | . "deploy, because the live connection's schema could not be restored " | ||||||||||||||||||||||||||||||
| 153 | . "afterwards\n" | ||||||||||||||||||||||||||||||
| 154 | unless defined $saved_schema && length $saved_schema; | ||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||
| 156 | # Defensively clear a same-named schema leaked by a crashed prior run (or an | ||||||||||||||||||||||||||||||
| 157 | # earlier same-pid call whose teardown was skipped) so CREATE SCHEMA cannot | ||||||||||||||||||||||||||||||
| 158 | # collide (SQL0601N). _drop_test_schema is best-effort and copes with a | ||||||||||||||||||||||||||||||
| 159 | # populated schema. | ||||||||||||||||||||||||||||||
| 160 | 2 | my ($exists) = $dbh->selectrow_array( | |||||||||||||||||||||||||||||
| 161 | 'SELECT 1 FROM syscat.schemata WHERE schemaname = ?', undef, $test_schema, | ||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | $self->_drop_test_schema($dbh, $test_schema) if $exists; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||
| 165 | 2 | $dbh->do("CREATE SCHEMA $test_schema"); | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||
| 167 | 2 | my $model = eval { | |||||||||||||||||||||||||||||
| 168 | 2 | $self->_set_current_schema($dbh, $test_schema); | |||||||||||||||||||||||||||||
| 169 | 2 | my $ddl = $self->_ddl_class->install_ddl($self->schema, $saved_schema, $opt); | |||||||||||||||||||||||||||||
| 170 | 2 | for my $stmt ($self->_split_qualify_ddl($ddl, $test_schema)) { | |||||||||||||||||||||||||||||
| 171 | 10 | $dbh->do($stmt); | |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||
| 173 | 1 | return $self->_new_introspect($dbh, $test_schema)->model; | |||||||||||||||||||||||||||||
| 174 | }; | ||||||||||||||||||||||||||||||
| 175 | # Capture the build error NOW. Everything below runs evals of its own -- the | ||||||||||||||||||||||||||||||
| 176 | # restore right here, and every statement in _drop_test_schema -- and each of | ||||||||||||||||||||||||||||||
| 177 | # them blanks $@, so the old `die $@ if $@ and not $model` after the teardown | ||||||||||||||||||||||||||||||
| 178 | # read a $@ that belonged to the last teardown eval, not to the build: a build | ||||||||||||||||||||||||||||||
| 179 | # failure came back as a silent undef model (or was masked by a teardown | ||||||||||||||||||||||||||||||
| 180 | # error). ADR 0001 already describes the intent this restores ("re-raises only | ||||||||||||||||||||||||||||||
| 181 | # when the build itself failed"). | ||||||||||||||||||||||||||||||
| 182 | 2 | my $err = $@; | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||
| 184 | # Put CURRENT SCHEMA back before the teardown, so the shared handle is correct | ||||||||||||||||||||||||||||||
| 185 | # again at the earliest possible point. The teardown itself does not depend on | ||||||||||||||||||||||||||||||
| 186 | # it -- every name it emits is explicitly qualified. | ||||||||||||||||||||||||||||||
| 187 | 2 | my $restored = eval { $self->_set_current_schema($dbh, $saved_schema); 1 }; | |||||||||||||||||||||||||||||
| 188 | 2 | my $restore_err = $@; | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | 2 | $self->_drop_test_schema($dbh, $test_schema, $model); | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | # A failed restore is fatal even when the model came back fine: the handle is | ||||||||||||||||||||||||||||||
| 193 | # shared with the live storage and is now pointing at a schema this method | ||||||||||||||||||||||||||||||
| 194 | # just dropped, so every later unqualified statement would resolve wrong. | ||||||||||||||||||||||||||||||
| 195 | 2 | ▶ | if (!$restored) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 196 | 0 | my $note = ref($self) . ": failed to restore CURRENT SCHEMA to " | |||||||||||||||||||||||||||||
| 197 | . "'$saved_schema' after the compare deploy -- the shared connection is " | ||||||||||||||||||||||||||||||
| 198 | . "left pointing at the dropped compare schema: $restore_err"; | ||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | die $err ? "$err\n$note\n" : "$note\n"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||
| 201 | 2 | ▶ | die $err if $err and not $model; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$err and not $model
MC/DC50%1/2
$err and not $model
$err
not $model
Truth table66%2/3
A and not(B)
A= $errB= not $model
| |||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||
| 203 | 1 | return $model; | |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||
| 206 | 3 | ▶ | sub _build_target_model { | ||||||||||||||||||||||||||||
|
Subroutine
_build_target_model: called CC=3 SCAR=16.0
| |||||||||||||||||||||||||||||||
| 207 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 208 | # the tables of the current schema, and then those of each schema named by a | ||||||||||||||||||||||||||||||
| 209 | # table schema.table (karr k57), each in a compare schema of its own | ||||||||||||||||||||||||||||||
| 210 | 3 | my $model = $self->_build_target_model_for({ local_only => 1 }); | |||||||||||||||||||||||||||||
| 211 | 1 | for my $schema_name ($self->_qualified_schemas) { | |||||||||||||||||||||||||||||
| 212 | 0 | my $other = $self->_build_target_model_for({ only_schema => $schema_name }); | |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | my %declared = map { $_->[0] =~ /\A\Q$schema_name\E\.(.*)\z/s ? ($1 => 1) : () } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 214 | DBIO::DB2::DDL->qualified_tables($self->schema); | ||||||||||||||||||||||||||||||
| 215 | 0 | $self->_merge_schema_model($model, $other, $schema_name, \%declared); | |||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||
| 217 | 1 | return $model; | |||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||
| 220 | # Tear down the throwaway compare schema. DB2's DROP SCHEMA ... RESTRICT only | ||||||||||||||||||||||||||||||
| 221 | # succeeds on an EMPTY schema, but by this point the schema holds the deployed | ||||||||||||||||||||||||||||||
| 222 | # tables, so the schema must be emptied first. SYSPROC.ADMIN_DROP_SCHEMA can drop | ||||||||||||||||||||||||||||||
| 223 | # a populated schema, but it requires the SYSTOOLSPACE tablespace, which does not | ||||||||||||||||||||||||||||||
| 224 | # exist on a stock DB2 instance (SQL0443N "... SYSTOOLSPACE tablespace does not | ||||||||||||||||||||||||||||||
| 225 | # exist"); when it is missing the call fails, the schema is NOT dropped, and the | ||||||||||||||||||||||||||||||
| 226 | # next CREATE SCHEMA in the same process collides (SQL0601N). So we tear the | ||||||||||||||||||||||||||||||
| 227 | # schema down with plain DDL + catalog lookups, robust on any DB2: drop the FK | ||||||||||||||||||||||||||||||
| 228 | # constraints first (so inter-table FK dependencies don't block table drops), | ||||||||||||||||||||||||||||||
| 229 | # then the tables (indexes drop with their table) and any views, then the now | ||||||||||||||||||||||||||||||
| 230 | # empty schema. Best-effort throughout -- each statement is its own eval so one | ||||||||||||||||||||||||||||||
| 231 | # failure does not abort teardown; the caller re-raises only the model-build | ||||||||||||||||||||||||||||||
| 232 | # error (when no model was produced). Catalog names match $test_schema verbatim: | ||||||||||||||||||||||||||||||
| 233 | # it is the uppercase DBIO_TEST_<pid>, which is how SYSCAT stores it -- so the | ||||||||||||||||||||||||||||||
| 234 | # schema prefix stays bare, but the object and constraint names DO NOT: since | ||||||||||||||||||||||||||||||
| 235 | # karr k28 they are created quoted and SYSCAT hands them back lowercase, and a | ||||||||||||||||||||||||||||||
| 236 | # bare DROP TABLE dbio_test_1.author folds to AUTHOR, misses, and fails under | ||||||||||||||||||||||||||||||
| 237 | # the eval without a sound. Every catalog-read name is therefore requoted | ||||||||||||||||||||||||||||||
| 238 | # through _db2_quote_ident before it goes back into DDL (karr k29). | ||||||||||||||||||||||||||||||
| 239 | 2 | ▶ | sub _drop_test_schema { | ||||||||||||||||||||||||||||
|
Subroutine
_drop_test_schema: called CC=7 SCAR=24.1
| |||||||||||||||||||||||||||||||
| 240 | 2 | my ($self, $dbh, $test_schema, $model) = @_; | |||||||||||||||||||||||||||||
| 241 | 2 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; # DBD::DB2 upper-cases hashref keys | |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||
| 243 | # 1. Drop FK constraints owned by tables in the schema (type 'F'), so the | ||||||||||||||||||||||||||||||
| 244 | # referenced tables can then be dropped in any order. | ||||||||||||||||||||||||||||||
| 245 | 2 | my $fks = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||||
| 246 | q{SELECT tabname, constname FROM syscat.tabconst WHERE tabschema = ? AND type = 'F'}, | ||||||||||||||||||||||||||||||
| 247 | { Slice => {} }, $test_schema, | ||||||||||||||||||||||||||||||
| 248 | ); | ||||||||||||||||||||||||||||||
| 249 | 2 | ▶ | for my $fk (@{ $fks || [] }) { | ||||||||||||||||||||||||||||
|
Condition50%1/2
$fks || []
MC/DC0%0/1
$fks || []
$fks
Truth table50%1/2
A
A= $fks
| |||||||||||||||||||||||||||||||
| 250 | 0 | eval { | |||||||||||||||||||||||||||||
| 251 | 0 | local $dbh->{PrintError} = 0; | |||||||||||||||||||||||||||||
| 252 | 0 | my $tbl = _db2_quote_ident($fk->{tabname}); | |||||||||||||||||||||||||||||
| 253 | 0 | my $name = _db2_quote_ident($fk->{constname}); | |||||||||||||||||||||||||||||
| 254 | 0 | $dbh->do("ALTER TABLE $test_schema.$tbl DROP FOREIGN KEY $name"); | |||||||||||||||||||||||||||||
| 255 | }; | ||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||
| 258 | # 2. Drop tables (indexes drop with them) and any views. | ||||||||||||||||||||||||||||||
| 259 | 2 | my $tables = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||||
| 260 | q{SELECT tabname FROM syscat.tables WHERE tabschema = ? AND type = 'T'}, | ||||||||||||||||||||||||||||||
| 261 | { Slice => {} }, $test_schema, | ||||||||||||||||||||||||||||||
| 262 | ); | ||||||||||||||||||||||||||||||
| 263 | 2 | ▶ | for my $t (@{ $tables || [] }) { | ||||||||||||||||||||||||||||
|
Condition50%1/2
$tables || []
MC/DC0%0/1
$tables || []
$tables
Truth table50%1/2
A
A= $tables
| |||||||||||||||||||||||||||||||
| 264 | 0 | eval { local $dbh->{PrintError} = 0; | |||||||||||||||||||||||||||||
| 265 | 0 | $dbh->do("DROP TABLE $test_schema." . _db2_quote_ident($t->{tabname})) }; | |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||
| 267 | 2 | my $views = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||||
| 268 | q{SELECT tabname FROM syscat.tables WHERE tabschema = ? AND type = 'V'}, | ||||||||||||||||||||||||||||||
| 269 | { Slice => {} }, $test_schema, | ||||||||||||||||||||||||||||||
| 270 | ); | ||||||||||||||||||||||||||||||
| 271 | 2 | ▶ | for my $v (@{ $views || [] }) { | ||||||||||||||||||||||||||||
|
Condition50%1/2
$views || []
MC/DC0%0/1
$views || []
$views
Truth table50%1/2
A
A= $views
| |||||||||||||||||||||||||||||||
| 272 | 0 | eval { local $dbh->{PrintError} = 0; | |||||||||||||||||||||||||||||
| 273 | 0 | $dbh->do("DROP VIEW $test_schema." . _db2_quote_ident($v->{tabname})) }; | |||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||
| 276 | # 3. Drop the now-empty schema. | ||||||||||||||||||||||||||||||
| 277 | 2 | eval { local $dbh->{PrintError} = 0; $dbh->do("DROP SCHEMA $test_schema RESTRICT") }; | |||||||||||||||||||||||||||||
| 278 | 2 | return; | |||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||
| 281 | # One object name as DBIO::DB2::DDL emits it: a double-quoted identifier | ||||||||||||||||||||||||||||||
| 282 | # (embedded `"` doubled), or -- for a hand-fed script that predates the | ||||||||||||||||||||||||||||||
| 283 | # always-quote change -- a bare word. | ||||||||||||||||||||||||||||||
| 284 | 34 | my $IDENT_RE = qr/"[^"]*(?:""[^"]*)*"|\w+/; | |||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||
| 286 | # Internal: rewrite the object positions of the statements DBIO::DB2::DDL | ||||||||||||||||||||||||||||||
| 287 | # emits (CREATE/DROP TABLE, CREATE/DROP INDEX, CREATE VIEW, and the inline | ||||||||||||||||||||||||||||||
| 288 | # REFERENCES target) so they name $schema instead of the live one. The | ||||||||||||||||||||||||||||||
| 289 | # regex pass is conservative -- DBIO::DB2::DDL emits a known shape -- | ||||||||||||||||||||||||||||||
| 290 | # so we trade a small fragility for not duplicating DDL rendering. | ||||||||||||||||||||||||||||||
| 291 | # | ||||||||||||||||||||||||||||||
| 292 | # That shape ALWAYS quotes its identifiers since karr k28, so the object name | ||||||||||||||||||||||||||||||
| 293 | # matched here must be $IDENT_RE, not a bare \w+: `\w+` cannot match | ||||||||||||||||||||||||||||||
| 294 | # CREATE TABLE "author", so every substitution silently did nothing and the | ||||||||||||||||||||||||||||||
| 295 | # whole compare DDL ran unqualified against CURRENT SCHEMA -- i.e. into the | ||||||||||||||||||||||||||||||
| 296 | # LIVE database (karr k29). CREATE UNIQUE INDEX is matched too; it was never | ||||||||||||||||||||||||||||||
| 297 | # covered by the CREATE INDEX branch. The qualified form is | ||||||||||||||||||||||||||||||
| 298 | # DBIO_TEST_<pid>."<name>": $test_schema stays bare because it is the | ||||||||||||||||||||||||||||||
| 299 | # uppercase name CREATE SCHEMA created and SYSCAT stores, while the object | ||||||||||||||||||||||||||||||
| 300 | # keeps the exact quoted case _drop_test_schema and the introspector read | ||||||||||||||||||||||||||||||
| 301 | # back out of SYSCAT. | ||||||||||||||||||||||||||||||
| 302 | # | ||||||||||||||||||||||||||||||
| 303 | # CREATE VIEW is rewritten in its NAME position only. Its body is the user's | ||||||||||||||||||||||||||||||
| 304 | # view_definition -- arbitrary SQL this pass has no business parsing -- and is | ||||||||||||||||||||||||||||||
| 305 | # passed through verbatim; the compare deploy points CURRENT SCHEMA at | ||||||||||||||||||||||||||||||
| 306 | # $test_schema instead, so the body resolves against the compare tables. Both | ||||||||||||||||||||||||||||||
| 307 | # halves are needed: the rewrite alone would aim a live-resolving body at a | ||||||||||||||||||||||||||||||
| 308 | # compare-schema view (karr k35); the DESCRIPTION section "Views -- the compare | ||||||||||||||||||||||||||||||
| 309 | # deploy also moves CURRENT SCHEMA" has the whole argument. DROP VIEW is | ||||||||||||||||||||||||||||||
| 310 | # deliberately absent: neither install_ddl nor the diff path emits one, and this | ||||||||||||||||||||||||||||||
| 311 | # pass grows only with the shapes that exist. | ||||||||||||||||||||||||||||||
| 312 | # | ||||||||||||||||||||||||||||||
| 313 | # The inline FK clause from _fk_constraint_clause is | ||||||||||||||||||||||||||||||
| 314 | # CONSTRAINT <name> FOREIGN KEY (<from>) REFERENCES <reftable>(<cols>) | ||||||||||||||||||||||||||||||
| 315 | # where <reftable> is UNQUALIFIED. Without rewriting it too, a child table | ||||||||||||||||||||||||||||||
| 316 | # deployed into the test schema would resolve its REFERENCES against CURRENT | ||||||||||||||||||||||||||||||
| 317 | # SCHEMA (the live schema) and cross-reference the live parent. So we also | ||||||||||||||||||||||||||||||
| 318 | # qualify the REFERENCES target to the test schema. The CONSTRAINT name itself | ||||||||||||||||||||||||||||||
| 319 | # is schema-scoped in DB2 (it lives in the test table's schema), so it cannot | ||||||||||||||||||||||||||||||
| 320 | # collide with the live constraint of the same name and is left untouched. | ||||||||||||||||||||||||||||||
| 321 | 7 | ▶ | sub _split_qualify_ddl { | ||||||||||||||||||||||||||||
|
Subroutine
_split_qualify_ddl: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||
| 322 | 7 | my ($self, $ddl, $test_schema) = @_; | |||||||||||||||||||||||||||||
| 323 | # A table named "schema.table" lives in a schema of its own and is no part of the | ||||||||||||||||||||||||||||||
| 324 | # model the diff compares (it reads one schema): its CREATE TABLE and the | ||||||||||||||||||||||||||||||
| 325 | # CREATE INDEX on it stay out of the compare deploy (karr k57). | ||||||||||||||||||||||||||||||
| 326 | my @stmts = grep { | ||||||||||||||||||||||||||||||
| 327 | 7 | ▶ | !/\A\s*CREATE\s+TABLE\s+$IDENT_RE\s*\.\s*$IDENT_RE/ | ||||||||||||||||||||||||||||
|
Condition100%3/3
/\A\s*CREATE\s+TABLE\s+$IDENT_RE\s*\.\s*$IDENT_RE/ || /\A\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+$IDENT_RE(?:\s*\.\s*$IDENT_RE)?\s+ON\s+$IDENT_RE\s*\.\s*$IDENT_RE/
MC/DC100%2/2
/\A\s*CREATE\s+TABLE\s+$IDENT_RE\s*\.\s*$IDENT_RE/ || /\A\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+$IDENT_RE(?:\s*\.\s*$IDENT_RE)?\s+ON\s+$IDENT_RE\s*\.\s*$IDENT_RE/
/\A\s*CREATE\s+TABLE\s+$IDENT_RE\s*\.\s*$IDENT_RE/
/\A\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+$IDENT_RE(?:\s*\.\s*$IDENT_RE)?\s+ON\s+$IDENT_RE\s*\.\s*$IDENT_RE/
Truth table100%3/3
A || B
A= /\A\s*CREATE\s+TABLE\s+$IDENT_RE\s*\.\s*$IDENT_RE/B= /\A\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+$IDENT_RE(?:\s*\.\s*$IDENT_RE)?\s+ON\s+$IDENT_RE\s*\.\s*$IDENT_RE/
| |||||||||||||||||||||||||||||||
| 328 | && !/\A\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+$IDENT_RE(?:\s*\.\s*$IDENT_RE)?\s+ON\s+$IDENT_RE\s*\.\s*$IDENT_RE/ | ||||||||||||||||||||||||||||||
| 329 | } DBIO::SQL::Util::_split_statements($ddl); | ||||||||||||||||||||||||||||||
| 330 | 7 | for my $stmt (@stmts) { | |||||||||||||||||||||||||||||
| 331 | 21 | $stmt =~ s/\b(CREATE\s+TABLE)\s+($IDENT_RE)/$1 $test_schema.$2/g; | |||||||||||||||||||||||||||||
| 332 | 21 | $stmt =~ s/\b(DROP\s+TABLE)\s+($IDENT_RE)/$1 $test_schema.$2/g; | |||||||||||||||||||||||||||||
| 333 | 21 | $stmt =~ s/\b(CREATE\s+(?:UNIQUE\s+)?INDEX)\s+($IDENT_RE)\s+ON\s+($IDENT_RE)/$1 $test_schema.$2 ON $test_schema.$3/g; | |||||||||||||||||||||||||||||
| 334 | 21 | $stmt =~ s/\b(DROP\s+INDEX)\s+($IDENT_RE)/$1 $test_schema.$2/g; | |||||||||||||||||||||||||||||
| 335 | 21 | $stmt =~ s/\b(CREATE\s+VIEW)\s+($IDENT_RE)/$1 $test_schema.$2/g; | |||||||||||||||||||||||||||||
| 336 | 21 | $stmt =~ s/\bREFERENCES\s+($IDENT_RE)\s*\(/REFERENCES $test_schema.$1(/g; | |||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||
| 338 | 7 | return @stmts; | |||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||
| 342 | 32 | ▶ | sub _check_before_apply { | ||||||||||||||||||||||||||||
|
Subroutine
_check_before_apply: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 343 | 32 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||||
| 344 | 32 | $self->SUPER::_check_before_apply($diff); | |||||||||||||||||||||||||||||
| 345 | 32 | $self->_reorg_pending_before_apply($diff); | |||||||||||||||||||||||||||||
| 346 | 32 | $self->_refuse_identity_on_rows($diff); | |||||||||||||||||||||||||||||
| 347 | 27 | $self->_refuse_unfillable($diff); | |||||||||||||||||||||||||||||
| 348 | 22 | return; | |||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||
| 351 | # Croaks, naming table and column, for every identity column the diff adds to a | ||||||||||||||||||||||||||||||
| 352 | # table that holds a row. Asks once per table, in name order. Private. | ||||||||||||||||||||||||||||||
| 353 | 32 | ▶ | sub _refuse_identity_on_rows { | ||||||||||||||||||||||||||||
|
Subroutine
_refuse_identity_on_rows: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 354 | 32 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||
| 356 | 32 | my %columns = $self->_identity_add_columns($diff); | |||||||||||||||||||||||||||||
| 357 | 32 | my @tables = keys %columns; | |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||
| 359 | 32 | ▶ | my $dbh = @tables ? $self->_dbh : undef; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 360 | 32 | my @refused; | |||||||||||||||||||||||||||||
| 361 | 32 | for my $table (sort @tables) { | |||||||||||||||||||||||||||||
| 362 | 12 | my ($row) = $dbh->selectrow_array( | |||||||||||||||||||||||||||||
| 363 | sprintf 'SELECT 1 FROM %s FETCH FIRST 1 ROW ONLY', _db2_quote_table($table)); | ||||||||||||||||||||||||||||||
| 364 | 12 | ▶ | push @refused, $table if $row; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||
| 366 | Carp::croak(join "\n", map { | ||||||||||||||||||||||||||||||
| 367 | 32 | ▶ | my $t = $_; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 368 | sprintf 'Cannot add identity column%s %s to table "%s": the table holds rows and Db2 would ' | ||||||||||||||||||||||||||||||
| 369 | . 'give every one of them the value 0 (ADD COLUMN ... NOT NULL DEFAULT 0, then SET GENERATED; ' | ||||||||||||||||||||||||||||||
| 370 | . 'ADR 0012). Add the column while the table is empty, or add it plain and make it an identity ' | ||||||||||||||||||||||||||||||
| 371 | . 'after the rows have their values; nothing has been executed', | ||||||||||||||||||||||||||||||
| 372 | 5 | ▶ | (@{ $columns{$t} } > 1 ? 's' : ''), join(', ', map { qq{"$_"} } @{ $columns{$t} }), $t; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 373 | } @refused) if @refused; | ||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||
| 375 | 27 | return; | |||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||
| 378 | # Throws a DBIO::Exception (a refusal, naming table and column) for what the | ||||||||||||||||||||||||||||||
| 379 | # server would refuse in the middle of the apply and Db2 cannot be given a value | ||||||||||||||||||||||||||||||
| 380 | # for (karr k57). Nothing has been executed. Private. | ||||||||||||||||||||||||||||||
| 381 | 27 | ▶ | sub _refuse_unfillable { | ||||||||||||||||||||||||||||
|
Subroutine
_refuse_unfillable: called CC=8 SCAR=21.8
| |||||||||||||||||||||||||||||||
| 382 | 27 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||
| 384 | 27 | ▶ | my $source = $diff->source->{tables} // {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$diff->source->{"tables"} // {}
MC/DC0%0/1
$diff->source->{"tables"} // {}
$diff->source->{"tables"}
Truth table50%1/2
A
A= $diff->source->{"tables"}
| |||||||||||||||||||||||||||||||
| 385 | 35 | ▶ | my @ops = grep { $_->isa('DBIO::DB2::Diff::Column') && exists $source->{ $_->table_name } } | ||||||||||||||||||||||||||||
|
Condition66%2/3
$_->isa('DBIO::DB2::Diff::Column') && exists $$source{$_->table_name}
MC/DC50%1/2
$_->isa('DBIO::DB2::Diff::Column') && exists $$source{$_->table_name}
$_->isa('DBIO::DB2::Diff::Column')
exists $$source{$_->table_name}
Truth table66%2/3
A && B
A= $_->isa('DBIO::DB2::Diff::Column')B= exists $$source{$_->table_name}
| |||||||||||||||||||||||||||||||
| 386 | 27 | @{ $diff->operations }; | |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||
| 388 | 27 | for my $op (@ops) { | |||||||||||||||||||||||||||||
| 389 | 20 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 390 | 'DBIO::DB2: the upgrade is refused: cannot add the column "%s" to table "%s": it is NOT NULL ' | ||||||||||||||||||||||||||||||
| 391 | . 'without a default, which Db2 refuses in ALTER TABLE ... ADD COLUMN (SQL0193N) and no value ' | ||||||||||||||||||||||||||||||
| 392 | . 'is made up for the rows; give the column a default, or add it nullable; nothing has been executed', | ||||||||||||||||||||||||||||||
| 393 | $op->column_name, $op->table_name) if $op->adds_not_null_without_default; | ||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | for my $op (sort { $a->table_name cmp $b->table_name or $a->column_name cmp $b->column_name } | ||||||||||||||||||||||||||||
|
Condition0%0/3
$a->table_name cmp $b->table_name || $a->column_name cmp $b->column_name
MC/DC0%0/2
$a->table_name cmp $b->table_name || $a->column_name cmp $b->column_name
$a->table_name cmp $b->table_name
$a->column_name cmp $b->column_name
Truth table0%0/3
A || B
A= $a->table_name cmp $b->table_nameB= $a->column_name cmp $b->column_name
| |||||||||||||||||||||||||||||||
| 397 | 17 | grep { $_->sets_not_null } @ops) { | |||||||||||||||||||||||||||||
| 398 | 4 | my ($row) = $self->_dbh->selectrow_array(sprintf | |||||||||||||||||||||||||||||
| 399 | 'SELECT 1 FROM %s WHERE %s IS NULL FETCH FIRST 1 ROW ONLY', | ||||||||||||||||||||||||||||||
| 400 | _db2_quote_table($op->table_name), _db2_quote_ident($op->column_name)); | ||||||||||||||||||||||||||||||
| 401 | 4 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 402 | 'DBIO::DB2: the upgrade is refused: cannot make the column "%s" NOT NULL: table "%s" holds rows ' | ||||||||||||||||||||||||||||||
| 403 | . 'where it is NULL (SQL0407N) and no value is made up for them; fill the column first; ' | ||||||||||||||||||||||||||||||
| 404 | . 'nothing has been executed', | ||||||||||||||||||||||||||||||
| 405 | $op->column_name, $op->table_name) if $row; | ||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||
| 408 | 22 | return; | |||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||
| 411 | # table name => [ column names ] for every identity column the diff adds to a | ||||||||||||||||||||||||||||||
| 412 | # table that exists already (Diff::Column::adds_identity). Private. | ||||||||||||||||||||||||||||||
| 413 | 32 | ▶ | sub _identity_add_columns { | ||||||||||||||||||||||||||||
|
Subroutine
_identity_add_columns: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||
| 414 | 32 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||||
| 415 | 32 | ▶ | my $source = $diff->source->{tables} // {}; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$diff->source->{"tables"} // {}
MC/DC0%0/1
$diff->source->{"tables"} // {}
$diff->source->{"tables"}
Truth table50%1/2
A
A= $diff->source->{"tables"}
| |||||||||||||||||||||||||||||||
| 416 | 32 | my %columns; | |||||||||||||||||||||||||||||
| 417 | 32 | for my $op (@{ $diff->operations }) { | |||||||||||||||||||||||||||||
| 418 | 42 | ▶ | next unless $op->isa('DBIO::DB2::Diff::Column') && $op->adds_identity; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op->isa("DBIO::DB2::Diff::Column") and $op->adds_identity
MC/DC100%2/2
$op->isa("DBIO::DB2::Diff::Column") and $op->adds_identity
$op->isa("DBIO::DB2::Diff::Column")
$op->adds_identity
Truth table100%3/3
A and B
A= $op->isa("DBIO::DB2::Diff::Column")B= $op->adds_identity
| |||||||||||||||||||||||||||||||
| 419 | 13 | ▶ | next unless exists $source->{ $op->table_name }; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 420 | 13 | push @{ $columns{ $op->table_name } }, $op->column_name; | |||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||
| 422 | 32 | return %columns; | |||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||
| 425 | # Runs the REORG for every table of both models of $diff that the server holds | ||||||||||||||||||||||||||||||
| 426 | # reorg-pending (see "_check_before_apply"). Private: not an interface. | ||||||||||||||||||||||||||||||
| 427 | 32 | ▶ | sub _reorg_pending_before_apply { | ||||||||||||||||||||||||||||
|
Subroutine
_reorg_pending_before_apply: called CC=10 SCAR=23.6
| |||||||||||||||||||||||||||||||
| 428 | 32 | my ($self, $diff) = @_; | |||||||||||||||||||||||||||||
| 429 | 32 | ▶ | my ($source, $target) = ($diff->source->{tables} // {}, $diff->target->{tables} // {}); | ||||||||||||||||||||||||||||
|
Condition50%2/4
$diff->source->{"tables"} // {}
$diff->target->{"tables"} // {}
MC/DC0%0/1
$diff->source->{"tables"} // {}
$diff->source->{"tables"}
MC/DC0%0/1
$diff->target->{"tables"} // {}
$diff->target->{"tables"}
Truth table50%1/2
A
A= $diff->source->{"tables"}
Truth table50%1/2
A
A= $diff->target->{"tables"}
| |||||||||||||||||||||||||||||||
| 430 | my @common = grep { | ||||||||||||||||||||||||||||||
| 431 | ($source->{$_}{kind} // 'table') eq 'table' | ||||||||||||||||||||||||||||||
| 432 | 37 | ▶ | && ($target->{$_}{kind} // 'table') eq 'table' | ||||||||||||||||||||||||||||
|
Condition57%4/7
($source->{$_}{'kind'} // 'table') eq 'table' && ($target->{$_}{'kind'} // 'table') eq 'table'
$source->{$_}{"kind"} // "table"
$target->{$_}{"kind"} // "table"
MC/DC0%0/2
($source->{$_}{'kind'} // 'table') eq 'table' && ($target->{$_}{'kind'} // 'table') eq 'table'
$source->{$_}{"kind"}
$target->{$_}{"kind"}
Truth table33%1/3
A && B
A= $source->{$_}{"kind"}B= $target->{$_}{"kind"}
| |||||||||||||||||||||||||||||||
| 433 | 32 | ▶ | } grep { exists $target->{$_} && !defined +(_split_table_name($_))[0] } keys %$source; | ||||||||||||||||||||||||||||
|
Condition66%2/3
exists $target->{$_} && !defined((_split_table_name($_))[0])
MC/DC50%1/2
exists $target->{$_} && !defined((_split_table_name($_))[0])
exists $target->{$_}
!defined((_split_table_name($_))[0])
Truth table66%2/3
A && not(B)
A= exists $target->{$_}B= !defined((_split_table_name($_))[0])
| |||||||||||||||||||||||||||||||
| 434 | 32 | ▶ | return unless @common; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||
| 436 | 31 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||||
| 437 | 31 | my $schema = $self->_current_schema($dbh); | |||||||||||||||||||||||||||||
| 438 | 31 | ▶ | my $pending = $dbh->selectcol_arrayref( | ||||||||||||||||||||||||||||
|
Condition50%1/2
$dbh->selectcol_arrayref(q[SELECT tabname FROM sysibmadm.admintabinfo WHERE tabschema = ? AND reorg_pending = 'Y'], undef, $schema) || []
MC/DC0%0/1
$dbh->selectcol_arrayref(q[SELECT tabname FROM sysibmadm.admintabinfo WHERE tabschema = ? AND reorg_pending = 'Y'], undef, $schema) || []
$dbh->selectcol_arrayref(q[SELECT tabname FROM sysibmadm.admintabinfo WHERE tabschema = ? AND reorg_pending = 'Y'], undef, $schema)
Truth table50%1/2
A
A= $dbh->selectcol_arrayref(q[SELECT tabname FROM sysibmadm.admintabinfo WHERE tabschema = ? AND reorg_pending = 'Y'], undef, $schema)
| |||||||||||||||||||||||||||||||
| 439 | q{SELECT tabname FROM sysibmadm.admintabinfo WHERE tabschema = ? AND reorg_pending = 'Y'}, | ||||||||||||||||||||||||||||||
| 440 | undef, $schema, | ||||||||||||||||||||||||||||||
| 441 | ) || []; | ||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||
| 443 | 31 | my %pending = map { ($_ => 1) } @$pending; | |||||||||||||||||||||||||||||
| 444 | 31 | my @tables = sort grep { $pending{$_} } @common; | |||||||||||||||||||||||||||||
| 445 | 31 | ▶ | return unless @tables; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||
| 447 | 10 | Carp::carp(ref($self) . ': reorg-pending table(s) found before the upgrade, REORG run first: ' | |||||||||||||||||||||||||||||
| 448 | . join(', ', @tables)); | ||||||||||||||||||||||||||||||
| 449 | $self->_execute_ddl($dbh, join "\n", map { | ||||||||||||||||||||||||||||||
| 450 | 10 | DBIO::DB2::Diff::Reorg->new(action => 'reorg', table_name => $_)->as_sql | |||||||||||||||||||||||||||||
| 451 | } @tables); | ||||||||||||||||||||||||||||||
| 452 | 10 | return; | |||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||
| 456 | 34 | 1; | |||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||
| 458 | __END__ | ||||||||||||||||||||||||||||||