blib/lib/DBIO/PostgreSQL/Diff/Column.pm

filter: Statementstmt 100% 477 / 477 Branchbran 92.9% 210 / 226 Conditioncond 75.4% 209 / 277 MC/DCmcdc 48.9% 67 / 137 Subroutinesub 100% 44 / 44 Podpod 100% 7 / 7 totaltotal 86.8% 1014 / 1168 CC 258 SCAR 32.0
CC
258
Cov
91%
diff
59.6
as_sql
39.2
_after_cascade
23.0
CRAP 24.4
« blib/lib/DBIO/PostgreSQL/SQLMaker.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Deploy.pm »
1 package DBIO::PostgreSQL::Diff::Column;
2 # ABSTRACT: Diff operations for PostgreSQL columns
3  
4 105 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 105 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 105 our $VERSION = '0.900007';
8  
9 105 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 105 ▶use DBIO::Diff::Compare qw(changed_fields column_change_lossy);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 105 ▶use DBIO::PostgreSQL::Diff::CaseOnly ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 105 ▶use DBIO::PostgreSQL::Diff::NoStatement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 105 ▶use DBIO::PostgreSQL::Diff::View ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 105 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 105 ▶use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified type_with_modifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 105 __PACKAGE__->mk_diff_accessors(qw(
19 table_key schema_name table_name column_name old_info new_info using
20 ));
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31 4 ▶sub _refuse_generated_change {
Subroutine _refuse_generated_change: called CC=5 SCAR=16.1
32 4 my ($table_key, $table, $src, $tgt) = @_;
33 4 my $column = $src->{column_name};
34  
35 4 my ($from, $to) = map { DBIO::PostgreSQL::Introspect->generated_kind($_->{generated}) } $src, $tgt;
36 my $what
37 = !$from ? 'an ordinary column in the database and a generated column in the target'
38 : !$to ? 'a generated column in the database and an ordinary column in the target'
39 : $from ne $to ? sprintf('a %s generated column in the database and a %s one in the target', lc $from, lc $to)
40 : sprintf('generated from %s in the database and from %s in the target',
41 4 ▶ map { $_->{default_value} // 'nothing' } $src, $tgt);
Branch: 3 branches
BranchTrueFalseTotal
!$from ? : 1 3 4
!$to ? : 1 2 3
$from ne $to ? : 1 1 2
Condition50%1/2
$_->{"default_value"} // "nothing"
l!l
20
MC/DC0%0/1
$_->{"default_value"} // "nothing"
$_->{"default_value"}
Truth table50%1/2
A
A= $_->{"default_value"}
Aresult
11
00
42  
43 4 die join "\n",
44 "refusing to diff: column $table_key.$column is $what",
45 '',
46 'The diff has no statement for a change to how a column is generated: ALTER',
47 'COLUMN ... SET EXPRESSION exists from PostgreSQL 17 on only, and nothing makes',
48 'an ordinary column a generated one. No statement was generated.',
49 '',
50 'Drop the column yourself when what it holds can go,',
51 " ALTER TABLE $table DROP COLUMN " . quote_ident($column) . ';',
52 'and diff again: the diff then adds it as the target has it.',
53 '';
54 }
55  
56 # Field comparison spec for the PostgreSQL column model. Covers the
57 # canonical data_type / not_null / default_value / size set plus the
58 # PostgreSQL-specific identity and generated flags (attidentity,
59 # attgenerated from pg_catalog) and the referenced enum type name
60 # (pg_enum_type). size is a 'dim' field because it may be a scalar length
61 # (varchar(100)) or an ordered [precision, scale] pair (numeric(10,2)) —
62 # matching DBIO::Diff::Compare::changed_column_fields. identity, generated
63 # and pg_enum_type are scalars (short opcodes / a schema-qualified type
64 # name, not user data).
65 105 my %_COLUMN_FIELD_SPEC = (
66 type => [qw/data_type/],
67 bool => [qw/not_null/],
68 scalar => [qw/default_value identity generated pg_enum_type/],
69 dim => [qw/size/],
70 );
71  
72 # The tables a table has columns from: the partitioned table it is a
73 # partition of, or the tables it inherits from.
74 4573 ▶sub _parents {
Subroutine _parents: called CC=2 SCAR=6.9
75 4573 my ($info) = @_;
76 4573 ▶ return grep { defined } $info->{partition_of}, @{ $info->{inherits} // [] };
Condition100%2/2
$info->{"inherits"} // []
l!l
1024471
MC/DC100%1/1
$info->{"inherits"} // []
$info->{"inherits"}
Truth table100%2/2
A
A= $info->{"inherits"}
Aresult
11
00
77 }
78  
79 # @keys with each table behind the tables it has columns from, and otherwise
80 # in the order given.
81 3632 ▶sub _parents_first {
Subroutine _parents_first: called CC=2 SCAR=6.9
82 3632 my ($tables, @keys) = @_;
83 3632 my %wanted = map { $_ => 1 } @keys;
84 3632 my (%seen, @order);
85 3632 my $visit;
86 3819 ▶ $visit = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
87 3819 my ($key) = @_;
88 3819 ▶ return if $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key}++ 77 3742 3819
89 3742 ▶ $visit->($_) for grep { $wanted{$_} } _parents($tables->{$key} // {});
Condition100%2/2
$tables->{$key} // {}
l!l
3563386
MC/DC100%1/1
$tables->{$key} // {}
$tables->{$key}
Truth table100%2/2
A
A= $tables->{$key}
Aresult
11
00
90 3742 push @order, $key;
91 3632 };
92 3632 $visit->($_) for @keys;
93 3632 undef $visit;
94 3632 return @order;
95 }
96  
97 # What the columns of a table are once the statements for the tables it has
98 # columns from have run. PostgreSQL carries those down (measured on 18.6, for
99 # partitions and for inheritance alike): ADD COLUMN puts the column into the
100 # child as it is above, DROP COLUMN takes it from a child that does not define
101 # it itself as well, ALTER COLUMN TYPE, SET DEFAULT, DROP DEFAULT and SET NOT
102 # NULL do to the child's column what they do above -- a default of the
103 # child's own is overwritten. Two things are not carried down for certain,
104 # and are left as the child has them: DROP NOT NULL leaves a NOT NULL the
105 # child has of its own, which the model cannot tell from an inherited one;
106 # and an identity is a partition's as it is the partitioned table's, but not
107 # inherited by a table that inherits -- there the target says which it was.
108 #
109 # $own and $target are the table's columns by name, in the database and in
110 # the target; each of @parents is [ columns before, columns after ] of a table
111 # it has columns from.
112 64 ▶sub _after_cascade {
Subroutine _after_cascade: called CC=22 SCAR=31.4
113 64 my ($own, $target, @parents) = @_;
114 64 my %now = %$own;
115  
116 64 my %names = map { $_ => 1 } map { keys %{ $_->[0] }, keys %{ $_->[1] } } @parents;
117 64 for my $name (sort keys %names) {
118 174 my @before = grep { $_->[0]{$name} } @parents;
119 174 my @after = grep { $_->[1]{$name} } @parents;
120 16 ▶ my $same_identity = sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
121 16 my ($above) = @_;
122 return $target->{$name}
123 16 ▶ && ($target->{$name}{identity} // '') eq ($above->{identity} // '');
Condition57%4/7
$target->{$name} && ($target->{$name}{'identity'} // '') eq ($above->{'identity'} // '')
!ll&&!rl&&r
0214
$target->{$name}{"identity"} // ""
l!l
160
$above->{"identity"} // ""
l!l
160
MC/DC0%0/2
$target->{$name} && ($target->{$name}{'identity'} // '') eq ($above->{'identity'} // '')
$target->{$name} $target->{$name}{"identity"}
MC/DC0%0/1
$above->{"identity"} // ""
$above->{"identity"}
Truth table33%1/3
A && B
A= $target->{$name}B= $target->{$name}{"identity"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $above->{"identity"}
Aresult
11
00
124 174 };
125  
126 174 ▶ if (!$now{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($now{$name}) 10 164 174
127 10 ▶ next if @before || !@after;
Branch: 1 branch
BranchTrueFalseTotal
if @before or not @after 0 10 10
Condition33%1/3
@before or not @after
l!l&&r!l&&!r
0010
MC/DC0%0/2
@before or not @after
@before not @after
Truth table33%1/3
A or not(B)
A= @beforeB= not @after
ABresult
1X1
011
000
128 10 my $above = $after[0][1]{$name};
129 $now{$name} = {
130 %$above, is_local => 0,
131 10 ▶ identity => ( $same_identity->($above) ? $above->{identity} : '' ),
Branch: 1 branch
BranchTrueFalseTotal
&$same_identity($above) ? : 10 0 10
132 };
133 10 next;
134 }
135  
136 164 ▶ next unless @before; # a column of the table's own
Branch: 1 branch
BranchTrueFalseTotal
unless @before 0 164 164
137 164 ▶ if (!@after) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@after) 8 156 164
138 8 ▶ delete $now{$name} unless $now{$name}{is_local};
Branch: 1 branch
BranchTrueFalseTotal
unless $now{$name}{"is_local"} 7 1 8
139 8 next;
140 }
141  
142 156 my %column = %{ $now{$name} };
143 156 for my $parent (@parents) {
144 166 my ($was, $is) = ($parent->[0]{$name}, $parent->[1]{$name});
145 166 ▶ next unless $was && $is;
Branch: 1 branch
BranchTrueFalseTotal
unless $was and $is 7 159 166
Condition100%3/3
$was and $is
!ll&&!rl&&r
61159
MC/DC100%2/2
$was and $is
$was $is
Truth table100%3/3
A and B
A= $wasB= $is
ABresult
0X0
100
111
146  
147 159 ▶ @column{qw( data_type size pg_enum_type )} = @{$is}{qw( data_type size pg_enum_type )}
Branch: 1 branch
BranchTrueFalseTotal
if _render_type($was) ne _render_type($is) 10 149 159
148 if _render_type($was) ne _render_type($is);
149 $column{default_value} = $is->{default_value}
150 159 ▶ if ($was->{default_value} // '') ne ($is->{default_value} // '');
Branch: 1 branch
BranchTrueFalseTotal
if ($was->{"default_value"} // "") ne ($is->{"default_value"} // "") 8 151 159
Condition75%3/4
$was->{"default_value"} // ""
l!l
0159
$is->{"default_value"} // ""
l!l
8151
MC/DC0%0/1
$was->{"default_value"} // ""
$was->{"default_value"}
MC/DC100%1/1
$is->{"default_value"} // ""
$is->{"default_value"}
Truth table50%1/2
A
A= $was->{"default_value"}
Aresult
11
00
Truth table100%2/2
A
A= $is->{"default_value"}
Aresult
11
00
151 159 ▶ $column{not_null} = 1 if !$was->{not_null} && $is->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if not $was->{"not_null"} and $is->{"not_null"} 6 153 159
Condition100%3/3
not $was->{"not_null"} and $is->{"not_null"}
!ll&&!rl&&r
58956
MC/DC100%2/2
not $was->{"not_null"} and $is->{"not_null"}
not $was->{"not_null"} $is->{"not_null"}
Truth table100%3/3
not(A) and B
A= not $was->{"not_null"}B= $is->{"not_null"}
ABresult
0X0
100
111
152 $column{identity} = $is->{identity}
153 159 ▶ if ($was->{identity} // '') ne ($is->{identity} // '') && $same_identity->($is);
Branch: 1 branch
BranchTrueFalseTotal
if ($was->{"identity"} // "") ne ($is->{"identity"} // "") and &$same_identity($is) 4 155 159
Condition71%5/7
($was->{"identity"} // "") ne ($is->{"identity"} // "") and &$same_identity($is)
!ll&&!rl&&r
15324
$was->{"identity"} // ""
l!l
1590
$is->{"identity"} // ""
l!l
1590
MC/DC50%1/2
($was->{"identity"} // "") ne ($is->{"identity"} // "") and &$same_identity($is)
$was->{"identity"} &$same_identity($is)
MC/DC0%0/1
$is->{"identity"} // ""
$is->{"identity"}
Truth table66%2/3
A and B
A= $was->{"identity"}B= &$same_identity($is)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $is->{"identity"}
Aresult
11
00
154 }
155 156 $now{$name} = \%column;
156 }
157  
158 64 return \%now;
159 }
160  
161 # What reads a column, in the model of the database: { table key => { column
162 # => [ view keys ] } }, from the depends_on_columns of its views.
163 7234 ▶sub _readers {
Subroutine _readers: called CC=6 SCAR=17.9
164 7234 my ($tables) = @_;
165 7234 my %readers;
166 7234 for my $view (sort keys %$tables) {
167 871 ▶ next unless DBIO::PostgreSQL::Diff::View->is_view($tables->{$view});
Branch: 1 branch
BranchTrueFalseTotal
unless "DBIO::PostgreSQL::Diff::View"->is_view($tables->{$view}) 746 125 871
168 125 ▶ my $reads = $tables->{$view}{depends_on_columns} // {};
Condition100%2/2
$tables->{$view}{"depends_on_columns"} // {}
l!l
9332
MC/DC100%1/1
$tables->{$view}{"depends_on_columns"} // {}
$tables->{$view}{"depends_on_columns"}
Truth table100%2/2
A
A= $tables->{$view}{"depends_on_columns"}
Aresult
11
00
169 125 for my $table (keys %$reads) {
170 93 push @{ $readers{$table}{$_} }, $view for @{ $reads->{$table} };
171 }
172 }
173 7234 return \%readers;
174 }
175  
176 # The views of the database that the target does not have and that stand on a
177 # column whose type changes. The target's views are dropped and created again
178 # by Diff::View; these are not the declaration's to create again.
179 4 ▶sub _refuse_type_change_under_undeclared_view {
Subroutine _refuse_type_change_under_undeclared_view: called CC=2 SCAR=6.9
180 4 my ($table_key, $column, $from, $to, $source_tables, @undeclared) = @_;
181  
182 4 ▶ my $kind = sub { $source_tables->{ $_[0] }{kind} eq 'm' ? 'materialized view' : 'view' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$$source_tables{$_[0]}{'kind'} eq 'm' ? : 2 8 10
183 4 my @drop = DBIO::PostgreSQL::Diff::View->with_dependents($source_tables, @undeclared);
184  
185 die join "\n",
186 sprintf('refusing to diff: column %s.%s changes its type from %s to %s while %s, which only the database has, depends on it',
187 $table_key, $column, $from, $to,
188 5 join(', ', map { $kind->($_) . " $_" } sort @undeclared)),
189 '',
190 'PostgreSQL does not alter the type of a column while a view uses it',
191 '(cannot alter type of a column used by a view or rule). The diff drops the',
192 'views the target has around that change and creates them again, but not a view',
193 'the target does not have: only the declaration is the diff\'s to create.',
194 'No statement was generated.',
195 '',
196 'Drop what only the database has yourself, with the views that stand on it,',
197 5 ▶ ( map { ' ' . DBIO::PostgreSQL::Diff::View->drop_sql($source_tables->{$_}) } @drop ),
Branch: 1 branch
BranchTrueFalseTotal
grep({&$kind($_) ne 'view';} @drop) ? : 1 3 4
198 'and diff again: the diff then drops the views the target has, alters the column',
199 'and creates them again.',
200 4 ( ( grep { $kind->($_) ne 'view' } @drop )
201 ? 'A materialized view is built anew by that, and the indexes on it are created again.'
202 : () ),
203 '';
204 }
205  
206 # The materialized views that stand on a column whose type changes -- reading
207 # it, or standing on a view that does. The diff drops and creates again the
208 # views around such a change, but not a materialized view: that would build
209 # its data anew and take its indexes with it (decision 2026-10-16).
210 3 ▶sub _refuse_type_change_under_matview {
Subroutine _refuse_type_change_under_matview: called CC=3 SCAR=11.1
211 3 my ($table_key, $column, $from, $to, $source_tables, $read_by, $in_the_way, @matviews) = @_;
212  
213 3 my %reads_in = map { $_->[0] => $_->[1] } @$read_by;
214 3 my %in_the_way = map { $_ => 1 } @$in_the_way;
215 3 ▶ my $kind = sub { $source_tables->{ $_[0] }{kind} eq 'm' ? 'materialized view' : 'view' };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$$source_tables{$_[0]}{'kind'} eq 'm' ? : 0 1 1
216 1 ▶ my $through = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
217 1 ▶ my @on = sort grep { $in_the_way{$_} } @{ $source_tables->{ $_[0] }{depends_on} // [] };
Condition50%1/2
$$source_tables{$_[0]}{"depends_on"} // []
l!l
10
MC/DC0%0/1
$$source_tables{$_[0]}{"depends_on"} // []
$$source_tables{$_[0]}{"depends_on"}
Truth table50%1/2
A
A= $$source_tables{$_[0]}{"depends_on"}
Aresult
11
00
218 1 ▶ return @on ? ' (through ' . join(', ', map { $kind->($_) . " $_" } @on) . ')' : '';
Branch: 1 branch
BranchTrueFalseTotal
@on ? : 1 0 1
219 3 };
220 3 my @drop = DBIO::PostgreSQL::Diff::View->with_dependents($source_tables, @matviews);
221  
222 die join "\n",
223 sprintf('refusing to diff: column %s.%s changes its type from %s to %s and is read by %s',
224 $table_key, $column, $from, $to,
225 join(', ', map {
226 3 my $in = $reads_in{$_};
227 3 ▶ "materialized view $_"
Branch: 2 branches
BranchTrueFalseTotal
!defined($in) ? : 1 2 3
$in eq $table_key ? : 2 0 2
228 . (!defined $in ? $through->($_) : $in eq $table_key ? '' : " (in $in)")
229 } sort @matviews)),
230 '',
231 'PostgreSQL does not alter the type of a column while a view uses it',
232 '(cannot alter type of a column used by a view or rule). The diff drops the',
233 'views around that change and creates them again, but not a materialized view:',
234 'that would build its data anew and take the indexes on it with it.',
235 'No statement was generated.',
236 '',
237 'Drop it yourself when that is acceptable, with the views that stand on it,',
238 3 ( map { ' ' . DBIO::PostgreSQL::Diff::View->drop_sql($source_tables->{$_}) } @drop ),
239 'and diff again: the diff then alters the column, creates again the views the',
240 'target has, and creates the materialized view as the target has it, with the',
241 'indexes the target has on it.',
242 '';
243 }
244  
245 # What the server says to a statement for a column at the table that only
246 # has it from another.
247 105 my %NOT_AT_PARENT = (
248 add => [ 'is to be added to %s, a partition of %s, and not to %2$s', 'cannot add column to a partition' ],
249 drop => [ 'is to be dropped from %s and not from %s, which it has the column from', 'cannot drop inherited column' ],
250 type => [ 'is to change its type in %s and not in %s, which it has the column from', 'cannot alter inherited column' ],
251 );
252  
253 3 ▶sub _refuse_not_at_parent {
Subroutine _refuse_not_at_parent: called CC=1 SCAR=0.0
254 3 my ($what, $table_key, $column, @parents) = @_;
255 3 my ($phrase, $server) = @{ $NOT_AT_PARENT{$what} };
256 3 die join "\n",
257 "refusing to diff: column $table_key.$column "
258 . sprintf($phrase, $table_key, join(', ', @parents)),
259 '',
260 'A column a table has from the table it is a partition of, or inherits from, is',
261 "changed there, and PostgreSQL refuses the statement anywhere else ($server).",
262 'No database has the two differ in it; this target model does.',
263 'No statement was generated.',
264 '',
265 'Make the two agree in the target model and diff again.',
266 '';
267 }
268  
269 # What else reads a column, in the model of the database, and is not a view of
270 # the model: { relation key => { column => [ reader ] } }. A reader is
271 # { kind, table (key of the relation it is on), name } for a policy, a trigger
272 # and a generated column, and { kind => 'view', key, ... } for a view outside
273 # the schemas the model holds.
274 3632 ▶sub _other_readers {
Subroutine _other_readers: called CC=19 SCAR=29.5
275 3632 my ($cols, $policies, $triggers, $outside) = @_;
276 3632 my %readers;
277 3632 for my $pair ([ policy => $policies ], [ trigger => $triggers ]) {
278 7264 my ($kind, $section) = @$pair;
279 7264 ▶ for my $table (sort keys %{ $section // {} }) {
Condition100%2/2
$section // {}
l!l
7070194
MC/DC100%1/1
$section // {}
$section
Truth table100%2/2
A
A= $section
Aresult
11
00
280 25 for my $name (sort keys %{ $section->{$table} }) {
281 26 my $entry = $section->{$table}{$name};
282 26 ▶ my $reads = $entry->{depends_on_columns} // {};
Condition100%2/2
$entry->{"depends_on_columns"} // {}
l!l
719
MC/DC100%1/1
$entry->{"depends_on_columns"} // {}
$entry->{"depends_on_columns"}
Truth table100%2/2
A
A= $entry->{"depends_on_columns"}
Aresult
11
00
283 26 for my $relation (keys %$reads) {
284 7 push @{ $readers{$relation}{$_} }, {
285 kind => $kind, table => $table, name => $name,
286 schema_name => $entry->{schema_name}, table_name => $entry->{table_name},
287 7 } for @{ $reads->{$relation} };
288 }
289 }
290 }
291 }
292 3632 ▶ for my $table (sort keys %{ $cols // {} }) {
Condition50%1/2
$cols // {}
l!l
36320
MC/DC0%0/1
$cols // {}
$cols
Truth table50%1/2
A
A= $cols
Aresult
11
00
293 367 for my $column (@{ $cols->{$table} }) {
294 1 push @{ $readers{$table}{$_} }, {
295 kind => 'generated column', table => $table, name => $column->{column_name},
296 673 ▶ } for @{ $column->{depends_on_columns} // [] };
Condition100%2/2
$column->{"depends_on_columns"} // []
l!l
25648
MC/DC100%1/1
$column->{"depends_on_columns"} // []
$column->{"depends_on_columns"}
Truth table100%2/2
A
A= $column->{"depends_on_columns"}
Aresult
11
00
297 }
298 }
299 3632 ▶ for my $view (sort keys %{ $outside // {} }) {
Condition100%2/2
$outside // {}
l!l
353597
MC/DC100%1/1
$outside // {}
$outside
Truth table100%2/2
A
A= $outside
Aresult
11
00
300 2 my $info = $outside->{$view};
301 2 ▶ my $reads = $info->{depends_on_columns} // {};
Condition50%1/2
$info->{"depends_on_columns"} // {}
l!l
20
MC/DC0%0/1
$info->{"depends_on_columns"} // {}
$info->{"depends_on_columns"}
Truth table50%1/2
A
A= $info->{"depends_on_columns"}
Aresult
11
00
302 2 for my $relation (keys %$reads) {
303 2 ▶ push @{ $readers{$relation}{$_} }, {
Branch: 1 branch
BranchTrueFalseTotal
$info->{'kind'} eq 'm' ? : 0 2 2
304 kind => $info->{kind} eq 'm' ? 'materialized view' : 'view',
305 table => $view, name => $info->{table_name}, schema_name => $info->{schema_name},
306 table_name => $info->{table_name}, outside => 1,
307 2 } for @{ $reads->{$relation} };
308 }
309 }
310 3632 return \%readers;
311 }
312  
313 # The statement that removes a reader, and the one that makes it again where
314 # the target says how.
315 6 ▶sub _drop_reader_sql {
Subroutine _drop_reader_sql: called CC=4 SCAR=13.9
316 6 my ($reader) = @_;
317 my $on = quote_qualified(DBIO::PostgreSQL::Introspect->key_names(
318 6 $reader->{table}, table_name => $reader));
319 6 my $name = quote_ident($reader->{name});
320 6 my $kind = $reader->{kind};
321 6 ▶ return "DROP POLICY $name ON $on;" if $kind eq 'policy';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "policy" 3 3 6
322 3 ▶ return "DROP TRIGGER $name ON $on;" if $kind eq 'trigger';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "trigger" 1 2 3
323 2 ▶ return "ALTER TABLE $on DROP COLUMN $name;" if $kind eq 'generated column';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "generated column" 1 1 2
324 1 return 'DROP ' . uc($kind) . " $on;";
325 }
326  
327 6 ▶sub _describe_reader {
Subroutine _describe_reader: called CC=4 SCAR=13.9
328 6 my ($reader) = @_;
329 6 my $kind = $reader->{kind};
330 6 ▶ return "$kind $reader->{table}" . ($reader->{outside} ? ' (outside the declared schemas)' : '')
Branch: 2 branches
BranchTrueFalseTotal
if $kind =~ /view/ 1 5 6
$reader->{'outside'} ? : 1 0 1
331 if $kind =~ /view/;
332 5 ▶ return "$kind $reader->{table}.$reader->{name}" if $kind eq 'generated column';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "generated column" 1 4 5
333 4 return "$kind $reader->{name} on $reader->{table}";
334 }
335  
336 # A column whose type changes while a policy, a trigger or a generated column
337 # reads it, or a view the model does not hold. PostgreSQL does not alter such
338 # a column, and the diff does not drop and create these again: a policy and a
339 # trigger would be created by the sections behind the columns, but only
340 # the model's own objects are the diff's, and what a generated column or an
341 # outside view has lost is not in it.
342 6 ▶sub _refuse_type_change_under_readers {
Subroutine _refuse_type_change_under_readers: called CC=2 SCAR=6.9
343 6 my ($table_key, $column, $from, $to, @readers) = @_;
344 6 my %seen;
345 6 @readers = grep { !$seen{ join "\0", @{$_}{qw( kind table name )} }++ } @readers;
346  
347 6 my %server = (
348 'policy' => 'cannot alter type of a column used in a policy definition',
349 'trigger' => 'cannot alter type of a column used in a trigger definition',
350 'generated column' => 'cannot alter type of a column used by a generated column',
351 'view' => 'cannot alter type of a column used by a view or rule',
352 'materialized view' => 'cannot alter type of a column used by a view or rule',
353 );
354 6 my @kinds = do { my %k; grep { !$k{$_}++ } map { $_->{kind} } @readers };
355  
356 die join "\n",
357 sprintf('refusing to diff: column %s.%s changes its type from %s to %s while %s depends on it',
358 6 $table_key, $column, $from, $to, join(', ', map { _describe_reader($_) } @readers)),
359 '',
360 'PostgreSQL does not alter the type of a column that a policy, a trigger\'s WHEN',
361 'or UPDATE OF, a generated column or a view uses ('
362 6 . join('; ', map { $server{$_} } @kinds) . ').',
363 'The diff does not take these out of the way and put them back: only the views',
364 'the declaration has are, and the other objects are not the type change\'s to',
365 'drop. No statement was generated.',
366 '',
367 'Take them out yourself, then diff again:',
368 6 ▶ ( map { ' ' . _drop_reader_sql($_) } @readers ),
Branch: 1 branch
BranchTrueFalseTotal
grep({$_->{'outside'};} @readers) ? : 1 5 6
369 'The diff then alters the column and creates again what the target has',
370 6 ( ( grep { $_->{outside} } @readers )
371 ? '(a view outside the declared schemas is not the target\'s: create it again yourself, after).'
372 : '(a policy, a trigger or a generated column the target declares).' ),
373 'Or change the type by hand, with the statements above and their CREATE',
374 'statements around it, in one transaction.',
375 '';
376 }
377  
378 # The relations whose columns $column of $table_key is: the table itself and
379 # the tables that have the column from it.
380 147 ▶sub _holders {
Subroutine _holders: called CC=5 SCAR=16.1
381 147 my ($table_key, $column, $children, $source_cols) = @_;
382 147 my (@holders, %asked);
383 147 my @todo = ($table_key);
384 147 ▶ while (defined(my $key = shift @todo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = shift @todo)) 158 147 305
385 158 ▶ next if $asked{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $asked{$key}++ 0 158 158
386 158 push @holders, $key;
387 push @todo, sort grep {
388 11 ▶ grep { $_->{column_name} eq $column } @{ $source_cols->{$_} // [] }
Condition50%1/2
$source_cols->{$_} // []
l!l
110
MC/DC0%0/1
$source_cols->{$_} // []
$source_cols->{$_}
Truth table50%1/2
A
A= $source_cols->{$_}
Aresult
11
00
389 158 ▶ } @{ $children->{$key} // [] };
Condition100%2/2
$children->{$key} // []
l!l
6152
MC/DC100%1/1
$children->{$key} // []
$children->{$key}
Truth table100%2/2
A
A= $children->{$key}
Aresult
11
00
390 }
391 147 return @holders;
392 }
393  
394 # The views that read $column of $table_key, or of a table that has it from
395 # this one: [ view key, table key it reads it in ], each view once.
396 164 ▶sub _read_by {
Subroutine _read_by: called CC=6 SCAR=17.9
397 164 my ($table_key, $column, $readers, $children, $source_cols) = @_;
398 164 my (@holders, %asked);
399 164 my @todo = ($table_key);
400 164 ▶ while (defined(my $key = shift @todo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = shift @todo)) 179 164 343
401 179 ▶ next if $asked{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $asked{$key}++ 0 179 179
402 179 push @holders, $key;
403 push @todo, sort grep {
404 15 ▶ grep { $_->{column_name} eq $column } @{ $source_cols->{$_} // [] }
Condition50%1/2
$source_cols->{$_} // []
l!l
150
MC/DC0%0/1
$source_cols->{$_} // []
$source_cols->{$_}
Truth table50%1/2
A
A= $source_cols->{$_}
Aresult
11
00
405 179 ▶ } @{ $children->{$key} // [] };
Condition100%2/2
$children->{$key} // []
l!l
8171
MC/DC100%1/1
$children->{$key} // []
$children->{$key}
Truth table100%2/2
A
A= $children->{$key}
Aresult
11
00
406 }
407 164 my %named;
408 43 return grep { !$named{ $_->[0] }++ }
409 164 ▶ map { my $in = $_; map { [ $_, $in ] } @{ $readers->{$in}{$column} // [] } } @holders;
Condition100%2/2
$readers->{$in}{$column} // []
l!l
39140
MC/DC100%1/1
$readers->{$in}{$column} // []
$readers->{$in}{$column}
Truth table100%2/2
A
A= $readers->{$in}{$column}
Aresult
11
00
410 }
411  
412  
413 3602 ▶sub type_changed_readers {
Subroutine type_changed_readers: called CC=17 SCAR=29.7
Pod documented
414 3602 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
415 3602 ▶ $source_cols //= {}; $target_cols //= {};
Condition50%2/4
$source_cols //= {}
l!l
36020
$target_cols //= {}
l!l
36020
MC/DC0%0/1
$source_cols //= {}
$source_cols
MC/DC0%0/1
$target_cols //= {}
$target_cols
Truth table50%1/2
A
A= $source_cols
Aresult
11
00
Truth table50%1/2
A
A= $target_cols
Aresult
11
00
416 3602 ▶ $source_tables //= {}; $target_tables //= {};
Condition50%2/4
$source_tables //= {}
l!l
36020
$target_tables //= {}
l!l
36020
MC/DC0%0/1
$source_tables //= {}
$source_tables
MC/DC0%0/1
$target_tables //= {}
$target_tables
Truth table50%1/2
A
A= $source_tables
Aresult
11
00
Truth table50%1/2
A
A= $target_tables
Aresult
11
00
417  
418 3602 my $readers = _readers($source_tables);
419 3602 ▶ return {} unless %$readers;
Branch: 1 branch
BranchTrueFalseTotal
unless %$readers 3580 22 3602
420 22 my %children;
421 22 for my $key (keys %$source_tables) {
422 63 push @{ $children{$_} }, $key for _parents($source_tables->{$key});
423 }
424  
425 22 my %found;
426 22 for my $table_key (sort keys %$target_cols) {
427 26 ▶ next unless exists $source_tables->{$table_key} && exists $target_tables->{$table_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_tables->{$table_key} and exists $target_tables->{$table_key} 0 26 26
Condition33%1/3
exists $source_tables->{$table_key} and exists $target_tables->{$table_key}
!ll&&!rl&&r
0026
MC/DC0%0/2
exists $source_tables->{$table_key} and exists $target_tables->{$table_key}
exists $source_tables->{$table_key} exists $target_tables->{$table_key}
Truth table33%1/3
A and B
A= exists $source_tables->{$table_key}B= exists $target_tables->{$table_key}
ABresult
0X0
100
111
428 26 ▶ next if grep { DBIO::PostgreSQL::Diff::View->is_view($_->{$table_key}) }
Branch: 1 branch
BranchTrueFalseTotal
if grep {"DBIO::PostgreSQL::Diff::View"->is_view($_->{$table_key});} $source_tables, $target_tables 0 26 26
429 $source_tables, $target_tables;
430 26 ▶ my %was = map { $_->{column_name} => $_ } @{ $source_cols->{$table_key} // [] };
Condition50%1/2
$source_cols->{$table_key} // []
l!l
260
MC/DC0%0/1
$source_cols->{$table_key} // []
$source_cols->{$table_key}
Truth table50%1/2
A
A= $source_cols->{$table_key}
Aresult
11
00
431 26 for my $now (@{ $target_cols->{$table_key} }) {
432 104 my $name = $now->{column_name};
433 104 ▶ next unless $was{$name} && _render_type($was{$name}) ne _render_type($now);
Branch: 1 branch
BranchTrueFalseTotal
unless $was{$name} and _render_type($was{$name}) ne _render_type($now) 81 23 104
Condition66%2/3
$was{$name} and _render_type($was{$name}) ne _render_type($now)
!ll&&!rl&&r
08123
MC/DC50%1/2
$was{$name} and _render_type($was{$name}) ne _render_type($now)
$was{$name} _render_type($was{$name}) ne _render_type($now)
Truth table66%2/3
A and B
A= $was{$name}B= _render_type($was{$name}) ne _render_type($now)
ABresult
0X0
100
111
434 $found{ $_->[0] } = 1
435 23 for _read_by($table_key, $name, $readers, \%children, $source_cols);
436 }
437 }
438 22 return \%found;
439 }
440  
441 3632 ▶sub diff {
Subroutine diff: called CC=59 SCAR=40.9
Pod documented
442 3632 my ($class, $source_cols, $target_cols, $source_tables, $target_tables,
443 $source_types, $target_types, $source_policies, $target_policies,
444 $source_triggers, $target_triggers, $source_outside, $target_outside,
445 $extensions) = @_;
446 3632 my (@ops, @case_only, @extension_refusals);
447 3632 my $others = _other_readers(
448 $source_cols, $source_policies, $source_triggers, $source_outside);
449  
450 # The columns of each table that has been through the loop below, by name:
451 # as the statements for the tables above it leave them, and as the target
452 # has them.
453 3632 my (%before, %after);
454 3632 my $readers = _readers($source_tables);
455 3632 my %children;
456 3632 for my $key (keys %$source_tables) {
457 424 push @{ $children{$_} }, $key for _parents($source_tables->{$key});
458 }
459  
460 # Only diff columns for tables that exist in both source and target.
461 # Brand new tables are handled by Diff::Table as full CREATE TABLE
462 # statements with columns inline. A table comes behind the tables it has
463 # columns from: what is its own is changed after what comes down to it.
464 3632 for my $table_key (_parents_first($source_tables, sort keys %$target_cols)) {
465 3735 ▶ next unless exists $source_tables->{$table_key} && exists $target_tables->{$table_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_tables->{$table_key} and exists $target_tables->{$table_key} 3386 349 3735
Condition66%2/3
exists $source_tables->{$table_key} and exists $target_tables->{$table_key}
!ll&&!rl&&r
33860349
MC/DC50%1/2
exists $source_tables->{$table_key} and exists $target_tables->{$table_key}
exists $source_tables->{$table_key} exists $target_tables->{$table_key}
Truth table66%2/3
A and B
A= exists $source_tables->{$table_key}B= exists $target_tables->{$table_key}
ABresult
0X0
100
111
466 349 ▶ next if grep { DBIO::PostgreSQL::Diff::View->is_view($_->{$table_key}) }
Branch: 1 branch
BranchTrueFalseTotal
if grep {"DBIO::PostgreSQL::Diff::View"->is_view($_->{$table_key});} $source_tables, $target_tables 5 344 349
467 $source_tables, $target_tables;
468  
469 my ($table_name, $schema_name) = reverse DBIO::PostgreSQL::Introspect->key_names(
470 344 $table_key, table_name => $target_tables->{$table_key}, $source_tables->{$table_key},
471 );
472 344 my @table = (
473 table_key => $table_key,
474 schema_name => $schema_name,
475 table_name => $table_name,
476 );
477  
478 344 ▶ my %source_by_name = map { $_->{column_name} => $_ } @{ $source_cols->{$table_key} // [] };
Condition50%1/2
$source_cols->{$table_key} // []
l!l
3440
MC/DC0%0/1
$source_cols->{$table_key} // []
$source_cols->{$table_key}
Truth table50%1/2
A
A= $source_cols->{$table_key}
Aresult
11
00
479 344 ▶ my %target_by_name = map { $_->{column_name} => $_ } @{ $target_cols->{$table_key} // [] };
Condition50%1/2
$target_cols->{$table_key} // []
l!l
3440
MC/DC0%0/1
$target_cols->{$table_key} // []
$target_cols->{$table_key}
Truth table50%1/2
A
A= $target_cols->{$table_key}
Aresult
11
00
480  
481 # What goes down from this table to the ones that have columns from it
482 # is all that changes in it, whether by a statement for it or by one for
483 # a table above it.
484 344 $before{$table_key} = { %source_by_name };
485 344 $after{$table_key} = \%target_by_name;
486  
487 # A partition, or a table that inherits: what the statements for the
488 # tables above it have done to its columns by now.
489 344 ▶ my @parents = grep { $before{$_} && $_ ne $table_key } _parents($source_tables->{$table_key});
Condition33%1/3
$before{$_} && $_ ne $table_key
!ll&&!rl&&r
0070
MC/DC0%0/2
$before{$_} && $_ ne $table_key
$before{$_} $_ ne $table_key
Truth table33%1/3
A && B
A= $before{$_}B= $_ ne $table_key
ABresult
0X0
100
111
490 344 ▶ %source_by_name = %{ _after_cascade(
Branch: 1 branch
BranchTrueFalseTotal
if @parents 64 280 344
491 64 \%source_by_name, \%target_by_name, map { [ $before{$_}, $after{$_} ] } @parents,
492 ) } if @parents;
493 171 ▶ my $from_parents = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
494 171 my ($name) = @_;
495 171 return grep { $after{$_}{$name} } @parents;
496 344 };
497  
498 # A column to add beside one to drop that is the same name but for its
499 # case: refused below, for every table at once.
500 push @case_only, map {
501 4 my ($old, $new) = map { $_->{name} } @$_;
502 {
503 kind => 'column',
504 database => "$table_key.$old",
505 target => "$table_key.$new",
506 rename => sprintf('ALTER TABLE %s RENAME COLUMN %s TO %s;',
507 4 quote_qualified(grep { defined } $schema_name, $table_name),
508 quote_ident($old), quote_ident($new)),
509 }
510 } DBIO::PostgreSQL::Diff::CaseOnly->pairs(
511 344 map { [ map { { in => [], name => $_ } } keys %$_ ] } \%source_by_name, \%target_by_name,
512 );
513  
514 # New columns
515 344 for my $col_name (sort keys %target_by_name) {
516 655 ▶ next if exists $source_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $source_by_name{$col_name} 615 40 655
517 _refuse_not_at_parent(add => $table_key, $col_name, $source_tables->{$table_key}{partition_of})
518 40 ▶ if @parents && defined $source_tables->{$table_key}{partition_of};
Branch: 1 branch
BranchTrueFalseTotal
if @parents and defined $source_tables->{$table_key}{"partition_of"} 1 39 40
Condition100%3/3
@parents and defined $source_tables->{$table_key}{"partition_of"}
!ll&&!rl&&r
3811
MC/DC100%2/2
@parents and defined $source_tables->{$table_key}{"partition_of"}
@parents defined $source_tables->{$table_key}{"partition_of"}
Truth table100%3/3
A and B
A= @parentsB= defined $source_tables->{$table_key}{"partition_of"}
ABresult
0X0
100
111
519 push @ops, $class->new(
520 action => 'add',
521 @table,
522 column_name => $col_name,
523 39 new_info => $target_by_name{$col_name},
524 );
525 }
526  
527 # Dropped columns
528 343 for my $col_name (sort keys %source_by_name) {
529 635 ▶ next if exists $target_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target_by_name{$col_name} 614 21 635
530 21 ▶ _refuse_not_at_parent(drop => $table_key, $col_name, $from_parents->($col_name))
Branch: 1 branch
BranchTrueFalseTotal
if &$from_parents($col_name) 1 20 21
531 if $from_parents->($col_name);
532 push @ops, $class->new(
533 action => 'drop',
534 @table,
535 column_name => $col_name,
536 20 old_info => $source_by_name{$col_name},
537 );
538 }
539  
540 # Altered columns
541 342 for my $col_name (sort keys %target_by_name) {
542 621 ▶ next unless exists $source_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_by_name{$col_name} 39 582 621
543 582 my $src = $source_by_name{$col_name};
544 582 my $tgt = $target_by_name{$col_name};
545  
546 582 my @changed = changed_fields($src, $tgt, %_COLUMN_FIELD_SPEC);
547  
548 # Both models come from a catalog, so a target without a default is a
549 # column without one; the desired-state rule of changed_fields, which
550 # skips a field the target leaves undef, would keep the database's
551 # (karr k150). A generated column keeps its expression there and is
552 # refused below, not dropped.
553 push @changed, 'default_value'
554 199 ▶ if !grep({ $_ eq 'default_value' } @changed)
Condition100%4/4
$src->{"generated"} // ""
l!l
12
$tgt->{"generated"} // ""
l!l
12
MC/DC100%1/1
$src->{"generated"} // ""
$src->{"generated"}
MC/DC100%1/1
$tgt->{"generated"} // ""
$tgt->{"generated"}
Truth table100%2/2
A
A= $src->{"generated"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"generated"}
Aresult
11
00
555 && !defined $tgt->{default_value}
556 && defined $src->{default_value} && length $src->{default_value}
557 582 ▶ && !length($src->{generated} // '') && !length($tgt->{generated} // '');
Branch: 1 branch
BranchTrueFalseTotal
if not grep({$_ eq "default_value";} @changed) || defined $tgt->{"default_value"} and defined $src->{"default_value"} and length $src->{"default_value"} and not length($src->{"generated"} // "") and not length($tgt->{"generated"} // "") 3 579 582
Condition80%12/15
not grep({$_ eq "default_value";} @changed) || defined $tgt->{"default_value"} and defined $src->{"default_value"} and length $src->{"default_value"} and not length($src->{"generated"} // "") and not length($tgt->{"generated"} // "")
!ll&&!rl&&r
57903
not grep({$_ eq "default_value";} @changed) || defined $tgt->{"default_value"} and defined $src->{"default_value"} and length $src->{"default_value"} and not length($src->{"generated"} // "")
!ll&&!rl&&r
57903
not grep({$_ eq "default_value";} @changed) || defined $tgt->{"default_value"} and defined $src->{"default_value"} and length $src->{"default_value"}
!ll&&!rl&&r
57903
not grep({$_ eq "default_value";} @changed) || defined $tgt->{"default_value"} and defined $src->{"default_value"}
!ll&&!rl&&r
335463
grep({$_ eq 'default_value';} @changed) || defined $tgt->{'default_value'}
l!l&&r!l&&!r
1518549
MC/DC50%3/6
not grep({$_ eq "default_value";} @changed) || defined $tgt->{"default_value"} and defined $src->{"default_value"} and length $src->{"default_value"} and not length($src->{"generated"} // "") and not length($tgt->{"generated"} // "")
grep({$_ eq 'default_value';} @changed) defined $tgt->{'default_value'} defined $src->{"default_value"} length $src->{"default_value"} not length($src->{"generated"} // "") not length($tgt->{"generated"} // "")
Truth table57%4/7
not(A || B) and C and D and not(E) and not(F)
A= grep({$_ eq 'default_value';} @changed)B= defined $tgt->{'default_value'}C= defined $src->{"default_value"}D= length $src->{"default_value"}E= not length($src->{"generated"} // "")F= not length($tgt->{"generated"} // "")
ABCDEFresult
1XXXXX0
01XXXX0
000XXX0
0010XX0
00110X0
0011100
0011111
558 582 ▶ next unless @changed;
Branch: 1 branch
BranchTrueFalseTotal
unless @changed 399 183 582
559  
560 # How the column is generated: the mark, and for a generated column
561 # the expression, which the model has in default_value.
562 _refuse_generated_change(
563 $table_key, quote_qualified($schema_name, $table_name), $src, $tgt,
564 ) if ($src->{generated} // '') ne ($tgt->{generated} // '')
565 || (length($tgt->{generated} // '')
566 183 ▶ && ($src->{default_value} // '') ne ($tgt->{default_value} // ''));
Branch: 1 branch
BranchTrueFalseTotal
if ($src->{"generated"} // "") ne ($tgt->{"generated"} // "") or length($tgt->{"generated"} // "") and ($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "") 4 179 183
Condition87%14/16
($src->{"generated"} // "") ne ($tgt->{"generated"} // "") or length($tgt->{"generated"} // "") and ($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "")
l!l&&r!l&&!r
31179
$src->{"generated"} // ""
l!l
63120
$tgt->{"generated"} // ""
l!l
63120
length($tgt->{"generated"} // "") and ($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "")
!ll&&!rl&&r
17811
$tgt->{"generated"} // ""
l!l
60120
$src->{"default_value"} // ""
l!l
20
$tgt->{"default_value"} // ""
l!l
20
MC/DC33%1/3
($src->{"generated"} // "") ne ($tgt->{"generated"} // "") or length($tgt->{"generated"} // "") and ($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "")
$src->{"generated"} $tgt->{"generated"} $src->{"default_value"}
MC/DC0%0/1
$tgt->{"default_value"} // ""
$tgt->{"default_value"}
Truth table50%2/4
A or B and C
A= $src->{"generated"}B= $tgt->{"generated"}C= $src->{"default_value"}
ABCresult
1XX1
0111
00X0
0100
Truth table50%1/2
A
A= $tgt->{"default_value"}
Aresult
11
00
567  
568 # A type that changes: not at a table that only has the column from
569 # another, and not while a view reads the column -- here, or in a table
570 # that has the column from this one.
571 179 my ($from, $to) = map { _render_type($_) } $src, $tgt;
572 179 ▶ if ($from ne $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from ne $to) 150 29 179
573 # Composite to composite: only a type of the same attributes is
574 # converted, through its text.
575 150 ▶ if ((_type_kind($src) // '') eq 'composite' && (_type_kind($tgt) // '') eq 'composite') {
Branch: 1 branch
BranchTrueFalseTotal
if ((_type_kind($src) // "") eq "composite" and (_type_kind($tgt) // "") eq "composite") 4 146 150
Condition100%7/7
(_type_kind($src) // "") eq "composite" and (_type_kind($tgt) // "") eq "composite"
!ll&&!rl&&r
14514
_type_kind($src) // ""
l!l
13137
_type_kind($tgt) // ""
l!l
41
MC/DC100%2/2
(_type_kind($src) // "") eq "composite" and (_type_kind($tgt) // "") eq "composite"
_type_kind($src) _type_kind($tgt)
Truth table100%3/3
A and B
A= _type_kind($src)B= _type_kind($tgt)
ABresult
0X0
100
111
576 4 my ($shape_from, $shape_to)
577 = (_composite_shape($src, $source_types), _composite_shape($tgt, $target_types));
578 4 ▶ _refuse_composite_change(
Branch: 1 branch
BranchTrueFalseTotal
if defined $shape_from and defined $shape_to and $shape_from ne $shape_to 2 2 4
Condition83%5/6
defined $shape_from and defined $shape_to and $shape_from ne $shape_to
!ll&&!rl&&r
112
defined $shape_from and defined $shape_to
!ll&&!rl&&r
103
MC/DC66%2/3
defined $shape_from and defined $shape_to and $shape_from ne $shape_to
defined $shape_from defined $shape_to $shape_from ne $shape_to
Truth table75%3/4
A and B and C
A= defined $shape_fromB= defined $shape_toC= $shape_from ne $shape_to
ABCresult
0XX0
10X0
1100
1111
579 $table_key, quote_qualified($schema_name, $table_name), $col_name, $from, $to, $shape_from, $shape_to)
580 if defined $shape_from && defined $shape_to && $shape_from ne $shape_to;
581 }
582  
583 148 ▶ _refuse_not_at_parent(type => $table_key, $col_name, $from_parents->($col_name))
Branch: 1 branch
BranchTrueFalseTotal
if &$from_parents($col_name) 1 147 148
584 if $from_parents->($col_name);
585  
586 # A policy, a trigger or a generated column of the database, or a
587 # view outside the schemas of the model: not the diff's to take out
588 # of the way.
589 147 ▶ my @others = map { @{ $others->{$_}{$col_name} // [] } }
Condition100%2/2
$others->{$_}{$col_name} // []
l!l
6152
MC/DC100%1/1
$others->{$_}{$col_name} // []
$others->{$_}{$col_name}
Truth table100%2/2
A
A= $others->{$_}{$col_name}
Aresult
11
00
590 _holders($table_key, $col_name, \%children, $source_cols);
591 147 ▶ _refuse_type_change_under_readers($table_key, $col_name, $from, $to, @others)
Branch: 1 branch
BranchTrueFalseTotal
if @others 6 141 147
592 if @others;
593  
594 141 my @read_by = _read_by($table_key, $col_name, $readers, \%children, $source_cols);
595  
596 # Views the target has are dropped and created again by
597 # Diff::View; a view only the database has is named, and so is a
598 # materialized view, which is not built anew for it.
599 141 ▶ if (@read_by) {
Branch: 1 branch
BranchTrueFalseTotal
if (@read_by) 22 119 141
600 my @in_the_way = DBIO::PostgreSQL::Diff::View->with_dependents(
601 22 $source_tables, map { $_->[0] } @read_by);
602 my @undeclared = grep {
603 22 !exists $target_tables->{$_}
604 35 ▶ || !DBIO::PostgreSQL::Diff::View->is_view($target_tables->{$_})
Condition66%2/3
exists $target_tables->{$_} && 'DBIO::PostgreSQL::Diff::View'->is_view($target_tables->{$_})
!ll&&!rl&&r
5030
MC/DC50%1/2
exists $target_tables->{$_} && 'DBIO::PostgreSQL::Diff::View'->is_view($target_tables->{$_})
exists $target_tables->{$_} 'DBIO::PostgreSQL::Diff::View'->is_view($target_tables->{$_})
Truth table66%2/3
A && B
A= exists $target_tables->{$_}B= 'DBIO::PostgreSQL::Diff::View'->is_view($target_tables->{$_})
ABresult
0X0
100
111
605 } @in_the_way;
606 22 ▶ _refuse_type_change_under_undeclared_view(
Branch: 1 branch
BranchTrueFalseTotal
if @undeclared 4 18 22
607 $table_key, $col_name, $from, $to, $source_tables, @undeclared)
608 if @undeclared;
609 18 my @matviews = grep { $source_tables->{$_}{kind} eq 'm' } @in_the_way;
610 18 ▶ _refuse_type_change_under_matview(
Branch: 1 branch
BranchTrueFalseTotal
if @matviews 3 15 18
611 $table_key, $col_name, $from, $to, $source_tables, \@read_by, \@in_the_way, @matviews)
612 if @matviews;
613 }
614 }
615  
616 # What the extensions of the schema say to a change of the type
617 # (ADR 0066, phase 3): a USING of their own, or a refusal.
618 163 my $using;
619 163 ▶ if ($from ne $to && $extensions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from ne $to and $extensions) 33 130 163
Condition100%3/3
$from ne $to and $extensions
!ll&&!rl&&r
2910133
MC/DC100%2/2
$from ne $to and $extensions
$from ne $to $extensions
Truth table100%3/3
A and B
A= $from ne $toB= $extensions
ABresult
0X0
100
111
620 33 my $answer = $extensions->verdict({
621 kind => 'column_type',
622 table => $table_key,
623 schema_name => $schema_name,
624 table_name => $table_name,
625 column_name => $col_name,
626 old_info => $src,
627 new_info => $tgt,
628 old_type => $from,
629 new_type => $to,
630 });
631 33 ▶ if ($answer && $answer->{refuse}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($answer and $answer->{'refuse'}) { } 3 30 33
elsif ($answer) { } 21 9 30
Condition100%3/3
$answer and $answer->{"refuse"}
!ll&&!rl&&r
9213
MC/DC100%2/2
$answer and $answer->{"refuse"}
$answer $answer->{"refuse"}
Truth table100%3/3
A and B
A= $answerB= $answer->{"refuse"}
ABresult
0X0
100
111
632 push @extension_refusals,
633 3 map { "column $table_key.$col_name: $_" } @{ $answer->{refuse} };
634 }
635 elsif ($answer) {
636 21 $using = $answer->{using};
637 }
638 }
639  
640 163 ▶ push @ops, $class->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $using ? : 21 142 163
641 action => 'alter',
642 @table,
643 column_name => $col_name,
644 old_info => $src,
645 new_info => $tgt,
646 (defined $using ? (using => $using) : ()),
647 );
648 163 $ops[-1]->_type_change; # the USING and the loss are decided once, here
649 }
650 }
651  
652 DBIO::PostgreSQL::Diff::CaseOnly->refuse(
653 3608 'DROP COLUMN takes what the column holds, in every row.', @case_only,
654 );
655  
656 die join "\n",
657 'refusing to diff: the extensions of the schema refuse the change of a column type:',
658 3606 ▶ (map { " - $_" } @extension_refusals),
Branch: 1 branch
BranchTrueFalseTotal
if @extension_refusals 2 3604 3606
659 '',
660 'No statement was generated.',
661 ''
662 if @extension_refusals;
663  
664 3604 return @ops;
665 }
666  
667 # 'enum' or 'composite' for a column of such a type, else nothing. The model
668 # the diff reads is the catalog row (Introspect::Columns): data_type is the
669 # type's name and type_category its kind. An already normalized column says
670 # it with data_type 'enum' (Introspect::Normalize).
671 556 ▶sub _type_kind {
Subroutine _type_kind: called CC=6 SCAR=17.9
672 556 my ($info) = @_;
673 556 ▶ my $category = $info->{type_category} // '';
Condition100%2/2
$info->{"type_category"} // ""
l!l
96460
MC/DC100%1/1
$info->{"type_category"} // ""
$info->{"type_category"}
Truth table100%2/2
A
A= $info->{"type_category"}
Aresult
11
00
674 556 ▶ return 'enum' if ($info->{data_type} // '') eq 'enum' || $category eq 'e';
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"data_type"} // "") eq "enum" or $category eq "e" 39 517 556
Condition80%4/5
($info->{"data_type"} // "") eq "enum" or $category eq "e"
l!l&&r!l&&!r
1128517
$info->{"data_type"} // ""
l!l
5560
MC/DC0%0/2
($info->{"data_type"} // "") eq "enum" or $category eq "e"
$info->{"data_type"} $category eq "e"
Truth table33%1/3
A or B
A= $info->{"data_type"}B= $category eq "e"
ABresult
1X1
011
000
675 517 ▶ return 'composite' if $category eq 'c';
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "c" 22 495 517
676 495 return;
677 }
678  
679 # The attributes of the composite type a column has, in order, as one string;
680 # undef where the model of types does not hold it.
681 8 ▶sub _composite_shape {
Subroutine _composite_shape: called CC=6 SCAR=18.6
682 8 my ($info, $types) = @_;
683 8 ▶ return unless ref $types eq 'HASH' && defined $info->{composite_type};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $types eq "HASH" and defined $info->{"composite_type"} 2 6 8
Condition66%2/3
ref $types eq "HASH" and defined $info->{"composite_type"}
!ll&&!rl&&r
206
MC/DC50%1/2
ref $types eq "HASH" and defined $info->{"composite_type"}
ref $types eq "HASH" defined $info->{"composite_type"}
Truth table66%2/3
A and B
A= ref $types eq "HASH"B= defined $info->{"composite_type"}
ABresult
0X0
100
111
684 my $type = $types->{ DBIO::PostgreSQL::Introspect->qualified_key(
685 6 ▶ $info->{type_schema} // 'public', $info->{composite_type}) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $$types{"DBIO::PostgreSQL::Introspect"->qualified_key($info->{"type_schema"} // "public", $info->{"composite_type"})} or return 6 0 6
Condition50%1/2
$info->{"type_schema"} // "public"
l!l
60
MC/DC0%0/1
$info->{"type_schema"} // "public"
$info->{"type_schema"}
Truth table50%1/2
A
A= $info->{"type_schema"}
Aresult
11
00
686 11 return join ', ', map { "$_->{name} $_->{type}" }
687 6 ▶ sort { $a->{ordinal} <=> $b->{ordinal} } @{ $type->{attributes} // [] };
Condition50%1/2
$type->{"attributes"} // []
l!l
60
MC/DC0%0/1
$type->{"attributes"} // []
$type->{"attributes"}
Truth table50%1/2
A
A= $type->{"attributes"}
Aresult
11
00
688 }
689  
690 # A composite type gives the value to another through its text, which is
691 # positional: a type with other attributes would read the same text as other
692 # fields, or fail on it, depending on the rows.
693 2 ▶sub _refuse_composite_change {
Subroutine _refuse_composite_change: called CC=1 SCAR=0.0
694 2 my ($table_key, $table, $column, $from, $to, $shape_from, $shape_to) = @_;
695 2 my ($old, $new) = map { quote_ident($_) } $column, "${column}_new";
696 2 die join "\n",
697 "refusing to diff: column $table_key.$column changes from composite type $from to $to, which has other attributes",
698 '',
699 " $from: ($shape_from)",
700 " $to: ($shape_to)",
701 '',
702 'PostgreSQL has no cast from one composite type to another, and the text of a',
703 'value is read by position: with other attributes it fails on some rows or reads',
704 'the same text as other fields. No statement was generated.',
705 '',
706 'Say how a value of the old type becomes one of the new, with a column of the',
707 'new type beside the old one:',
708 " ALTER TABLE $table ADD COLUMN $new $to;",
709 " UPDATE $table SET $new = ROW(...)::$to;",
710 " ALTER TABLE $table DROP COLUMN $old;",
711 " ALTER TABLE $table RENAME COLUMN $new TO $old;",
712 'and diff again.',
713 '';
714 }
715  
716 # Render a column's PostgreSQL type string from its introspect-shaped info
717 # hashref: data_type plus size. A scalar size becomes data_type(size)
718 # (e.g. varchar(100)); an arrayref size becomes data_type(p,s) (e.g.
719 # numeric(10,2)); no size leaves data_type untouched. Where the modifier
720 # stands is the rule of DBIO::PostgreSQL::Util::type_with_modifier -- behind
721 # the first word of "timestamp with time zone", not at its end. Enum columns
722 # carry the literal 'enum' in data_type with the real PG type name in
723 # pg_enum_type (e.g. auth.role_type), so render that name instead.
724 1282 ▶sub _render_type {
Subroutine _render_type: called CC=5 SCAR=16.1
725 1282 my ($info, $context) = @_;
726 1282 ▶ if (($info->{data_type} // '') eq 'enum' && defined $info->{pg_enum_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($info->{"data_type"} // "") eq "enum" and defined $info->{"pg_enum_type"}) 12 1270 1282
Condition80%4/5
($info->{"data_type"} // "") eq "enum" and defined $info->{"pg_enum_type"}
!ll&&!rl&&r
1270012
$info->{"data_type"} // ""
l!l
12802
MC/DC50%1/2
($info->{"data_type"} // "") eq "enum" and defined $info->{"pg_enum_type"}
$info->{"data_type"} defined $info->{"pg_enum_type"}
Truth table66%2/3
A and B
A= $info->{"data_type"}B= defined $info->{"pg_enum_type"}
ABresult
0X0
100
111
727 12 return $info->{pg_enum_type};
728 }
729 1270 ▶ return type_with_modifier($info->{data_type} // '', $info->{size}, $context);
Condition100%2/2
$info->{"data_type"} // ""
l!l
12682
MC/DC100%1/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
Aresult
11
00
730 }
731  
732 # The pairs of types a column changes between without altering a stored
733 # value, as DBIO::Diff::Compare::column_change_lossy reads them (old->new, in
734 # lower case, of the type names as format_type writes them). What is left out
735 # is left out on purpose: char(n) and bit(n) pad on a change, real to double
736 # precision shows another text, timestamp and timestamptz depend on the
737 # session's time zone, text to varchar(n) cuts.
738 105 my %LOSSLESS_PAIRS = map { $_ => 1 } (
739 'smallint->integer', 'smallint->bigint', 'integer->bigint', 'varchar->text',
740 );
741  
742 # The types whose modifier only grows a limit: a larger one keeps every value
743 # (the core compares the numbers; numeric has its own rule below).
744 105 my %SIZE_LIMIT_ONLY = map { $_ => 1 } (
745 'varchar', 'timestamp', 'timestamp with time zone', 'time', 'time with time zone',
746 );
747  
748 105 my %TYPE_ALIAS = (
749 'character varying' => 'varchar', int2 => 'smallint', int4 => 'integer', int => 'integer',
750 int8 => 'bigint', decimal => 'numeric', timestamptz => 'timestamp with time zone',
751 );
752  
753 # 'numeric(10,2)' as ('numeric', [10, 2]); 'timestamp(3) with time zone' as
754 # ('timestamp with time zone', [3]); a type without modifier has no list.
755 180 ▶sub _split_type {
Subroutine _split_type: called CC=5 SCAR=16.1
756 180 my ($type) = @_;
757 180 ▶ $type = lc($type // '');
Condition50%1/2
$type // ""
l!l
1800
MC/DC0%0/1
$type // ""
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
758 180 $type =~ s/\s+/ /g;
759 180 $type =~ s/\A | \z//g;
760 180 my @params;
761 180 ▶ if ($type =~ /\A([a-z0-9_ ]+?) ?\(([0-9, ]+)\)(.*)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /\A([a-z0-9_ ]+?) ?\(([0-9, ]+)\)(.*)\z/) 50 130 180
762 50 my ($head, $list, $tail) = ($1, $2, $3);
763 50 @params = split /\s*,\s*/, $list;
764 50 $type = "$head$tail";
765 50 $type =~ s/ +\z//;
766 }
767 180 ▶ return ($TYPE_ALIAS{$type} // $type, @params ? \@params : undef);
Branch: 1 branch
BranchTrueFalseTotal
@params ? : 50 130 180
Condition66%2/3
$TYPE_ALIAS{$type} // $type
l!l&&r!l&&!r
121680
MC/DC0%0/2
$TYPE_ALIAS{$type} // $type
$TYPE_ALIAS{$type} $type
Truth table66%2/3
A // B
A= $TYPE_ALIAS{$type}B= $type
ABresult
1X1
011
000
768 }
769  
770 # True when the change from the type $old to the type $new (as rendered, with
771 # modifier) may alter or lose a stored value. Anything not known to keep every
772 # value is lossy.
773 90 ▶sub _type_change_is_lossy {
Subroutine _type_change_is_lossy: called CC=18 SCAR=29.0
774 90 my ($old, $new) = @_;
775 90 ▶ return 0 if lc($old) eq lc($new);
Branch: 1 branch
BranchTrueFalseTotal
if lc $old eq lc $new 0 90 90
776 90 my ($old_base, $old_params) = _split_type($old);
777 90 my ($new_base, $new_params) = _split_type($new);
778  
779 90 ▶ if ($old_base eq 'numeric' && $new_base eq 'numeric') {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_base eq "numeric" and $new_base eq "numeric") 10 80 90
Condition66%2/3
$old_base eq "numeric" and $new_base eq "numeric"
!ll&&!rl&&r
80010
MC/DC50%1/2
$old_base eq "numeric" and $new_base eq "numeric"
$old_base eq "numeric" $new_base eq "numeric"
Truth table66%2/3
A and B
A= $old_base eq "numeric"B= $new_base eq "numeric"
ABresult
0X0
100
111
780 10 ▶ return 0 unless $new_params; # numeric without limit takes every value
Branch: 1 branch
BranchTrueFalseTotal
unless $new_params 1 9 10
781 9 ▶ return 1 unless $old_params;
Branch: 1 branch
BranchTrueFalseTotal
unless $old_params 1 8 9
782 8 ▶ my ($p, $s) = ($old_params->[0], $old_params->[1] // 0);
Condition50%1/2
$old_params->[1] // 0
l!l
80
MC/DC0%0/1
$old_params->[1] // 0
$old_params->[1]
Truth table50%1/2
A
A= $old_params->[1]
Aresult
11
00
783 8 ▶ my ($np, $ns) = ($new_params->[0], $new_params->[1] // 0);
Condition50%1/2
$new_params->[1] // 0
l!l
80
MC/DC0%0/1
$new_params->[1] // 0
$new_params->[1]
Truth table50%1/2
A
A= $new_params->[1]
Aresult
11
00
784 # the scale does not shrink and the digits before the point do not shrink
785 8 ▶ return ($ns >= $s && $np - $ns >= $p - $s) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ns >= $s && $np - $ns >= $p - $s ? : 5 3 8
Condition100%3/3
$ns >= $s and $np - $ns >= $p - $s
!ll&&!rl&&r
125
MC/DC100%2/2
$ns >= $s and $np - $ns >= $p - $s
$ns >= $s $np - $ns >= $p - $s
Truth table100%3/3
A and B
A= $ns >= $sB= $np - $ns >= $p - $s
ABresult
0X0
100
111
786 }
787 80 ▶ return 0 if $old_base eq 'text' && $new_base eq 'varchar' && !$new_params; # the same strings
Branch: 1 branch
BranchTrueFalseTotal
if $old_base eq "text" and $new_base eq "varchar" and not $new_params 1 79 80
Condition100%6/6
$old_base eq "text" and $new_base eq "varchar" and not $new_params
!ll&&!rl&&r
7631
$old_base eq "text" and $new_base eq "varchar"
!ll&&!rl&&r
61154
MC/DC100%3/3
$old_base eq "text" and $new_base eq "varchar" and not $new_params
$old_base eq "text" $new_base eq "varchar" not $new_params
Truth table100%4/4
A and B and not(C)
A= $old_base eq "text"B= $new_base eq "varchar"C= not $new_params
ABCresult
0XX0
10X0
1100
1111
788 79 ▶ return 1 if $old_base eq $new_base && !$SIZE_LIMIT_ONLY{$old_base};
Branch: 1 branch
BranchTrueFalseTotal
if $old_base eq $new_base and not $SIZE_LIMIT_ONLY{$old_base} 1 78 79
Condition100%3/3
$old_base eq $new_base and not $SIZE_LIMIT_ONLY{$old_base}
!ll&&!rl&&r
6991
MC/DC100%2/2
$old_base eq $new_base and not $SIZE_LIMIT_ONLY{$old_base}
$old_base eq $new_base not $SIZE_LIMIT_ONLY{$old_base}
Truth table100%3/3
A and not(B)
A= $old_base eq $new_baseB= not $SIZE_LIMIT_ONLY{$old_base}
ABresult
0X0
100
111
789 78 ▶ return column_change_lossy(
Branch: 3 branches
BranchTrueFalseTotal
column_change_lossy({'data_type', $old_base, defined $old_params ? ('size', $old_params) : ()}, {'data_type', $new_base, defined $new_params ? ('size', $new_params) : ()}, \%LOSSLESS_PAIRS) ? : 39 39 78
defined $old_params ? : 11 67 78
defined $new_params ? : 19 59 78
790 { data_type => $old_base, (defined $old_params ? (size => $old_params) : ()) },
791 { data_type => $new_base, (defined $new_params ? (size => $new_params) : ()) },
792 \%LOSSLESS_PAIRS,
793 ) ? 1 : 0;
794 }
795  
796 # What the change of the column's type comes to, decided once for the
797 # operation and kept: the rendered types, the USING (given by an extension, or
798 # by a hook of register_using_hook) and whether the change may alter a stored
799 # value. L</lossy> and L</as_sql> read it, so that the statement and the answer
800 # to the guard of the deploy cannot differ.
801 322 ▶sub _type_change {
Subroutine _type_change: called CC=17 SCAR=29.1
802 322 my ($self) = @_;
803 322 ▶ return $self->{_type_change} //= do {
Condition66%2/3
$self->{"_type_change"} //= do { () = ($self->old_info // {}, $self->new_info // {}); my $context = ($self->table_key // "") . '.' . ($self->column_name // ""); () = (_render_type($src, $context), _render_type($tgt, $context)); my $using; if ($src_type ne $tgt_type) { $using = defined $self->using && length $self->using ? $self->using : (_type_kind($tgt) ? undef : _using_from_hooks($self, quote_ident($self->column_name // ""), $src, $tgt, $src_type, $tgt_type)); } ; my $lossy = $src_type eq $tgt_type ? 0 : (defined $using && length $using || _type_kind($tgt) || _type_kind($src) || _type_change_is_lossy($src_type, $tgt_type) ? 1 : 0); +{"src_type", $src_type, "tgt_type", $tgt_type, "using", $using, "lossy", $lossy} }
l!l&&r!l&&!r
1551670
MC/DC0%0/2
$self->{"_type_change"} //= do { () = ($self->old_info // {}, $self->new_info // {}); my $context = ($self->table_key // "") . '.' . ($self->column_name // ""); () = (_render_type($src, $context), _render_type($tgt, $context)); my $using; if ($src_type ne $tgt_type) { $using = defined $self->using && length $self->using ? $self->using : (_type_kind($tgt) ? undef : _using_from_hooks($self, quote_ident($self->column_name // ""), $src, $tgt, $src_type, $tgt_type)); } ; my $lossy = $src_type eq $tgt_type ? 0 : (defined $using && length $using || _type_kind($tgt) || _type_kind($src) || _type_change_is_lossy($src_type, $tgt_type) ? 1 : 0); +{"src_type", $src_type, "tgt_type", $tgt_type, "using", $using, "lossy", $lossy} }
$self->{"_type_change"} do { () = ($self->old_info // {}, $self->new_info // {}); my $context = ($self->table_key // "") . '.' . ($self->column_name // ""); () = (_render_type($src, $context), _render_type($tgt, $context)); my $using; if ($src_type ne $tgt_type) { $using = defined $self->using && length $self->using ? $self->using : (_type_kind($tgt) ? undef : _using_from_hooks($self, quote_ident($self->column_name // ""), $src, $tgt, $src_type, $tgt_type)); } ; my $lossy = $src_type eq $tgt_type ? 0 : (defined $using && length $using || _type_kind($tgt) || _type_kind($src) || _type_change_is_lossy($src_type, $tgt_type) ? 1 : 0); +{"src_type", $src_type, "tgt_type", $tgt_type, "using", $using, "lossy", $lossy} }
Truth table66%2/3
A //= B
A= $self->{"_type_change"}B= do { () = ($self->old_info // {}, $self->new_info // {}); my $context = ($self->table_key // "") . '.' . ($self->column_name // ""); () = (_render_type($src, $context), _render_type($tgt, $context)); my $using; if ($src_type ne $tgt_type) { $using = defined $self->using && length $self->using ? $self->using : (_type_kind($tgt) ? undef : _using_from_hooks($self, quote_ident($self->column_name // ""), $src, $tgt, $src_type, $tgt_type)); } ; my $lossy = $src_type eq $tgt_type ? 0 : (defined $using && length $using || _type_kind($tgt) || _type_kind($src) || _type_change_is_lossy($src_type, $tgt_type) ? 1 : 0); +{"src_type", $src_type, "tgt_type", $tgt_type, "using", $using, "lossy", $lossy} }
ABresult
1X1
011
000
804 167 ▶ my ($src, $tgt) = ($self->old_info // {}, $self->new_info // {});
Condition50%2/4
$self->old_info // {}
l!l
1670
$self->new_info // {}
l!l
1670
MC/DC0%0/1
$self->old_info // {}
$self->old_info
MC/DC0%0/1
$self->new_info // {}
$self->new_info
Truth table50%1/2
A
A= $self->old_info
Aresult
11
00
Truth table50%1/2
A
A= $self->new_info
Aresult
11
00
805 167 ▶ my $context = ($self->table_key // '') . '.' . ($self->column_name // '');
Condition50%2/4
$self->table_key // ""
l!l
1670
$self->column_name // ""
l!l
1670
MC/DC0%0/1
$self->table_key // ""
$self->table_key
MC/DC0%0/1
$self->column_name // ""
$self->column_name
Truth table50%1/2
A
A= $self->table_key
Aresult
11
00
Truth table50%1/2
A
A= $self->column_name
Aresult
11
00
806 167 my ($src_type, $tgt_type) = (_render_type($src, $context), _render_type($tgt, $context));
807 167 my $using;
808 167 ▶ if ($src_type ne $tgt_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_type ne $tgt_type) 134 33 167
809 134 ▶ $using = defined $self->using && length $self->using ? $self->using
Branch: 2 branches
BranchTrueFalseTotal
defined $self->using && length $self->using ? : 21 113 134
_type_kind($tgt) ? : 14 99 113
Condition60%3/5
defined $self->using and length $self->using
!ll&&!rl&&r
113021
$self->column_name // ""
l!l
990
MC/DC50%1/2
defined $self->using and length $self->using
defined $self->using length $self->using
MC/DC0%0/1
$self->column_name // ""
$self->column_name
Truth table66%2/3
A and B
A= defined $self->usingB= length $self->using
ABresult
0X0
100
111
Truth table50%1/2
A
A= $self->column_name
Aresult
11
00
810 : _type_kind($tgt) ? undef
811 : _using_from_hooks($self, quote_ident($self->column_name // ''), $src, $tgt, $src_type, $tgt_type);
812 }
813 165 ▶ my $lossy = $src_type eq $tgt_type ? 0
Branch: 2 branches
BranchTrueFalseTotal
$src_type eq $tgt_type ? : 33 132 165
defined $using && length $using || _type_kind($tgt) || _type_kind($src) || _type_change_is_lossy($src_type, $tgt_type) ? : 86 46 132
Condition91%11/12
defined $using and length $using or _type_kind($tgt) or _type_kind($src) or _type_change_is_lossy($src_type, $tgt_type)
l!l&&r!l&&!r
424446
defined $using and length $using or _type_kind($tgt) or _type_kind($src)
l!l&&r!l&&!r
39390
defined $using and length $using or _type_kind($tgt)
l!l&&r!l&&!r
251493
defined $using and length $using
!ll&&!rl&&r
107025
MC/DC80%4/5
defined $using and length $using or _type_kind($tgt) or _type_kind($src) or _type_change_is_lossy($src_type, $tgt_type)
defined $using length $using _type_kind($tgt) _type_kind($src) _type_change_is_lossy($src_type, $tgt_type)
Truth table55%5/9
A and B or C or D or E
A= defined $usingB= length $usingC= _type_kind($tgt)D= _type_kind($src)E= _type_change_is_lossy($src_type, $tgt_type)
ABCDEresult
11XXX1
0X1XX1
101XX1
0X01X1
1001X1
0X0011
100011
0X0000
100000
814 : (defined $using && length $using) || _type_kind($tgt) || _type_kind($src)
815 || _type_change_is_lossy($src_type, $tgt_type) ? 1 : 0;
816 165 { src_type => $src_type, tgt_type => $tgt_type, using => $using, lossy => $lossy };
817 };
818 }
819  
820  
821 43 ▶sub lossy {
Subroutine lossy: called CC=5 SCAR=16.7
Pod documented
822 43 my ($self) = @_;
823 43 ▶ return 0 unless ($self->action // '') eq 'alter' && $self->old_info && $self->new_info;
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->action // "") eq "alter" and $self->old_info and $self->new_info 2 41 43
Condition62%5/8
($self->action // "") eq "alter" and $self->old_info and $self->new_info
!ll&&!rl&&r
2041
($self->action // "") eq "alter" and $self->old_info
!ll&&!rl&&r
2041
$self->action // ""
l!l
430
MC/DC0%0/3
($self->action // "") eq "alter" and $self->old_info and $self->new_info
$self->action $self->old_info $self->new_info
Truth table25%1/4
A and B and C
A= $self->actionB= $self->old_infoC= $self->new_info
ABCresult
0XX0
10X0
1100
1111
824 41 return $self->_type_change->{lossy};
825 }
826  
827 # The registry of USING hooks: [ name, coderef ] in registration order.
828 105 my @_USING_HOOKS;
829  
830  
831 9 ▶sub register_using_hook {
Subroutine register_using_hook: called CC=5 SCAR=18.1
Pod documented
832 9 my ($class, $name, $code) = @_;
833 9 ▶ die "register_using_hook: a name is required\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and length $name 0 9 9
Condition33%2/6
defined $name and not ref $name and length $name
!ll&&!rl&&r
009
defined $name and not ref $name
!ll&&!rl&&r
009
MC/DC0%0/3
defined $name and not ref $name and length $name
defined $name not ref $name length $name
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= length $name
ABCresult
0XX0
10X0
1100
1111
834 unless defined $name && !ref $name && length $name;
835 9 ▶ die "register_using_hook: hook '$name' is not a code reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 9 9
836 unless ref $code eq 'CODE';
837 9 @_USING_HOOKS = grep { $_->[0] ne $name } @_USING_HOOKS;
838 9 push @_USING_HOOKS, [ $name, $code ];
839 9 return;
840 }
841  
842  
843 9 ▶sub unregister_using_hook {
Subroutine unregister_using_hook: called CC=2 SCAR=7.0
Pod documented
844 9 my ($class, $name) = @_;
845 9 ▶ @_USING_HOOKS = grep { $_->[0] ne ($name // '') } @_USING_HOOKS;
Condition50%1/2
$name // ""
l!l
150
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
846 9 return;
847 }
848  
849 99 ▶sub _using_from_hooks {
Subroutine _using_from_hooks: called CC=6 SCAR=17.9
850 99 my ($self, $column, $src, $tgt, $src_type, $tgt_type) = @_;
851 99 for my $hook (@_USING_HOOKS) {
852 8 my ($name, $code) = @$hook;
853 8 my $using = $code->({
854 table_key => $self->table_key,
855 column_name => $self->column_name,
856 column => $column,
857 old_info => $src,
858 new_info => $tgt,
859 old_type => $src_type,
860 new_type => $tgt_type,
861 });
862 7 ▶ next unless defined $using;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $using 1 6 7
863 6 ▶ die sprintf "USING hook '%s' for column %s.%s returned a %s reference, not a plain string\n",
Branch: 1 branch
BranchTrueFalseTotal
if ref $using 1 5 6
Condition50%1/2
$self->table_key // ""
l!l
10
MC/DC0%0/1
$self->table_key // ""
$self->table_key
Truth table50%1/2
A
A= $self->table_key
Aresult
11
00
864 $name, $self->table_key // '', $self->column_name, ref $using
865 if ref $using;
866 5 ▶ next unless length $using;
Branch: 1 branch
BranchTrueFalseTotal
unless length $using 1 4 5
867 4 return $using;
868 }
869 93 return undef;
870 }
871  
872  
873 227 ▶sub as_sql {
Subroutine as_sql: called CC=39 SCAR=36.7
Pod documented
874 227 my ($self) = @_;
875 227 my $table = quote_qualified(
876 DBIO::PostgreSQL::Introspect->key_names($self->table_key, table_name => $self));
877 227 my $column = quote_ident($self->column_name);
878 227 ▶ my $object = sprintf 'column %s.%s', $self->table_key // '', $self->column_name;
Condition50%1/2
$self->table_key // ""
l!l
2270
MC/DC0%0/1
$self->table_key // ""
$self->table_key
Truth table50%1/2
A
A= $self->table_key
Aresult
11
00
879 227 ▶ my $context = ($self->table_key // '') . '.' . $self->column_name;
Condition50%1/2
$self->table_key // ""
l!l
2270
MC/DC0%0/1
$self->table_key // ""
$self->table_key
Truth table50%1/2
A
A= $self->table_key
Aresult
11
00
880  
881 227 ▶ if ($self->action eq 'add') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->action eq 'add') { } 64 163 227
elsif ($self->action eq 'drop') { } 44 119 163
elsif ($self->action eq 'alter') { } 118 1 119
882 64 my $type = _render_type($self->new_info, $context);
883 64 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /\S/ 4 60 64
884 $self, $object, 'its new_info has no data_type, and a column is added with a type',
885 ) unless $type =~ /\S/;
886 60 my $sql = sprintf 'ALTER TABLE %s ADD COLUMN %s %s',
887 $table, $column, $type;
888 60 ▶ $sql .= ' NOT NULL' if $self->new_info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $self->new_info->{"not_null"} 8 52 60
889 60 my $kind = DBIO::PostgreSQL::Introspect->identity_kind($self->new_info->{identity});
890 60 ▶ if (my $generated = DBIO::PostgreSQL::Introspect->generated_kind($self->new_info->{generated})) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $generated = 'DBIO::PostgreSQL::Introspect'->generated_kind($self->new_info->{'generated'})) { } 8 52 60
elsif ($kind) { } 3 49 52
elsif (defined $self->new_info->{'default_value'}) { } 3 46 49
891 # default_value is the expression the column is computed from
892 $sql .= sprintf ' GENERATED ALWAYS AS (%s) %s',
893 8 $self->new_info->{default_value}, $generated;
894 }
895 elsif ($kind) {
896 3 $sql .= " GENERATED $kind AS IDENTITY";
897 }
898 elsif (defined $self->new_info->{default_value}) {
899 3 $sql .= sprintf ' DEFAULT %s', $self->new_info->{default_value};
900 }
901 60 return "$sql;";
902 }
903 elsif ($self->action eq 'drop') {
904 44 return sprintf 'ALTER TABLE %s DROP COLUMN %s;',
905 $table, $column;
906 }
907 elsif ($self->action eq 'alter') {
908 118 my @stmts;
909 118 my $src = $self->old_info;
910 118 my $tgt = $self->new_info;
911  
912 # Compare the fully rendered type strings (data_type + size), so a
913 # size-only change (varchar(50) -> varchar(100), numeric(10,2) ->
914 # numeric(12,2)) emits an ALTER even though data_type is unchanged.
915 118 my $change = $self->_type_change;
916 118 my ($src_type, $tgt_type) = @{$change}{qw( src_type tgt_type )};
917 118 my $default_dropped;
918 118 ▶ if ($src_type ne $tgt_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_type ne $tgt_type) 88 30 118
919 88 my $stmt = sprintf 'ALTER TABLE %s ALTER COLUMN %s TYPE %s',
920 $table, $column, $tgt_type;
921 # An enum or composite target has no implicit cast from the previous
922 # type; route the conversion through text so the generated ALTER is
923 # executable. The server also casts the column's default to the new
924 # type and cannot do so for these, so the default stands out of the way
925 # (karr k121).
926 88 ▶ if (_type_kind($tgt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_type_kind($tgt)) { } 13 75 88
927 $stmt .= defined $change->{using} && length $change->{using}
928 ? ' USING ' . $change->{using}
929 13 ▶ : sprintf ' USING %s::text::%s', $column, $tgt_type;
Branch: 1 branch
BranchTrueFalseTotal
defined $change->{'using'} && length $change->{'using'} ? : 1 12 13
Condition66%2/3
defined $change->{"using"} and length $change->{"using"}
!ll&&!rl&&r
1201
MC/DC50%1/2
defined $change->{"using"} and length $change->{"using"}
defined $change->{"using"} length $change->{"using"}
Truth table66%2/3
A and B
A= defined $change->{"using"}B= length $change->{"using"}
ABresult
0X0
100
111
930 13 ▶ if (defined $src->{default_value} && $src->{default_value} ne ''
Branch: 1 branch
BranchTrueFalseTotal
if (defined $src->{"default_value"} and $src->{"default_value"} ne "" and not length($src->{"generated"} // "")) 3 10 13
Condition62%5/8
defined $src->{"default_value"} and $src->{"default_value"} ne "" and not length($src->{"generated"} // "")
!ll&&!rl&&r
1003
defined $src->{"default_value"} and $src->{"default_value"} ne ""
!ll&&!rl&&r
1003
$src->{"generated"} // ""
l!l
03
MC/DC33%1/3
defined $src->{"default_value"} and $src->{"default_value"} ne "" and not length($src->{"generated"} // "")
defined $src->{"default_value"} $src->{"default_value"} ne "" $src->{"generated"}
Truth table50%2/4
A and B and not(C)
A= defined $src->{"default_value"}B= $src->{"default_value"} ne ""C= $src->{"generated"}
ABCresult
0XX0
10X0
1110
1101
931 && !length($src->{generated} // '')) {
932 3 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;',
933 $table, $column;
934 3 $default_dropped = 1;
935 }
936 }
937 else {
938 75 ▶ $stmt .= " USING $change->{using}" if defined $change->{using};
Branch: 1 branch
BranchTrueFalseTotal
if defined $change->{"using"} 26 49 75
939 }
940 88 push @stmts, "$stmt;";
941 }
942 118 ▶ if (($src->{not_null} // 0) != ($tgt->{not_null} // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($src->{"not_null"} // 0) != ($tgt->{"not_null"} // 0)) 13 105 118
Condition100%4/4
$src->{"not_null"} // 0
l!l
9622
$tgt->{"not_null"} // 0
l!l
9622
MC/DC100%1/1
$src->{"not_null"} // 0
$src->{"not_null"}
MC/DC100%1/1
$tgt->{"not_null"} // 0
$tgt->{"not_null"}
Truth table100%2/2
A
A= $src->{"not_null"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"not_null"}
Aresult
11
00
943 13 ▶ if ($tgt->{not_null}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tgt->{'not_null'}) { } 8 5 13
944 8 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;',
945 $table, $column;
946 } else {
947 5 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;',
948 $table, $column;
949 }
950 }
951 # Identity transitions. attidentity is '' (none), 'a' (ALWAYS), 'd' (BY
952 # DEFAULT). An identity and a default do not stand on one column, so the
953 # one goes before the other comes: DROP IDENTITY ahead of SET DEFAULT
954 # (column ... is an identity column), DROP DEFAULT ahead of ADD GENERATED
955 # (column ... already has a default value).
956 118 ▶ my $src_id = $src->{identity} // '';
Condition100%2/2
$src->{"identity"} // ""
l!l
4771
MC/DC100%1/1
$src->{"identity"} // ""
$src->{"identity"}
Truth table100%2/2
A
A= $src->{"identity"}
Aresult
11
00
957 118 ▶ my $tgt_id = $tgt->{identity} // '';
Condition100%2/2
$tgt->{"identity"} // ""
l!l
4870
MC/DC100%1/1
$tgt->{"identity"} // ""
$tgt->{"identity"}
Truth table100%2/2
A
A= $tgt->{"identity"}
Aresult
11
00
958 118 my $src_kind = DBIO::PostgreSQL::Introspect->identity_kind($src_id);
959 118 my $tgt_kind = DBIO::PostgreSQL::Introspect->identity_kind($tgt_id);
960 118 ▶ if ($src_kind && !$tgt_kind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_kind and not $tgt_kind) 2 116 118
Condition100%3/3
$src_kind and not $tgt_kind
!ll&&!rl&&r
11422
MC/DC100%2/2
$src_kind and not $tgt_kind
$src_kind not $tgt_kind
Truth table100%3/3
A and not(B)
A= $src_kindB= not $tgt_kind
ABresult
0X0
100
111
961 2 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s DROP IDENTITY;',
962 $table, $column;
963 }
964  
965 118 ▶ if ($default_dropped || ($src->{default_value} // '') ne ($tgt->{default_value} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_dropped or ($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "")) 17 101 118
Condition100%7/7
$default_dropped or ($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "")
l!l&&r!l&&!r
314101
$src->{"default_value"} // ""
l!l
7108
$tgt->{"default_value"} // ""
l!l
13102
MC/DC100%2/2
$default_dropped or ($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "")
$default_dropped $src->{"default_value"}
MC/DC100%1/1
$tgt->{"default_value"} // ""
$tgt->{"default_value"}
Truth table100%3/3
A or B
A= $default_droppedB= $src->{"default_value"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $tgt->{"default_value"}
Aresult
11
00
966 17 ▶ if (defined $tgt->{default_value} && $tgt->{default_value} ne '') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $tgt->{'default_value'} and $tgt->{'default_value'} ne '') { } 13 4 17
elsif (not $default_dropped) { } 3 1 4
Condition66%2/3
defined $tgt->{"default_value"} and $tgt->{"default_value"} ne ""
!ll&&!rl&&r
4013
MC/DC50%1/2
defined $tgt->{"default_value"} and $tgt->{"default_value"} ne ""
defined $tgt->{"default_value"} $tgt->{"default_value"} ne ""
Truth table66%2/3
A and B
A= defined $tgt->{"default_value"}B= $tgt->{"default_value"} ne ""
ABresult
0X0
100
111
967 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;',
968 13 $table, $column, $tgt->{default_value};
969 } elsif (!$default_dropped) {
970 3 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;',
971 $table, $column;
972 }
973 }
974  
975 118 ▶ if ($src_id ne $tgt_id && $tgt_kind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_id ne $tgt_id and $tgt_kind) 8 110 118
Condition100%3/3
$src_id ne $tgt_id and $tgt_kind
!ll&&!rl&&r
10828
MC/DC100%2/2
$src_id ne $tgt_id and $tgt_kind
$src_id ne $tgt_id $tgt_kind
Truth table100%3/3
A and B
A= $src_id ne $tgt_idB= $tgt_kind
ABresult
0X0
100
111
976 8 ▶ push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s %s;', $table, $column,
Branch: 1 branch
BranchTrueFalseTotal
$src_kind ? : 2 6 8
977 ( $src_kind ? "SET GENERATED $tgt_kind" : "ADD GENERATED $tgt_kind AS IDENTITY" );
978 }
979 DBIO::PostgreSQL::Diff::NoStatement->throw(
980 118 ▶ $self, $object,
Branch: 1 branch
BranchTrueFalseTotal
unless @stmts 1 117 118
981 'old_info and new_info differ in nothing the diff has a statement for (type, NOT NULL, default, identity)',
982 ) unless @stmts;
983 117 return join "\n", @stmts;
984 }
985 1 DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object);
986 }
987  
988  
989 5 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
990 5 my ($self) = @_;
991 5 my $prefix = $self->summary_prefix;
992 5 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 4 1 5
993 5 return sprintf ' %scolumn: %s.%s%s', $prefix, $self->table_key, $self->column_name, $type;
994 }
995  
996 105 1;
997  
998 __END__
« blib/lib/DBIO/PostgreSQL/SQLMaker.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Deploy.pm »