« blib/lib/DBIO/PostgreSQL/Diff/Function.pm
↑ Summary
blib/lib/DBIO/PostgreSQL/Introspect/OutsideReaders.pm »
| 1 | package DBIO::PostgreSQL::Diff::Index; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for PostgreSQL indexes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 107 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 107 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 107 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 107 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 107 | ▶ | use DBIO::PostgreSQL::Diff::CaseOnly (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 107 | ▶ | use DBIO::PostgreSQL::Diff::NoStatement (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 107 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 107 | ▶ | use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified deferrable_clause); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 107 | ▶ | use bytes (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 107 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | table_key schema_name table_name index_name index_info replaced via renamed_from partitions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | )); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Every index of one model section, as { schema => { index name => entry } }. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 252 | ▶ | sub _by_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_by_schema: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 252 | my ($section, $tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 252 | my %by_schema; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 252 | for my $table_key (keys %$section) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 213 | for my $idx_name (keys %{ $section->{$table_key} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 225 | my $info = $section->{$table_key}{$idx_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | my ($table_name, $schema_name) = reverse DBIO::PostgreSQL::Introspect->key_names( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 225 | $table_key, table_name => $info, $tables->{$table_key}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 225 | ▶ | $by_schema{ $schema_name // '' }{$idx_name} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$schema_name // ""
MC/DC0%0/1
$schema_name // ""
$schema_name
Truth table50%1/2
A
A= $schema_name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | table_key => $table_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | schema_name => $schema_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | table_name => $table_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | index_name => $idx_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | index_info => $info, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 252 | return \%by_schema; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # The entries of _by_schema in the order the operations come out: by index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # name, then by schema. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 360 | ▶ | sub _in_order { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_in_order: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 360 | my ($by_schema) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 34 | ▶ | sort { $a->{index_name} cmp $b->{index_name} or ($a->{schema_name} // '') cmp ($b->{schema_name} // '') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition57%4/7
$DBIO::PostgreSQL::Diff::Index::a->{'index_name'} cmp $DBIO::PostgreSQL::Diff::Index::b->{'index_name'} || ($DBIO::PostgreSQL::Diff::Index::a->{'schema_name'} // '') cmp ($DBIO::PostgreSQL::Diff::Index::b->{'schema_name'} // '')
$DBIO::PostgreSQL::Diff::Index::a->{"schema_name"} // ""
$DBIO::PostgreSQL::Diff::Index::b->{"schema_name"} // ""
MC/DC0%0/2
$DBIO::PostgreSQL::Diff::Index::a->{'index_name'} cmp $DBIO::PostgreSQL::Diff::Index::b->{'index_name'} || ($DBIO::PostgreSQL::Diff::Index::a->{'schema_name'} // '') cmp ($DBIO::PostgreSQL::Diff::Index::b->{'schema_name'} // '')
$DBIO::PostgreSQL::Diff::Index::a->{'index_name'} cmp $DBIO::PostgreSQL::Diff::Index::b->{'index_name'}
$DBIO::PostgreSQL::Diff::Index::a->{"schema_name"}
MC/DC0%0/1
$DBIO::PostgreSQL::Diff::Index::b->{"schema_name"} // ""
$DBIO::PostgreSQL::Diff::Index::b->{"schema_name"}
Truth table66%2/3
A || B
A= $DBIO::PostgreSQL::Diff::Index::a->{'index_name'} cmp $DBIO::PostgreSQL::Diff::Index::b->{'index_name'}B= $DBIO::PostgreSQL::Diff::Index::a->{"schema_name"}
Truth table50%1/2
A
A= $DBIO::PostgreSQL::Diff::Index::b->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 360 | map { values %$_ } values %$by_schema; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # The entries of _by_schema as DBIO::PostgreSQL::Diff::CaseOnly compares them: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # a name among the indexes of its table. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 252 | ▶ | sub _named { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_named: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 252 | my ($by_schema) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | return [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 225 | ▶ | map { { %$_, in => [ $_->{schema_name} // '', $_->{table_key} ], name => $_->{index_name} } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"schema_name"} // ""
MC/DC0%0/1
$_->{"schema_name"} // ""
$_->{"schema_name"}
Truth table50%1/2
A
A= $_->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 252 | map { values %$_ } values %$by_schema | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 107 | my %CONSTRAINT_KIND = (p => 'PRIMARY KEY', u => 'UNIQUE', x => 'EXCLUDE'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # The constraint an index stands behind, as the model says it: ($name, $type) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # with the type p, u or x, or an empty list for an index without one. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # primary key's index is the constraint's, under the same name, whether or not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # the entry has the keys (a model built by hand may not). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 732 | ▶ | sub _constraint { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_constraint: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 732 | my ($info, $index_name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | return ($info->{constraint_name}, $info->{constraint_type}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 732 | ▶ | if defined $info->{constraint_name} && defined $info->{constraint_type}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"constraint_name"} and defined $info->{"constraint_type"}
MC/DC50%1/2
defined $info->{"constraint_name"} and defined $info->{"constraint_type"}
defined $info->{"constraint_name"}
defined $info->{"constraint_type"}
Truth table66%2/3
A and B
A= defined $info->{"constraint_name"}B= defined $info->{"constraint_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 257 | ▶ | return ($index_name, 'p') if $info->{is_primary} && defined $index_name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"is_primary"} and defined $index_name
MC/DC50%1/2
$info->{"is_primary"} and defined $index_name
$info->{"is_primary"}
defined $index_name
Truth table66%2/3
A and B
A= $info->{"is_primary"}B= defined $index_name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 254 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # An index to create beside one to drop, on the same table, that is the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # name but for its case -- where the one to drop stands behind a constraint: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # the drop takes the constraint, and the diff creates no constraint to put in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # its place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 126 | ▶ | sub _refuse_case_only { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refuse_case_only: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 126 | my ($src_indexes, $tgt_indexes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | DBIO::PostgreSQL::Diff::CaseOnly->refuse( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | "The index to drop is the index of a constraint. PostgreSQL does not drop it by DROP\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | . "INDEX, and dropping the constraint builds the index again under the new name,\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | . 'where a rename keeps it.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | my ($old, $new) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | my @schema = grep { defined } $old->{schema_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | kind => 'index', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | database => join('.', @schema, $old->{name}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | target => join('.', @schema, $new->{name}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | rename => sprintf('ALTER INDEX %s RENAME TO %s;', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | quote_qualified(@schema, $old->{name}), quote_ident($new->{name})), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 126 | grep { my @constraint = _constraint(@{$_->[0]}{qw( index_info index_name )}); scalar @constraint } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | DBIO::PostgreSQL::Diff::CaseOnly->pairs(_named($src_indexes), _named($tgt_indexes)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # The definition of an index without its name: the text pg_get_indexdef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # writes, the name behind INDEX replaced. Two indexes with this text in common | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # are the same index under two names. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 126 | ▶ | sub _definition_without_name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_definition_without_name: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 126 | my ($definition) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 126 | ▶ | $definition //= ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$definition //= ""
MC/DC0%0/1
$definition //= ""
$definition
Truth table50%1/2
A
A= $definition
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 126 | $definition =~ s/\A(\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?)(?:"(?:[^"]|"")*"|\S+)(\s+ON\s)/$1\0$2/i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 126 | return $definition; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # The index as a reader calls it, in a message: its name and its table. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 47 | ▶ | sub _name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_name: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 47 | my ($index) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 47 | ▶ | return sprintf '%s on %s', $index->{index_name} // '', $index->{table_key} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$index->{"index_name"} // ""
$index->{"table_key"} // ""
MC/DC0%0/1
$index->{"index_name"} // ""
$index->{"index_name"}
MC/DC0%0/1
$index->{"table_key"} // ""
$index->{"table_key"}
Truth table50%1/2
A
A= $index->{"index_name"}
Truth table50%1/2
A
A= $index->{"table_key"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # Whether the index is one over NULLS NOT DISTINCT (PostgreSQL 15 and later). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # That is in no field of the model: pg_get_indexdef writes it behind the key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # list, and a constraint says it as UNIQUE NULLS NOT DISTINCT (cols). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 29 | ▶ | sub _nulls_not_distinct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_nulls_not_distinct: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 29 | my ($info) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 29 | ▶ | return ($info->{definition} // '') =~ /\)\s+NULLS\s+NOT\s+DISTINCT\b/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$info->{"definition"} // ""
MC/DC0%0/1
$info->{"definition"} // ""
$info->{"definition"}
Truth table50%1/2
A
A= $info->{"definition"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # The timing of a UNIQUE constraint: its DEFERRABLE clause, with the space that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # leads it, or nothing. Only a UNIQUE constraint carries it in the model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # (karr k147); a primary key's is not read. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 96 | ▶ | sub _timing { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_timing: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 96 | my ($info, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 96 | ▶ | return '' unless ($type // '') eq 'u'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$type // ""
MC/DC100%1/1
$type // ""
$type
Truth table100%2/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 58 | return deferrable_clause($info->{deferrable}, $info->{initially_deferred}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # The constraint clause of the target, as far as the model carries it: what | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # the diff writes for a constraint it creates, and, where it cannot write it, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | # what it tells the reader. The elements of an EXCLUDE constraint are not in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # the model. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 38 | ▶ | sub _add_constraint_sql { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_constraint_sql: called CC=11 SCAR=24.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 38 | my ($info, $name, $type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 38 | ▶ | my @columns = @{ $info->{columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"columns"} // []
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | return sprintf 'ADD CONSTRAINT %s %s (...)', quote_ident($name), $CONSTRAINT_KIND{$type} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 38 | ▶ | if $type eq 'x' || !@columns || grep { !defined } @columns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$type eq "x" or not @columns or grep {not defined $_;} @columns
$type eq "x" or not @columns
MC/DC33%1/3
$type eq "x" or not @columns or grep {not defined $_;} @columns
$type eq "x"
not @columns
grep {not defined $_;} @columns
Truth table50%2/4
A or not(B) or C
A= $type eq "x"B= not @columnsC= grep {not defined $_;} @columns
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 37 | ▶ | my @include = @{ $info->{include_columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"include_columns"} // []
MC/DC0%0/1
$info->{"include_columns"} // []
$info->{"include_columns"}
Truth table50%1/2
A
A= $info->{"include_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 37 | ▶ | my %with = %{ $info->{storage_params} // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"storage_params"} // {}
MC/DC0%0/1
$info->{"storage_params"} // {}
$info->{"storage_params"}
Truth table50%1/2
A
A= $info->{"storage_params"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | return sprintf 'ADD CONSTRAINT %s %s%s (%s)%s%s%s', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | quote_ident($name), $CONSTRAINT_KIND{$type}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | ($type eq 'u' && _nulls_not_distinct($info) ? ' NULLS NOT DISTINCT' : ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 45 | ▶ | join(', ', map { quote_ident($_) } @columns), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | ▶ | (@include ? ' INCLUDE (' . join(', ', map { quote_ident($_) } @include) . ')' : ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 37 | ▶ | (%with ? ' WITH (' . join(', ', map { "$_ = $with{$_}" } sort keys %with) . ')' : ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "u" and _nulls_not_distinct($info)
MC/DC100%2/2
$type eq "u" and _nulls_not_distinct($info)
$type eq "u"
_nulls_not_distinct($info)
Truth table100%3/3
A and B
A= $type eq "u"B= _nulls_not_distinct($info)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | _timing($info, $type); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # Whether the source index and the target index differ in what the diff | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # compares besides the text of the definition: the constraint behind it (kind), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # the key columns in the order of the key, the INCLUDE columns, the storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # parameters. The text carries the columns, the INCLUDE list and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # parameters as well; it does not carry the constraint (karr k139). Whether the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # index is valid is not compared: the index of a partitioned table is created | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # from its text, ON ONLY, and is invalid until its partitions have theirs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # -- the diff would drop and create it again for ever. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 33 | ▶ | sub _attributes_differ { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attributes_differ: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 33 | my ($src, $tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 33 | my ($s, $t) = map { $_->{index_info} } $src, $tgt; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 33 | my (undef, $stype) = _constraint($s, $src->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 33 | my (undef, $ttype) = _constraint($t, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 33 | ▶ | return 1 if ($stype // '') ne ($ttype // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
$stype // ""
$ttype // ""
MC/DC100%1/1
$stype // ""
$stype
MC/DC100%1/1
$ttype // ""
$ttype
Truth table100%2/2
A
A= $stype
Truth table100%2/2
A
A= $ttype
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 18 | ▶ | return 1 if _timing($s, $stype) ne _timing($t, $ttype); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 15 | ▶ | my $list = sub { join "\0", map { $_ // "\1" } @{ $_[0] // [] } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Condition75%3/4
$_ // "\1"
$_[0] // []
MC/DC0%0/1
$_ // "\1"
$_
MC/DC100%1/1
$_[0] // []
$_[0]
Truth table50%1/2
A
A= $_
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 15 | ▶ | return 1 if $list->($s->{columns}) ne $list->($t->{columns}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 15 | ▶ | return 1 if $list->($s->{include_columns}) ne $list->($t->{include_columns}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | my $params = sub { my $p = $_[0] // {}; join "\0", map { "$_=$p->{$_}" } sort keys %$p }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Condition100%2/2
$_[0] // {}
MC/DC100%1/1
$_[0] // {}
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 15 | ▶ | return 1 if $params->($s->{storage_params}) ne $params->($t->{storage_params}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 15 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # The statement a reader runs by hand to put the constraint of $tgt in place of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # the index $src, where the diff does not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 10 | ▶ | sub _by_hand { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_by_hand: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 10 | my ($src, $tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 10 | my ($name, $type) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 10 | my @table = grep { defined } @{$tgt}{qw( schema_name table_name )}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 20 | ( $src ? sprintf(' DROP INDEX %s;', quote_qualified(grep { defined } @{$src}{qw( schema_name index_name )})) : () ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 10 | ▶ | sprintf(' ALTER TABLE %s %s;', quote_qualified(@table), _add_constraint_sql($tgt->{index_info}, $name, $type)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # Whether the foreign key $by, which points at a constraint that is to go, is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # gone before the constraint is: the target model declares foreign keys, has the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # table of the key, and either lacks the key or has it to other columns. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # drops of the keys come ahead of the drops of the indexes (DBIO::PostgreSQL:: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # Diff, ORDER OF OPERATIONS). A key the source model does not list -- the copy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # at a partition, which the introspection leaves out -- is not shown to be gone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 13 | ▶ | sub _foreign_key_goes { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_foreign_key_goes: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 13 | my ($by, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 13 | ▶ | my ($source_fks, $target_fks, $target_tables) = @{ $fk // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$fk // []
MC/DC0%0/1
$fk // []
$fk
Truth table50%1/2
A
A= $fk
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 13 | ▶ | return 0 unless $target_fks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 10 | my $key = DBIO::PostgreSQL::Introspect->qualified_key(@{$by}{qw( schema_name table_name )}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 10 | ▶ | return 0 unless $target_tables && exists $target_tables->{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$target_tables and exists $target_tables->{$key}
MC/DC50%1/2
$target_tables and exists $target_tables->{$key}
$target_tables
exists $target_tables->{$key}
Truth table66%2/3
A and B
A= $target_tablesB= exists $target_tables->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 9 | ▶ | my $named = sub { grep { ($_->{constraint_name} // '') eq $by->{constraint_name} } @{ $_[0]{$key} // [] } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Condition75%3/4
$_->{"constraint_name"} // ""
$_[0]{$key} // []
MC/DC0%0/1
$_->{"constraint_name"} // ""
$_->{"constraint_name"}
MC/DC100%1/1
$_[0]{$key} // []
$_[0]{$key}
Truth table50%1/2
A
A= $_->{"constraint_name"}
Truth table100%2/2
A
A= $_[0]{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 9 | ▶ | my ($old) = $named->($source_fks // {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source_fks // {}
MC/DC0%0/1
$source_fks // {}
$source_fks
Truth table50%1/2
A
A= $source_fks
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 9 | ▶ | return 0 unless $old; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 8 | my ($new) = $named->($target_fks); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 8 | ▶ | return 1 unless $new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 5 | ▶ | my $points = sub { join "\0", (map { $_[0]{$_} // '' } qw( remote_schema remote_table )), @{ $_[0]{remote_columns} // [] } }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
Condition50%2/4
$_[0]{$_} // ""
$_[0]{"remote_columns"} // []
MC/DC0%0/1
$_[0]{$_} // ""
$_[0]{$_}
MC/DC0%0/1
$_[0]{"remote_columns"} // []
$_[0]{"remote_columns"}
Truth table50%1/2
A
A= $_[0]{$_}
Truth table50%1/2
A
A= $_[0]{"remote_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 5 | ▶ | return $points->($old) ne $points->($new) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # Why the constraint behind $src cannot go: the lines that say it, with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # statements that settle it. Nothing when it can go -- as ALTER TABLE ... DROP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # CONSTRAINT, with the index. A foreign key that points at it is no reason when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # it is gone before the constraint is ($fk, see _foreign_key_goes); a constraint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # that is dropped and added again ($replaced) goes in the first half of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # operations, ahead of any drop of a key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 58 | ▶ | sub _drop_reasons { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_drop_reasons: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 58 | my ($src, $replaced, $fk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 58 | my $info = $src->{index_info}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 58 | ▶ | my @by = grep { $replaced || !_foreign_key_goes($_, $fk) } @{ $info->{referenced_by} // [] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$replaced || !_foreign_key_goes($_, $fk)
$info->{"referenced_by"} // []
MC/DC100%2/2
$replaced || !_foreign_key_goes($_, $fk)
$replaced
!_foreign_key_goes($_, $fk)
MC/DC100%1/1
$info->{"referenced_by"} // []
$info->{"referenced_by"}
Truth table100%3/3
A || not(B)
A= $replacedB= !_foreign_key_goes($_, $fk)
Truth table100%2/2
A
A= $info->{"referenced_by"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 'foreign keys of other tables point at it, and ALTER TABLE ... DROP CONSTRAINT would fail:', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 12 | sprintf ' ALTER TABLE %s DROP CONSTRAINT %s;', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 12 | quote_qualified(@{$_}{qw( schema_name table_name )}), quote_ident($_->{constraint_name}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 12 | } @by, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # The primary key constraint of the source on the table of $tgt, under another | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | # name than the primary key $tgt is: PostgreSQL takes one per table, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # diff creates before it drops. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 57 | ▶ | sub _other_primary { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_other_primary: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 57 | my ($src_indexes, $tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 57 | my ($name, $type) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 57 | ▶ | return unless defined $type && $type eq 'p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $type and $type eq "p"
MC/DC50%1/2
defined $type and $type eq "p"
defined $type
$type eq "p"
Truth table66%2/3
A and B
A= defined $typeB= $type eq "p"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 13 | for my $other (_in_order($src_indexes)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 12 | ▶ | next unless $other->{table_key} eq $tgt->{table_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 12 | ▶ | next if $other->{index_name} eq $tgt->{index_name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 6 | my (undef, $otype) = _constraint($other->{index_info}, $other->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 6 | ▶ | return $other if defined $otype && $otype eq 'p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $otype and $otype eq "p"
MC/DC50%1/2
defined $otype and $otype eq "p"
defined $otype
$otype eq "p"
Truth table66%2/3
A and B
A= defined $otypeB= $otype eq "p"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 10 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | # Why the constraint of $tgt cannot be created as ALTER TABLE ... ADD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # CONSTRAINT (...): the lines that say it. Nothing when it can. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 38 | ▶ | sub _create_reasons { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_reasons: called CC=13 SCAR=25.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 38 | my ($tgt, $src_indexes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 38 | my $info = $tgt->{index_info}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 38 | my ($name, $type) = _constraint($info, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 38 | my @table = grep { defined } @{$tgt}{qw( schema_name table_name )}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 38 | my @reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 38 | ▶ | if ($type eq 'x') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 3 | 'it is an EXCLUDE constraint, and the model does not carry its elements, so the diff cannot write it:', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | sprintf(' ALTER TABLE %s ADD CONSTRAINT %s EXCLUDE (...);', quote_qualified(@table), quote_ident($name)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 35 | ▶ | my @columns = @{ $info->{columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"columns"} // []
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | push @reasons, 'its key has an expression or no column the model names, and a constraint names columns' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 35 | ▶ | if !@columns || grep { !defined } @columns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not @columns or grep {not defined $_;} @columns
MC/DC50%1/2
not @columns or grep {not defined $_;} @columns
not @columns
grep {not defined $_;} @columns
Truth table66%2/3
not(A) or B
A= not @columnsB= grep {not defined $_;} @columns
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | push @reasons, 'its index has a sort order (' . join(', ', map { "$_ " . $info->{sort_options}{$_} } sort keys %{ $info->{sort_options} }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | . '), which a constraint cannot have' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 35 | ▶ | if %{ $info->{sort_options} // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$info->{"sort_options"} // {}
MC/DC100%1/1
$info->{"sort_options"} // {}
$info->{"sort_options"}
Truth table100%2/2
A
A= $info->{"sort_options"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | push @reasons, 'its index has an operator class (' . join(', ', map { "$_ " . $info->{opclasses}{$_} } sort keys %{ $info->{opclasses} }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | . '), which a constraint cannot name' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 35 | ▶ | if %{ $info->{opclasses} // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$info->{"opclasses"} // {}
MC/DC100%1/1
$info->{"opclasses"} // {}
$info->{"opclasses"}
Truth table100%2/2
A
A= $info->{"opclasses"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | push @reasons, sprintf('its index is a %s index, and a constraint is a btree index', $info->{access_method}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 35 | ▶ | if defined $info->{access_method} && $info->{access_method} ne 'btree'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $info->{"access_method"} and $info->{"access_method"} ne "btree"
MC/DC0%0/2
defined $info->{"access_method"} and $info->{"access_method"} ne "btree"
defined $info->{"access_method"}
$info->{"access_method"} ne "btree"
Truth table33%1/3
A and B
A= defined $info->{"access_method"}B= $info->{"access_method"} ne "btree"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | @reasons = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 35 | ▶ | ( map { "$_," } @reasons ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 'so the diff cannot write it as a constraint. Declare it as an index of its own, or write the DDL yourself.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | ) if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 35 | ▶ | if (my $other = _other_primary($src_indexes, $tgt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | push @reasons, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | sprintf('the table has the PRIMARY KEY constraint %s in the database, and PostgreSQL takes one per table (multiple primary keys); the diff creates before it drops:', $other->{index_name}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | sprintf(' ALTER TABLE %s DROP CONSTRAINT %s;', quote_qualified(@table), quote_ident($other->{index_name})); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 35 | return @reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Why the index $src cannot become the constraint of $tgt by ALTER TABLE ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # ADD CONSTRAINT ... USING INDEX: the lines that say it. Measured on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # PostgreSQL 18.6, the server refuses it for an index that is not unique or not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # btree, that has a predicate, an expression, a sort order other than the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # default or is not valid, for a partitioned table, and a second primary key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # it renames the index to the name of the constraint. INCLUDE columns and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | # NULLS NOT DISTINCT are kept. Nothing when it can. An index that is renamed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # first ($renamed) has the name of the constraint by the time it is used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 22 | ▶ | sub _using_index_reasons { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_using_index_reasons: called CC=22 SCAR=31.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 22 | my ($src, $tgt, $src_tables, $src_indexes, $renamed) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 22 | my $info = $src->{index_info}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 22 | my ($name, $type) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 22 | my @reasons; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 22 | ▶ | push @reasons, 'the index is not unique' unless $info->{is_unique} || $info->{is_primary}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"is_unique"} or $info->{"is_primary"}
MC/DC50%1/2
$info->{"is_unique"} or $info->{"is_primary"}
$info->{"is_unique"}
$info->{"is_primary"}
Truth table66%2/3
A or B
A= $info->{"is_unique"}B= $info->{"is_primary"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | push @reasons, sprintf('the index is a %s index, not btree', $info->{access_method}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 22 | ▶ | if defined $info->{access_method} && $info->{access_method} ne 'btree'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"access_method"} and $info->{"access_method"} ne "btree"
MC/DC50%1/2
defined $info->{"access_method"} and $info->{"access_method"} ne "btree"
defined $info->{"access_method"}
$info->{"access_method"} ne "btree"
Truth table66%2/3
A and B
A= defined $info->{"access_method"}B= $info->{"access_method"} ne "btree"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 22 | ▶ | push @reasons, 'the index has a predicate (it is a partial index)' if length($info->{predicate} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$info->{"predicate"} // ""
MC/DC100%1/1
$info->{"predicate"} // ""
$info->{"predicate"}
Truth table100%2/2
A
A= $info->{"predicate"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | push @reasons, 'the index has an expression' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 22 | ▶ | if length($info->{expressions} // '') || grep { !defined } @{ $info->{columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition71%5/7
length($info->{"expressions"} // "") or grep {not defined $_;} @{$info->{"columns"} // [];}
$info->{"expressions"} // ""
$info->{"columns"} // []
MC/DC50%1/2
length($info->{"expressions"} // "") or grep {not defined $_;} @{$info->{"columns"} // [];}
$info->{"expressions"}
grep {not defined $_;} @{$info->{"columns"} // [];}
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table66%2/3
A or B
A= $info->{"expressions"}B= grep {not defined $_;} @{$info->{"columns"} // [];}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 1 | push @reasons, 'the index has a sort order other than ascending with the nulls last (' . join(', ', map { "$_ " . $info->{sort_options}{$_} } sort keys %{ $info->{sort_options} }) . ')' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 22 | ▶ | if %{ $info->{sort_options} // {} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$info->{"sort_options"} // {}
MC/DC0%0/1
$info->{"sort_options"} // {}
$info->{"sort_options"}
Truth table50%1/2
A
A= $info->{"sort_options"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | push @reasons, 'the index is not valid (a CREATE INDEX CONCURRENTLY that failed)' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 22 | ▶ | if defined $info->{is_valid} && !$info->{is_valid}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"is_valid"} and not $info->{"is_valid"}
MC/DC50%1/2
defined $info->{"is_valid"} and not $info->{"is_valid"}
defined $info->{"is_valid"}
not $info->{"is_valid"}
Truth table66%2/3
A and not(B)
A= defined $info->{"is_valid"}B= not $info->{"is_valid"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | push @reasons, 'the table is partitioned, and ADD CONSTRAINT ... USING INDEX is not supported on a partitioned table' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 22 | ▶ | if (($src_tables->{ $src->{table_key} } // {})->{kind} // '') eq 'p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
+($$src_tables{$src->{"table_key"}} // {})->{"kind"} // ""
$$src_tables{$src->{"table_key"}} // {}
MC/DC0%0/1
+($$src_tables{$src->{"table_key"}} // {})->{"kind"} // ""
$$src_tables{$src->{"table_key"}}
Truth table50%1/2
A
A= $$src_tables{$src->{"table_key"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | push @reasons, sprintf('the constraint is named %s and the index %s: USING INDEX would rename the index to the name of the constraint', $name, $src->{index_name}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 22 | ▶ | if $name ne $src->{index_name} && !$renamed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name ne $src->{"index_name"} and not $renamed
MC/DC100%2/2
$name ne $src->{"index_name"} and not $renamed
$name ne $src->{"index_name"}
not $renamed
Truth table100%3/3
A and not(B)
A= $name ne $src->{"index_name"}B= not $renamed
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 22 | my @primary; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 22 | ▶ | if (my $other = _other_primary($src_indexes, $tgt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | @primary = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | sprintf('the table has the PRIMARY KEY constraint %s in the database, and PostgreSQL takes one per table (multiple primary keys):', $other->{index_name}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 2 | sprintf(' ALTER TABLE %s DROP CONSTRAINT %s;', quote_qualified(grep { defined } @{$tgt}{qw( schema_name table_name )}), quote_ident($other->{index_name})), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 22 | ▶ | ( @reasons ? (map({ "$_," } @reasons), 'so the diff does not put the constraint on this index. The index goes and the constraint comes, by hand:', _by_hand($src, $tgt)) : () ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | @primary, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | # The refusal for every index that stands behind a constraint and cannot go, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # or be created, the way the diff takes it; returns when there is none. @found | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | # are [ $entry, @reasons ], the entry being the one whose constraint the lines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # are about. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 121 | ▶ | sub _refuse_constraint { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refuse_constraint: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 121 | my (@found) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 121 | ▶ | return unless @found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 30 | my $first = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | die join "\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | ( map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 30 | my ($entry, @reasons) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 30 | my ($name, $type) = _constraint($entry->{index_info}, $entry->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | my $head = sprintf 'index %s on %s is the index of the %s constraint %s', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 30 | $entry->{index_name}, $entry->{table_key}, $CONSTRAINT_KIND{$type}, $name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 30 | ▶ | ( ($first-- > 0 ? "refusing to diff: $head" : $head), map { " $_" } @reasons ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | } @found ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 'The diff takes a constraint away with ALTER TABLE ... DROP CONSTRAINT and brings it', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 'with ALTER TABLE ... ADD CONSTRAINT [... USING INDEX]; it does not do what is named', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 'above. No statement', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 'was generated.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 'Run the statements named above yourself and diff again.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # The foreign keys the target creates (or redefines) that the server would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | # bind to the index $src: a key to the table of the index, on exactly its key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # columns, where the index is a plain unique one. The server takes the first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # unique index over the columns it finds -- the older one, not the one the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # target adds -- and this index is dropped at the end of the run. Returns the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # lines that say it, with the statement that settles it; nothing when there is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # no such key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 39 | ▶ | sub _new_keys_bind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new_keys_bind: called CC=28 SCAR=38.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 39 | my ($src, $source_fks, $target_fks) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 39 | my $info = $src->{index_info}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 39 | ▶ | return unless $target_fks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 10 | ▶ | return unless $info->{is_unique} || $info->{is_primary}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$info->{"is_unique"} or $info->{"is_primary"}
MC/DC0%0/2
$info->{"is_unique"} or $info->{"is_primary"}
$info->{"is_unique"}
$info->{"is_primary"}
Truth table33%1/3
A or B
A= $info->{"is_unique"}B= $info->{"is_primary"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 10 | ▶ | return if length($info->{predicate} // '') || length($info->{expressions} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition42%3/7
length($info->{"predicate"} // "") or length($info->{"expressions"} // "")
$info->{"predicate"} // ""
$info->{"expressions"} // ""
MC/DC0%0/2
length($info->{"predicate"} // "") or length($info->{"expressions"} // "")
$info->{"predicate"}
$info->{"expressions"}
Truth table33%1/3
A or B
A= $info->{"predicate"}B= $info->{"expressions"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 10 | ▶ | my @columns = @{ $info->{columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"columns"} // []
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 10 | ▶ | return if !@columns || grep { !defined } @columns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not @columns or grep {not defined $_;} @columns
MC/DC0%0/2
not @columns or grep {not defined $_;} @columns
not @columns
grep {not defined $_;} @columns
Truth table33%1/3
not(A) or B
A= not @columnsB= grep {not defined $_;} @columns
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 10 | ▶ | my $list = sub { join "\0", map { $_ // '' } @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Condition50%1/2
$_ // ""
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 8 | ▶ | my $shape = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 8 | my ($key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 8 | ▶ | return $list->(@{$key}{qw( remote_schema remote_table )}, @{ $key->{remote_columns} // [] }, '/', @{ $key->{local_columns} // [] }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$key->{"remote_columns"} // []
$key->{"local_columns"} // []
MC/DC0%0/1
$key->{"remote_columns"} // []
$key->{"remote_columns"}
MC/DC0%0/1
$key->{"local_columns"} // []
$key->{"local_columns"}
Truth table50%1/2
A
A= $key->{"remote_columns"}
Truth table50%1/2
A
A= $key->{"local_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 10 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 10 | my @found; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 10 | for my $table_key (sort keys %$target_fks) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 7 | for my $key (@{ $target_fks->{$table_key} }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | next unless ($key->{remote_table} // '') eq ($src->{table_name} // '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 6 | ▶ | && ($key->{remote_schema} // '') eq ($src->{schema_name} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition45%5/11
($key->{"remote_table"} // "") eq ($src->{"table_name"} // "") and ($key->{"remote_schema"} // "") eq ($src->{"schema_name"} // "")
$key->{"remote_table"} // ""
$src->{"table_name"} // ""
$key->{"remote_schema"} // ""
$src->{"schema_name"} // ""
MC/DC0%0/2
($key->{"remote_table"} // "") eq ($src->{"table_name"} // "") and ($key->{"remote_schema"} // "") eq ($src->{"schema_name"} // "")
$key->{"remote_table"}
$key->{"remote_schema"}
MC/DC0%0/1
$src->{"table_name"} // ""
$src->{"table_name"}
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table33%1/3
A and B
A= $key->{"remote_table"}B= $key->{"remote_schema"}
Truth table50%1/2
A
A= $src->{"table_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 6 | ▶ | next unless $list->(@{ $key->{remote_columns} // [] }) eq $list->(@columns); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$key->{"remote_columns"} // []
MC/DC0%0/1
$key->{"remote_columns"} // []
$key->{"remote_columns"}
Truth table50%1/2
A
A= $key->{"remote_columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 5 | ▶ | my ($old) = grep { ($_->{constraint_name} // '') eq $key->{constraint_name} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"constraint_name"} // ""
MC/DC0%0/1
$_->{"constraint_name"} // ""
$_->{"constraint_name"}
Truth table50%1/2
A
A= $_->{"constraint_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 5 | ▶ | @{ ($source_fks // {})->{$table_key} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
+($source_fks // {})->{$table_key} // []
$source_fks // {}
MC/DC0%0/1
+($source_fks // {})->{$table_key} // []
$source_fks
Truth table50%1/2
A
A= $source_fks
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 5 | ▶ | next if $old && $shape->($old) eq $shape->($key); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$old and &$shape($old) eq &$shape($key)
MC/DC50%1/2
$old and &$shape($old) eq &$shape($key)
$old
&$shape($old) eq &$shape($key)
Truth table66%2/3
A and B
A= $oldB= &$shape($old) eq &$shape($key)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 1 | push @found, [ $table_key, $key ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 10 | ▶ | return unless @found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 1 | my ($constraint) = _constraint($info, $src->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 'the foreign key the target creates on its columns would be bound by the server to this index, which the same run drops:', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 1 | ▶ | ( map { sprintf ' %s on %s', $_->[1]{constraint_name}, $_->[0] } @found ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 'Drop the index first (the key is then created against the constraint of the target), and diff again:', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | ' ' . (defined $constraint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | ? 'ALTER TABLE ' . quote_qualified(grep { defined } @{$src}{qw( schema_name table_name )}) . ' DROP CONSTRAINT ' . quote_ident($constraint) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 1 | : 'DROP INDEX ' . quote_qualified(grep { defined } @{$src}{qw( schema_name index_name )})) . ';', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | # The refusal for every index the run drops that a foreign key stands on, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | # would be bound to; returns when there is none. @found are [ $entry, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | # @reasons ]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 91 | ▶ | sub _refuse_bound { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refuse_bound: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 91 | my (@found) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 91 | ▶ | return unless @found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 2 | my $first = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | die join "\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | ( map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 2 | my ($entry, @reasons) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 2 | my $head = sprintf 'index %s on %s is dropped by this run', $entry->{index_name}, $entry->{table_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 2 | ▶ | ( ($first-- > 0 ? "refusing to diff: $head" : $head), map { " $_" } @reasons ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | } @found ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 'The server refuses to drop an index a foreign key stands on. No statement', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 'was generated.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 'Run the statements named above yourself and diff again.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # --- the index of a partitioned table (karr k146) --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 107 | my $IDENT = qr/"(?:[^"]|"")*"|[^\s".]+/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # The text of the index of a partitioned table at one of its partitions: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # name and the table replaced, ON ONLY kept where the partition is partitioned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # itself. Nothing when the text is not the form pg_get_indexdef writes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 20 | ▶ | sub _partition_definition { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_partition_definition: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 20 | my ($definition, $name, $schema, $table, $only) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 20 | ▶ | $definition =~ s/\A(\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+)(?:$IDENT)(\s+ON\s+)ONLY\s+$IDENT(?:\.$IDENT)?(\s+USING\s)/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 20 | ▶ | $1 . quote_ident($name) . $2 . ($only ? 'ONLY ' : '') . quote_qualified(grep { defined } $schema, $table) . $3/ex | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 20 | return $definition; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | # The name the server gives an index it names itself (ChooseIndexName): the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | # table, the columns of the key joined by _ ("expr" for an expression), the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | # label (idx, idx1, ...); at most 63 bytes, the longer of table and columns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | # losing a character at a time (makeObjectName). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 25 | ▶ | sub _partition_index_name { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_partition_index_name: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 25 | my ($table, $columns, $label) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 25 | ▶ | my $cols = join '_', map { defined $_ && length $_ ? $_ : 'expr' } @$columns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_ and length $_
MC/DC50%1/2
defined $_ and length $_
defined $_
length $_
Truth table66%2/3
A and B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 25 | ▶ | $cols = 'expr' unless length $cols; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 25 | my ($t, $c) = ($table, $cols); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 25 | ▶ | while (bytes::length($t) + bytes::length($c) + bytes::length($label) + 2 > 63) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | ▶ | if (length $t > length $c) { chop $t } else { chop $c } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 25 | return join '_', $t, $c, $label; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | # The tree of indexes to put at the partitions of the table $table_key, for the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | # index whose text is $definition. $taken is { schema => { name => 1 } } and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | # grows with the names given. Nothing when the text cannot be rewritten. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 13 | ▶ | sub _partition_tree { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_partition_tree: called CC=14 SCAR=27.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 13 | my ($source_tables, $target_tables, $table_key, $info, $definition, $taken) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 13 | my @tree; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 13 | for my $key (sort keys %$source_tables) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 67 | my $t = $source_tables->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 67 | ▶ | next unless defined $t->{partition_of} && $t->{partition_of} eq $table_key && exists $target_tables->{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $t->{"partition_of"} and $t->{"partition_of"} eq $table_key and exists $target_tables->{$key}
defined $t->{"partition_of"} and $t->{"partition_of"} eq $table_key
MC/DC100%3/3
defined $t->{"partition_of"} and $t->{"partition_of"} eq $table_key and exists $target_tables->{$key}
defined $t->{"partition_of"}
$t->{"partition_of"} eq $table_key
exists $target_tables->{$key}
Truth table100%4/4
A and B and C
A= defined $t->{"partition_of"}B= $t->{"partition_of"} eq $table_keyC= exists $target_tables->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 20 | my $schema = $t->{schema_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 20 | ▶ | my $name = _partition_index_name($t->{table_name}, $info->{columns} // [], 'idx'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"columns"} // []
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 20 | ▶ | for (my $n = 1; $taken->{ $schema // '' }{$name}; $n++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$schema // ""
MC/DC0%0/1
$schema // ""
$schema
Truth table50%1/2
A
A= $schema
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 1 | ▶ | $name = _partition_index_name($t->{table_name}, $info->{columns} // [], "idx$n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"columns"} // []
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 20 | ▶ | $taken->{ $schema // '' }{$name} = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$schema // ""
MC/DC0%0/1
$schema // ""
$schema
Truth table50%1/2
A
A= $schema
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 20 | ▶ | my $partitioned = ($t->{kind} // '') eq 'p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$t->{"kind"} // ""
MC/DC0%0/1
$t->{"kind"} // ""
$t->{"kind"}
Truth table50%1/2
A
A= $t->{"kind"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 20 | ▶ | my $text = _partition_definition($definition, $name, $schema, $t->{table_name}, $partitioned) // return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
_partition_definition($definition, $name, $schema, $t->{"table_name"}, $partitioned) // (return)
MC/DC0%0/1
_partition_definition($definition, $name, $schema, $t->{"table_name"}, $partitioned) // (return)
_partition_definition($definition, $name, $schema, $t->{"table_name"}, $partitioned)
Truth table50%1/2
A
A= _partition_definition($definition, $name, $schema, $t->{"table_name"}, $partitioned)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 20 | my $below = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 20 | ▶ | if ($partitioned) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 6 | ▶ | $below = _partition_tree($source_tables, $target_tables, $key, $info, $definition, $taken) // return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
_partition_tree($source_tables, $target_tables, $key, $info, $definition, $taken) // (return)
MC/DC0%0/1
_partition_tree($source_tables, $target_tables, $key, $info, $definition, $taken) // (return)
_partition_tree($source_tables, $target_tables, $key, $info, $definition, $taken)
Truth table50%1/2
A
A= _partition_tree($source_tables, $target_tables, $key, $info, $definition, $taken)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | push @tree, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | schema_name => $schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | table_name => $t->{table_name}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 20 | index_name => $name, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | definition => $text, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | partitions => $below, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 13 | return \@tree; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # The statements for $tree below the index $parent (a list: schema, name). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 76 | ▶ | sub _partition_sql { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_partition_sql: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 76 | my ($parent, $tree) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | return map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 76 | my $child = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | "$child->{definition};", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | _partition_sql([ $child->{schema_name}, $child->{index_name} ], $child->{partitions} // []), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | sprintf('ALTER INDEX %s ATTACH PARTITION %s;', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 40 | quote_qualified(grep { defined } @$parent), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 20 | ▶ | quote_qualified(grep { defined } @{$child}{qw( schema_name index_name )})), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$child->{"partitions"} // []
MC/DC0%0/1
$child->{"partitions"} // []
$child->{"partitions"}
Truth table50%1/2
A
A= $child->{"partitions"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } @$tree; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 3 | ▶ | sub _refuse_without_definition { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refuse_without_definition: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 3 | my ($tgt, $in_database) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 3 | DBIO::PostgreSQL::Diff::NoStatement->refuse_without_definition( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | kind => 'index', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | name => _name($tgt), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | in_database => $in_database, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | from => 'pg_get_indexdef', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | null_when_gone => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | also => "The columns of the index, which the model may still hold, are not the index:\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | . "whether it is unique, its predicate, its access method, the order and the\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | . 'operator class of each column are in the definition.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 126 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=44 SCAR=38.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 126 | my ($class, $source, $target, $source_tables, $target_tables, $source_fks, $target_fks) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 126 | my @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | # Tables present in the source model but absent from the target are being | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | # dropped in this same pass. Their indexes go with the table via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | # DROP TABLE, so a standalone DROP INDEX for one of them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # must be suppressed (karr k32). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 126 | ▶ | $source_tables //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_tables //= {}
MC/DC100%1/1
$source_tables //= {}
$source_tables
Truth table100%2/2
A
A= $source_tables
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 126 | ▶ | $target_tables //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target_tables //= {}
MC/DC100%1/1
$target_tables //= {}
$target_tables
Truth table100%2/2
A
A= $target_tables
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 13 | my %dropped_table = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 126 | grep { !exists $target_tables->{$_} } keys %$source_tables; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 126 | my $src_indexes = _by_schema($source, $source_tables); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 126 | my $tgt_indexes = _by_schema($target, $target_tables); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 126 | _refuse_case_only($src_indexes, $tgt_indexes); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | 124 | my @constrained; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | # Indexes this run drops that a foreign key stands on or would be bound to. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | my @bound; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | # What decides whether a foreign key that points at a constraint to drop is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | # gone before it (the foreign_keys sections are optional, undef when absent). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | 124 | my $fk = [ $source_fks, $target_fks, $target_tables ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | # Indexes of the source that are renamed and given a constraint, by schema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | # and name, so that they are not dropped as well. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 124 | my %renamed; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | # The index of the source the constraint of $tgt is put on: on the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | # table, with the same definition but for the name, no constraint on it, not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | # an index the target keeps. Each one serves one constraint. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 40 | ▶ | my $renamable = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 40 | my ($tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 40 | my $want = _definition_without_name($tgt->{index_info}{definition}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 40 | for my $src (_in_order($src_indexes)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 27 | ▶ | next if $src->{table_key} ne $tgt->{table_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 26 | ▶ | next if $renamed{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 26 | ▶ | next if $tgt_indexes->{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 17 | ▶ | next if _constraint($src->{index_info}, $src->{index_name}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 13 | ▶ | next if _definition_without_name($src->{index_info}{definition}) ne $want; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 9 | return $src; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 31 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 124 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | # The constraint of the source that is the constraint of $tgt under another | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | # name: on the same table, of the same kind, with the same definition but for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | # the name, a name the target does not keep. It is renamed, ALTER TABLE ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | # RENAME CONSTRAINT, which renames its index with it: dropping it and adding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | # the other would fail where a key stands on it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 43 | ▶ | my $renamable_constraint = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 43 | my ($tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 43 | my (undef, $type) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 43 | ▶ | return unless defined $type && $type ne 'x'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $type and $type ne "x"
MC/DC0%0/2
defined $type and $type ne "x"
defined $type
$type ne "x"
Truth table33%1/3
A and B
A= defined $typeB= $type ne "x"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 43 | my $want = _definition_without_name($tgt->{index_info}{definition}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 43 | for my $src (_in_order($src_indexes)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 32 | ▶ | next if $src->{table_key} ne $tgt->{table_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 31 | ▶ | next if $renamed{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 31 | ▶ | next if $tgt_indexes->{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 22 | my (undef, $stype) = _constraint($src->{index_info}, $src->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 22 | ▶ | next unless defined $stype && $stype eq $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $stype and $stype eq $type
MC/DC50%1/2
defined $stype and $stype eq $type
defined $stype
$stype eq $type
Truth table66%2/3
A and B
A= defined $stypeB= $stype eq $type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 8 | ▶ | next if _definition_without_name($src->{index_info}{definition}) ne $want; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | # A rename keeps the timing of the constraint (karr k147); one that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | # changes it is a drop and a create, see $retimed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 6 | ▶ | next if _timing($src->{index_info}, $stype) ne _timing($tgt->{index_info}, $type); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 3 | return $src; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 40 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 124 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | # The constraint of the source that is the constraint of $tgt under another | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | # name but for its timing (deferrable, initially_deferred): the renamed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | # constraint would keep the old one, so it is dropped and the target's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | # created (karr k147). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 45 | ▶ | my $retimed = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=25.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 45 | my ($tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 45 | my (undef, $type) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 45 | ▶ | return unless defined $type && $type eq 'u'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $type and $type eq "u"
MC/DC100%2/2
defined $type and $type eq "u"
defined $type
$type eq "u"
Truth table100%3/3
A and B
A= defined $typeB= $type eq "u"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 19 | my $want = _definition_without_name($tgt->{index_info}{definition}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 19 | for my $src (_in_order($src_indexes)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 9 | ▶ | next if $src->{table_key} ne $tgt->{table_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 8 | ▶ | next if $renamed{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 8 | ▶ | next if $tgt_indexes->{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 7 | my (undef, $stype) = _constraint($src->{index_info}, $src->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 7 | ▶ | next unless defined $stype && $stype eq $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $stype and $stype eq $type
MC/DC50%1/2
defined $stype and $stype eq $type
defined $stype
$stype eq $type
Truth table66%2/3
A and B
A= defined $stypeB= $stype eq $type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 3 | ▶ | next if _definition_without_name($src->{index_info}{definition}) ne $want; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 3 | return $src; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 16 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 124 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | # The operations that put the constraint of $tgt on an index of the source | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | # that has another name: the constraint renamed, or the index renamed and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | # the constraint put on it. Nothing when there is none. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 73 | ▶ | my $take_over = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 73 | my ($tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 73 | my (undef, $type) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 73 | ▶ | return unless defined $type && $type ne 'x'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $type and $type ne "x"
MC/DC100%2/2
defined $type and $type ne "x"
defined $type
$type ne "x"
Truth table100%3/3
A and B
A= defined $typeB= $type ne "x"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 43 | ▶ | if (my $old = $renamable_constraint->($tgt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 3 | ▶ | $renamed{ $old->{schema_name} // '' }{ $old->{index_name} } = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$old->{"schema_name"} // ""
MC/DC0%0/1
$old->{"schema_name"} // ""
$old->{"schema_name"}
Truth table50%1/2
A
A= $old->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 3 | return $class->new(action => 'rename', %$tgt, renamed_from => $old->{index_name}, via => 'constraint'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 40 | ▶ | if (my $old = $renamable->($tgt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | # A second index would leave the foreign keys on this one (karr k139). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 9 | my @reasons = _using_index_reasons($old, $tgt, $source_tables, $src_indexes, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 9 | ▶ | push @constrained, [ $tgt, @reasons ] if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 9 | ▶ | $renamed{ $old->{schema_name} // '' }{ $old->{index_name} } = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$old->{"schema_name"} // ""
MC/DC0%0/1
$old->{"schema_name"} // ""
$old->{"schema_name"}
Truth table50%1/2
A
A= $old->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 9 | $class->new(action => 'rename', %$tgt, renamed_from => $old->{index_name}), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | $class->new(action => 'create', %$tgt, via => 'using_index'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 31 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 124 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | # Names the schemas hold: the indexes of the source, and what the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | # names the copies of an index of a partitioned table at each partition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | # (the copies are not in the model). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 124 | my %taken; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 124 | for my $entry (map { values %$_ } values %$src_indexes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 112 | ▶ | $taken{ $entry->{schema_name} // '' }{ $entry->{index_name} } = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"schema_name"} // ""
MC/DC0%0/1
$entry->{"schema_name"} // ""
$entry->{"schema_name"}
Truth table50%1/2
A
A= $entry->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 112 | ▶ | next unless (($source_tables->{ $entry->{table_key} } // {})->{kind} // '') eq 'p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
+($$source_tables{$entry->{"table_key"}} // {})->{"kind"} // ""
$$source_tables{$entry->{"table_key"}} // {}
MC/DC100%1/1
+($$source_tables{$entry->{"table_key"}} // {})->{"kind"} // ""
$$source_tables{$entry->{"table_key"}}
Truth table100%2/2
A
A= $$source_tables{$entry->{"table_key"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 4 | ▶ | for my $key (grep { ($source_tables->{$_}{partition_of} // '') eq $entry->{table_key} } keys %$source_tables) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$source_tables->{$_}{"partition_of"} // ""
MC/DC100%1/1
$source_tables->{$_}{"partition_of"} // ""
$source_tables->{$_}{"partition_of"}
Truth table100%2/2
A
A= $source_tables->{$_}{"partition_of"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 4 | my $part = $source_tables->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 4 | ▶ | $taken{ $part->{schema_name} // '' }{ _partition_index_name($part->{table_name}, $entry->{index_info}{columns} // [], 'idx') } = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$part->{"schema_name"} // ""
$entry->{"index_info"}{"columns"} // []
MC/DC0%0/1
$part->{"schema_name"} // ""
$part->{"schema_name"}
MC/DC0%0/1
$entry->{"index_info"}{"columns"} // []
$entry->{"index_info"}{"columns"}
Truth table50%1/2
A
A= $part->{"schema_name"}
Truth table50%1/2
A
A= $entry->{"index_info"}{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | # The indexes to put at the partitions when $tgt is the index of a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | # partitioned table (karr k146): (partitions => \@tree), or nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 28 | ▶ | my $partitions = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 28 | my ($tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 28 | ▶ | return unless (($target_tables->{ $tgt->{table_key} } // {})->{kind} // '') eq 'p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%4/4
+($$target_tables{$tgt->{"table_key"}} // {})->{"kind"} // ""
$$target_tables{$tgt->{"table_key"}} // {}
MC/DC100%1/1
+($$target_tables{$tgt->{"table_key"}} // {})->{"kind"} // ""
$$target_tables{$tgt->{"table_key"}}
Truth table100%2/2
A
A= $$target_tables{$tgt->{"table_key"}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 7 | ▶ | my $def = $tgt->{index_info}{definition} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tgt->{"index_info"}{"definition"} // ""
MC/DC0%0/1
$tgt->{"index_info"}{"definition"} // ""
$tgt->{"index_info"}{"definition"}
Truth table50%1/2
A
A= $tgt->{"index_info"}{"definition"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 7 | ▶ | return unless $def =~ /\A\s*CREATE\s+(?:UNIQUE\s+)?INDEX\s+$IDENT\s+ON\s+ONLY\s/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 7 | ▶ | my $tree = _partition_tree($source_tables, $target_tables, $tgt->{table_key}, $tgt->{index_info}, $def, \%taken) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 7 | ▶ | return @$tree ? (partitions => $tree) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 124 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | # An index to create, as the constraint it is the index of when the target | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | # says so, with the reasons it cannot be written that way collected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 66 | ▶ | my $create = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 66 | my ($tgt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 66 | my (undef, $type) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 66 | ▶ | return $class->new(action => 'create', %$tgt, $partitions->($tgt)) unless defined $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 38 | my @reasons = _create_reasons($tgt, $src_indexes); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | 38 | ▶ | push @constrained, [ $tgt, @reasons ] if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 38 | return $class->new(action => 'create', %$tgt, via => 'constraint'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 124 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | # New indexes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 124 | for my $tgt (_in_order($tgt_indexes)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 109 | ▶ | my $src = $src_indexes->{ $tgt->{schema_name} // '' }{ $tgt->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tgt->{"schema_name"} // ""
MC/DC0%0/1
$tgt->{"schema_name"} // ""
$tgt->{"schema_name"}
Truth table50%1/2
A
A= $tgt->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 109 | ▶ | my $tgt_def = $tgt->{index_info}{definition} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$tgt->{"index_info"}{"definition"} // ""
MC/DC100%1/1
$tgt->{"index_info"}{"definition"} // ""
$tgt->{"index_info"}{"definition"}
Truth table100%2/2
A
A= $tgt->{"index_info"}{"definition"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 109 | ▶ | if (!$src) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 58 | ▶ | _refuse_without_definition($tgt, 0) unless $tgt_def =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | # The constraint is there under another name, or its index is: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | # constraint takes it (karr k139). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 56 | ▶ | if (my @take = $take_over->($tgt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 11 | push @ops, @take; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 11 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | # The same constraint under another name and another timing: dropped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | # and created, not renamed (karr k147). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 45 | ▶ | if (my $old = $retimed->($tgt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 3 | ▶ | $renamed{ $old->{schema_name} // '' }{ $old->{index_name} } = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$old->{"schema_name"} // ""
MC/DC0%0/1
$old->{"schema_name"} // ""
$old->{"schema_name"}
Truth table50%1/2
A
A= $old->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 3 | my @reasons = _drop_reasons($old, 1, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | 3 | ▶ | push @constrained, [ $old, @reasons ] if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 3 | push @ops, $class->new(action => 'drop', %$old, replaced => 1), $create->($tgt); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 42 | push @ops, $create->($tgt); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | 42 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | # Changed indexes: compare definitions, and what the definition does not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | # say (the constraint behind the index first of all) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 51 | ▶ | my $src_def = $src->{index_info}{definition} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$src->{"index_info"}{"definition"} // ""
MC/DC100%1/1
$src->{"index_info"}{"definition"} // ""
$src->{"index_info"}{"definition"}
Truth table100%2/2
A
A= $src->{"index_info"}{"definition"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | 51 | ▶ | if ($src_def ne $tgt_def || _attributes_differ($src, $tgt)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$src_def ne $tgt_def or _attributes_differ($src, $tgt)
MC/DC100%2/2
$src_def ne $tgt_def or _attributes_differ($src, $tgt)
$src_def ne $tgt_def
_attributes_differ($src, $tgt)
Truth table100%3/3
A or B
A= $src_def ne $tgt_defB= _attributes_differ($src, $tgt)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 36 | ▶ | _refuse_without_definition($tgt, 1) unless $tgt_def =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 35 | my (undef, $stype) = _constraint($src->{index_info}, $src->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | 35 | my (undef, $ttype) = _constraint($tgt->{index_info}, $tgt->{index_name}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | # The index is there, the constraint is not: the constraint takes the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | # index it needs, ALTER TABLE ... ADD CONSTRAINT ... USING INDEX. Written | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | # without USING INDEX the server answers "relation already exists". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 35 | ▶ | if (!defined $stype && defined $ttype && $ttype ne 'x' && $src_def eq $tgt_def) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
not defined $stype and defined $ttype and $ttype ne "x" and $src_def eq $tgt_def
not defined $stype and defined $ttype and $ttype ne "x"
not defined $stype and defined $ttype
MC/DC75%3/4
not defined $stype and defined $ttype and $ttype ne "x" and $src_def eq $tgt_def
not defined $stype
defined $ttype
$ttype ne "x"
$src_def eq $tgt_def
Truth table80%4/5
not(A) and B and C and D
A= not defined $stypeB= defined $ttypeC= $ttype ne "x"D= $src_def eq $tgt_def
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 13 | my @reasons = _using_index_reasons($src, $tgt, $source_tables, $src_indexes); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 13 | ▶ | push @constrained, [ $tgt, @reasons ] if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 13 | push @ops, $class->new(action => 'create', %$tgt, via => 'using_index'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 13 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | # Another index of the source is this constraint under another name, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | # the one that has the name is in its way (a nonunique index, or a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | # constraint other than the target's): it goes first, then the other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | # takes the name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 22 | ▶ | my @take = $src_def ne $tgt_def ? $take_over->($tgt) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 22 | ▶ | if (defined $stype) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 16 | my @reasons = _drop_reasons($src, 1, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | 16 | ▶ | push @constrained, [ $src, @reasons ] if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 22 | push @ops, $class->new(action => 'drop', %$src, replaced => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 22 | ▶ | push @ops, @take ? @take : $create->($tgt); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | # Dropped indexes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 121 | for my $src (_in_order($src_indexes)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 111 | ▶ | next if $tgt_indexes->{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 61 | ▶ | next if $renamed{ $src->{schema_name} // '' }{ $src->{index_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$src->{"schema_name"} // ""
MC/DC0%0/1
$src->{"schema_name"} // ""
$src->{"schema_name"}
Truth table50%1/2
A
A= $src->{"schema_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | # Skip when the owning table is itself being dropped this pass; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | # DROP TABLE already removes this index (karr k32). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 46 | ▶ | next if $dropped_table{ $src->{table_key} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 39 | ▶ | if (my @constraint = _constraint($src->{index_info}, $src->{index_name})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | 15 | my @reasons = _drop_reasons($src, 0, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 15 | ▶ | push @constrained, [ $src, @reasons ] if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | # an index without a constraint that a foreign key stands on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 24 | my @reasons = _drop_reasons($src, 0, $fk); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 24 | ▶ | push @bound, [ $src, @reasons ] if @reasons; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 39 | my @binds = _new_keys_bind($src, $source_fks, $target_fks); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 39 | ▶ | push @bound, [ $src, @binds ] if @binds; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 39 | push @ops, $class->new(action => 'drop', %$src); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 121 | _refuse_constraint(@constrained); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | 91 | _refuse_bound(@bound); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | 89 | return @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 185 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=27 SCAR=35.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 185 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 185 | my @table = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | DBIO::PostgreSQL::Introspect->key_names($self->table_key, table_name => $self); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | # A key without a schema -- a model built by hand -- leaves the name alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 185 | ▶ | my @schema = @table > 1 ? $table[0] : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 185 | ▶ | my $action = $self->action // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->action // ""
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 185 | ▶ | my $info = $self->index_info // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->index_info // {}
MC/DC0%0/1
$self->index_info // {}
$self->index_info
Truth table50%1/2
A
A= $self->index_info
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 185 | ▶ | if ($action eq 'rename') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 11 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw($self, 'index ' . _name($self), 'it has no name to rename from') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->renamed_from and length $self->renamed_from
MC/DC50%1/2
defined $self->renamed_from and length $self->renamed_from
defined $self->renamed_from
length $self->renamed_from
Truth table66%2/3
A and B
A= defined $self->renamed_fromB= length $self->renamed_from
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | unless defined $self->renamed_from && length $self->renamed_from; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 10 | ▶ | return sprintf 'ALTER TABLE %s RENAME CONSTRAINT %s TO %s;', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->via and $self->via eq "constraint"
MC/DC50%1/2
defined $self->via and $self->via eq "constraint"
defined $self->via
$self->via eq "constraint"
Truth table66%2/3
A and B
A= defined $self->viaB= $self->via eq "constraint"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | quote_qualified(@table), quote_ident($self->renamed_from), quote_ident($self->index_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | if defined $self->via && $self->via eq 'constraint'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | 7 | return sprintf 'ALTER INDEX %s RENAME TO %s;', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | quote_qualified(@schema, $self->renamed_from), quote_ident($self->index_name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | ▶ | elsif ($action eq 'create' && defined $self->via) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$action eq "create" and defined $self->via
MC/DC100%2/2
$action eq "create" and defined $self->via
$action eq "create"
defined $self->via
Truth table100%3/3
A and B
A= $action eq "create"B= defined $self->via
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | # A key of the table: the constraint, not the index behind it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | 39 | my ($name, $type) = _constraint($info, $self->index_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | 39 | my $object = 'constraint ' . _name($self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | 39 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object, 'the class knows no such way to create it') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->via eq "constraint" or $self->via eq "using_index"
MC/DC0%0/2
$self->via eq "constraint" or $self->via eq "using_index"
$self->via eq "constraint"
$self->via eq "using_index"
Truth table66%2/3
A or B
A= $self->via eq "constraint"B= $self->via eq "using_index"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | unless $self->via eq 'constraint' || $self->via eq 'using_index'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 39 | ▶ | DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $type and $type eq "p" || $type eq "u"
$type eq "p" or $type eq "u"
MC/DC0%0/3
defined $type and $type eq "p" || $type eq "u"
defined $type
$type eq "p"
$type eq "u"
Truth table50%2/4
A and B or C
A= defined $typeB= $type eq "p"C= $type eq "u"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 'its index_info says no PRIMARY KEY or UNIQUE constraint stands behind it') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | unless defined $type && ($type eq 'p' || $type eq 'u'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s %s USING INDEX %s%s;', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 39 | ▶ | quote_qualified(@table), quote_ident($name), $CONSTRAINT_KIND{$type}, quote_ident($self->index_name), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | _timing($info, $type) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | if $self->via eq 'using_index'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 28 | ▶ | my @columns = @{ $info->{columns} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"columns"} // []
MC/DC0%0/1
$info->{"columns"} // []
$info->{"columns"}
Truth table50%1/2
A
A= $info->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | 'its index_info has no columns to name, or one without a name, as an expression has none') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | 28 | ▶ | if !@columns || grep { !defined || !length } @columns; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not @columns or grep {not defined $_ && length $_;} @columns
defined $_ && length $_
MC/DC0%0/2
not @columns or grep {not defined $_ && length $_;} @columns
not @columns
grep {not defined $_ && length $_;} @columns
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table33%1/3
not(A) or B
A= not @columnsB= grep {not defined $_ && length $_;} @columns
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 28 | return sprintf 'ALTER TABLE %s %s;', quote_qualified(@table), _add_constraint_sql($info, $name, $type); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | elsif ($action eq 'create') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | # Use the full definition from introspection if available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | 60 | my $def = $info->{definition}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | 60 | ▶ | return join "\n", "$def;", _partition_sql([ grep { defined } $self->schema_name, $self->index_name ], $self->partitions // []) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
defined $def and $def =~ /\S/
$self->partitions // []
MC/DC50%1/2
defined $def and $def =~ /\S/
defined $def
$def =~ /\S/
MC/DC100%1/1
$self->partitions // []
$self->partitions
Truth table66%2/3
A and B
A= defined $defB= $def =~ /\S/
Truth table100%2/2
A
A= $self->partitions
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | if defined $def && $def =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | # The columns are not the index: unique, predicate, access method, order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | # and operator class stand only in the definition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | 4 | DBIO::PostgreSQL::Diff::NoStatement->throw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | $self, 'index ' . _name($self), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | 'its index_info has no definition, and columns do not stand in for it: that the index is unique, its predicate, access method, order and operator class are in the definition only', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | elsif ($action eq 'drop') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | # An index behind a constraint is dropped with the constraint: the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | # refuses DROP INDEX for it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | 75 | my ($constraint) = _constraint($info, $self->index_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | 75 | ▶ | return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', quote_qualified(@table), quote_ident($constraint) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | if defined $constraint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | 56 | return sprintf 'DROP INDEX %s;', quote_qualified(@schema, $self->index_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | 0 | DBIO::PostgreSQL::Diff::NoStatement->throw($self, 'index ' . _name($self)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 12 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | 12 | ▶ | return sprintf ' ~%s: %s -> %s (on %s)', (defined $self->via ? 'constraint' : 'index'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%2/4
$self->action // ""
$self->renamed_from // ""
MC/DC0%0/1
$self->action // ""
$self->action
MC/DC0%0/1
$self->renamed_from // ""
$self->renamed_from
Truth table50%1/2
A
A= $self->action
Truth table50%1/2
A
A= $self->renamed_from
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | $self->renamed_from // '', $self->index_name, $self->table_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | if ($self->action // '') eq 'rename'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | 10 | ▶ | my $prefix = $self->action eq 'create' ? '+' : '-'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | 10 | ▶ | my $kind = ($self->action eq 'create' ? defined $self->via : _constraint($self->index_info // {}, $self->index_name)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
$self->index_info // {}
MC/DC0%0/1
$self->index_info // {}
$self->index_info
Truth table50%1/2
A
A= $self->index_info
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | ? 'constraint' : 'index'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 10 | return sprintf ' %s%s: %s (on %s)', $prefix, $kind, $self->index_name, $self->table_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | 107 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||