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

filter: Statementstmt 97.9% 233 / 238 Branchbran 92.5% 87 / 94 Conditioncond 69.3% 52 / 75 MC/DCmcdc 40.0% 16 / 40 Subroutinesub 100% 20 / 20 Podpod 100% 3 / 3 totaltotal 87.4% 411 / 470 CC 91 SCAR 28.9
CC
91
Cov
91%
_type_change
32.9
as_sql
20.1
diff
19.0
CRAP 18.1
« blib/lib/DBIO/PostgreSQL/Introspect/OutsideReaders.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/Triggers.pm »
1 package DBIO::PostgreSQL::Diff::Type;
2 # ABSTRACT: Diff operations for PostgreSQL types (enums, composites, ranges)
3  
4 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 106 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 106 our $VERSION = '0.900007';
8  
9 106 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 106 ▶use DBIO::PostgreSQL::Diff::CaseOnly ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 106 ▶use DBIO::PostgreSQL::Diff::Dependents ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 106 ▶use DBIO::PostgreSQL::Diff::NoStatement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 106 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 106 ▶use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified quote_literal);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 106 __PACKAGE__->mk_diff_accessors(qw(type_key type_info added_values value_positions));
18  
19  
20  
21  
22  
23  
24  
25 # The types of one section as DBIO::PostgreSQL::Diff::CaseOnly compares them:
26 # a name within its schema, among the types of its kind.
27 116 ▶sub _named {
Subroutine _named: called CC=4 SCAR=14.0
28 116 my ($types) = @_;
29 116 my @entries;
30 116 for my $key (keys %$types) {
31 116 my $info = $types->{$key};
32 116 my ($schema, $name) = DBIO::PostgreSQL::Introspect->key_names($key, type_name => $info);
33 116 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 116 116
34 push @entries, {
35 116 ▶ in => [ $schema, $info->{type_kind} // '' ], name => $name, schema => $schema,
Condition50%1/2
$info->{"type_kind"} // ""
l!l
1160
MC/DC0%0/1
$info->{"type_kind"} // ""
$info->{"type_kind"}
Truth table50%1/2
A
A= $info->{"type_kind"}
Aresult
11
00
36 };
37 }
38 116 return \@entries;
39 }
40  
41 58 ▶sub _refuse_case_only {
Subroutine _refuse_case_only: called CC=1 SCAR=0.0
42 58 my ($source, $target) = @_;
43 DBIO::PostgreSQL::Diff::CaseOnly->refuse(
44 "A column does not go from the one type to the other without a cast, and DROP\n"
45 . 'TYPE of the old one meets every column that is still of it.',
46 map {
47 58 my ($old, $new) = @$_;
48 3 my $schema = $old->{schema};
49 {
50 kind => 'type',
51 database => "$schema.$old->{name}",
52 target => "$schema.$new->{name}",
53 rename => sprintf('ALTER TYPE %s RENAME TO %s;',
54 3 quote_qualified($schema, $old->{name}), quote_ident($new->{name})),
55 }
56 } DBIO::PostgreSQL::Diff::CaseOnly->pairs(_named($source), _named($target)),
57 );
58 }
59  
60 58 ▶sub _kind_name {
Subroutine _kind_name: called CC=3 SCAR=11.7
61 58 my ($info) = @_;
62 58 ▶ my $kind = $info->{type_kind} // '';
Condition50%1/2
$info->{"type_kind"} // ""
l!l
580
MC/DC0%0/1
$info->{"type_kind"} // ""
$info->{"type_kind"}
Truth table50%1/2
A
A= $info->{"type_kind"}
Aresult
11
00
63 58 ▶ return $kind eq '' ? 'type' : "$kind type";
Branch: 1 branch
BranchTrueFalseTotal
$kind eq '' ? : 0 58 58
64 }
65  
66 # An attribute as the diff compares and shows it: name, type, and the
67 # collation where it has one of its own.
68 30 ▶sub _attribute_text {
Subroutine _attribute_text: called CC=3 SCAR=11.0
69 30 my ($attr) = @_;
70 return "$attr->{name} $attr->{type}"
71 30 ▶ . ( ($attr->{collation} // '') ne '' ? " COLLATE $attr->{collation}" : '' );
Branch: 1 branch
BranchTrueFalseTotal
($attr->{'collation'} // '') ne '' ? : 5 25 30
Condition100%2/2
$attr->{"collation"} // ""
l!l
525
MC/DC100%1/1
$attr->{"collation"} // ""
$attr->{"collation"}
Truth table100%2/2
A
A= $attr->{"collation"}
Aresult
11
00
72 }
73  
74 8 ▶sub _attribute_lines {
Subroutine _attribute_lines: called CC=1 SCAR=0.0
75 8 my ($sign, $list) = @_;
76 8 return map { " $sign " . _attribute_text($_) } @$list;
77 }
78  
79 # What a range type sets besides its subtype, as the keys of its entry and
80 # the words the refusal uses for them.
81 106 my @RANGE_EXTRAS = (
82 [ collation => 'collation' ],
83 [ subtype_opclass => 'subtype_opclass' ],
84 [ canonical => 'canonical' ],
85 [ subtype_diff => 'subtype_diff' ],
86 );
87  
88 # What differs between the same type in the database ($src) and in the target
89 # ($tgt), as the lines of one refusal; none where the diff has a statement
90 # for the change (new values at the end of an enum) or there is no change.
91 44 ▶sub _type_change {
Subroutine _type_change: called CC=32 SCAR=34.9
92 44 my ($key, $src, $tgt) = @_;
93 44 ▶ my $src_kind = $src->{type_kind} // '';
Condition50%1/2
$src->{"type_kind"} // ""
l!l
440
MC/DC0%0/1
$src->{"type_kind"} // ""
$src->{"type_kind"}
Truth table50%1/2
A
A= $src->{"type_kind"}
Aresult
11
00
94 44 ▶ my $tgt_kind = $tgt->{type_kind} // '';
Condition50%1/2
$tgt->{"type_kind"} // ""
l!l
440
MC/DC0%0/1
$tgt->{"type_kind"} // ""
$tgt->{"type_kind"}
Truth table50%1/2
A
A= $tgt->{"type_kind"}
Aresult
11
00
95 44 my $what = _kind_name($tgt);
96  
97 # A type the target declares without values or attributes, which the
98 # database has with some: the target means an empty type, not "no type",
99 # and the diff has no statement for it that keeps the data (a drop would
100 # take every column of the type with its rows).
101 44 ▶ if ($src_kind eq $tgt_kind && ($tgt_kind eq 'enum' || $tgt_kind eq 'composite')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_kind eq $tgt_kind and $tgt_kind eq "enum" || $tgt_kind eq "composite") 27 17 44
Condition100%6/6
$src_kind eq $tgt_kind and $tgt_kind eq "enum" || $tgt_kind eq "composite"
!ll&&!rl&&r
11627
$tgt_kind eq "enum" or $tgt_kind eq "composite"
l!l&&r!l&&!r
20716
MC/DC100%3/3
$src_kind eq $tgt_kind and $tgt_kind eq "enum" || $tgt_kind eq "composite"
$src_kind eq $tgt_kind $tgt_kind eq "enum" $tgt_kind eq "composite"
Truth table100%4/4
A and B or C
A= $src_kind eq $tgt_kindB= $tgt_kind eq "enum"C= $tgt_kind eq "composite"
ABCresult
0XX0
1000
11X1
1011
102 27 ▶ my $field = $tgt_kind eq 'enum' ? 'values' : 'attributes';
Branch: 1 branch
BranchTrueFalseTotal
$tgt_kind eq 'enum' ? : 20 7 27
103 27 ▶ if (!@{ $tgt->{$field} // [] } && @{ $src->{$field} // [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$tgt->{$field} // [];} and @{$src->{$field} // [];}) 2 25 27
Condition71%5/7
not @{$tgt->{$field} // [];} and @{$src->{$field} // [];}
!ll&&!rl&&r
2412
$tgt->{$field} // []
l!l
270
$src->{$field} // []
l!l
30
MC/DC100%2/2
not @{$tgt->{$field} // [];} and @{$src->{$field} // [];}
not @{$tgt->{$field} // [];} @{$src->{$field} // [];}
MC/DC0%0/1
$tgt->{$field} // []
$tgt->{$field}
MC/DC0%0/1
$src->{$field} // []
$src->{$field}
Truth table100%3/3
not(A) and B
A= not @{$tgt->{$field} // [];}B= @{$src->{$field} // [];}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $tgt->{$field}
Aresult
11
00
Truth table50%1/2
A
A= $src->{$field}
Aresult
11
00
104 return (
105 2 ▶ "$what $key is declared empty in the target but has $field in the database",
Branch: 1 branch
BranchTrueFalseTotal
$tgt_kind eq 'enum' ? : 1 1 2
106 'The diff does not empty a type: that would be a DROP TYPE of a type the columns',
107 'are of, and their data with them, and PostgreSQL has no ALTER TYPE that',
108 ($tgt_kind eq 'enum' ? 'removes values.' : 'removes all attributes at once.'),
109 'Declare the type as the database has it, or move what uses it to another type',
110 'and drop this one by hand.',
111 );
112 }
113 }
114  
115 42 ▶ if ($src_kind ne $tgt_kind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_kind ne $tgt_kind) 1 41 42
116 return (
117 1 "type $key is " . _article($tgt) . " in the target and " . _article($src) . ' in the database',
118 'PostgreSQL has no ALTER TYPE that makes a type another kind. Move what uses it',
119 'to a new type, drop this one and create it again as the target has it.',
120 );
121 }
122  
123 41 ▶ if ($tgt_kind eq 'enum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tgt_kind eq "enum") 19 22 41
124 19 ▶ my @src = @{ $src->{values} // [] };
Condition50%1/2
$src->{"values"} // []
l!l
190
MC/DC0%0/1
$src->{"values"} // []
$src->{"values"}
Truth table50%1/2
A
A= $src->{"values"}
Aresult
11
00
125 19 ▶ my @tgt = @{ $tgt->{values} // [] };
Condition50%1/2
$tgt->{"values"} // []
l!l
190
MC/DC0%0/1
$tgt->{"values"} // []
$tgt->{"values"}
Truth table50%1/2
A
A= $tgt->{"values"}
Aresult
11
00
126 19 my %in_tgt = map { $_ => 1 } @tgt;
127 19 my %in_src = map { $_ => 1 } @src;
128 19 my @lost = grep { !$in_tgt{$_} } @src;
129 19 ▶ if (@lost) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lost) 2 17 19
130 return (
131 2 "$what $key has lost the value(s) " . join(', ', map { quote_literal($_) } @lost)
132 . ' in the target',
133 'PostgreSQL has no ALTER TYPE ... DROP VALUE. Create a new type with the values',
134 'wanted, move the columns with ALTER TABLE ... ALTER COLUMN ... TYPE ... USING',
135 'col::text::new_type, drop this type and rename the new one.',
136 );
137 }
138 17 my @src_common = @src;
139 17 my @tgt_common = grep { $in_src{$_} } @tgt;
140 17 ▶ if (join("\0", @src_common) ne join("\0", @tgt_common)) {
Branch: 1 branch
BranchTrueFalseTotal
if (join("\0", @src_common) ne join("\0", @tgt_common)) 1 16 17
141 return (
142 1 "$what $key has its values in another order in the target",
143 'PostgreSQL places a new value (ALTER TYPE ... ADD VALUE ... BEFORE|AFTER)',
144 'and does not reorder the ones a type has. Create a new type with the order wanted and',
145 'move the columns to it, as for a lost value.',
146 );
147 }
148 16 return ();
149 }
150  
151 22 ▶ if ($tgt_kind eq 'composite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tgt_kind eq "composite") 6 16 22
152 6 ▶ my @s = map { _attribute_text($_) } @{ $src->{attributes} // [] };
Condition50%1/2
$src->{"attributes"} // []
l!l
60
MC/DC0%0/1
$src->{"attributes"} // []
$src->{"attributes"}
Truth table50%1/2
A
A= $src->{"attributes"}
Aresult
11
00
153 6 ▶ my @t = map { _attribute_text($_) } @{ $tgt->{attributes} // [] };
Condition50%1/2
$tgt->{"attributes"} // []
l!l
60
MC/DC0%0/1
$tgt->{"attributes"} // []
$tgt->{"attributes"}
Truth table50%1/2
A
A= $tgt->{"attributes"}
Aresult
11
00
154 6 ▶ return () if join("\n", @s) eq join("\n", @t);
Branch: 1 branch
BranchTrueFalseTotal
if join("\n", @s) eq join("\n", @t) 2 4 6
155 4 my %s = map { $_ => 1 } @s;
156 4 my %t = map { $_ => 1 } @t;
157 return (
158 "$what $key has other attributes in the target than in the database",
159 5 ▶ _attribute_lines('-', [ grep { !$t{_attribute_text($_)} } @{ $src->{attributes} // [] } ]),
Condition50%1/2
$src->{"attributes"} // []
l!l
40
MC/DC0%0/1
$src->{"attributes"} // []
$src->{"attributes"}
Truth table50%1/2
A
A= $src->{"attributes"}
Aresult
11
00
160 4 ▶ _attribute_lines('+', [ grep { !$s{_attribute_text($_)} } @{ $tgt->{attributes} // [] } ]),
Condition50%1/2
$tgt->{"attributes"} // []
l!l
40
MC/DC0%0/1
$tgt->{"attributes"} // []
$tgt->{"attributes"}
Truth table50%1/2
A
A= $tgt->{"attributes"}
Aresult
11
00
161 'The diff writes no ALTER TYPE ... ADD ATTRIBUTE, DROP ATTRIBUTE or ALTER',
162 'ATTRIBUTE ... TYPE (which also sets the collation). Change the type by hand',
163 '(the attributes keep their order only by being added at the end).',
164 );
165 }
166  
167 16 ▶ if ($tgt_kind eq 'range') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tgt_kind eq "range") 16 0 16
168 16 ▶ my ($s, $t) = map { $_->{subtype} // '' } $src, $tgt;
Condition50%1/2
$_->{"subtype"} // ""
l!l
320
MC/DC0%0/1
$_->{"subtype"} // ""
$_->{"subtype"}
Truth table50%1/2
A
A= $_->{"subtype"}
Aresult
11
00
169 16 ▶ if ($s ne $t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s ne $t) 2 14 16
170 return (
171 2 "$what $key has another subtype in the target than in the database",
172 " database: $s",
173 " target: $t",
174 'PostgreSQL has no ALTER TYPE for the subtype of a range. Move what uses the',
175 'type to a new one, drop this one and create it again.',
176 );
177 }
178 14 for my $extra (@RANGE_EXTRAS) {
179 44 my ($field, $word) = @$extra;
180 44 ▶ my ($s_val, $t_val) = map { $_->{$field} // '' } $src, $tgt;
Condition100%2/2
$_->{$field} // ""
l!l
1672
MC/DC100%1/1
$_->{$field} // ""
$_->{$field}
Truth table100%2/2
A
A= $_->{$field}
Aresult
11
00
181 44 ▶ next if $s_val eq $t_val;
Branch: 1 branch
BranchTrueFalseTotal
if $s_val eq $t_val 36 8 44
182 return (
183 8 ▶ "$what $key has another $word in the target than in the database",
Branch: 2 branches
BranchTrueFalseTotal
$s_val eq '' ? : 4 4 8
$t_val eq '' ? : 4 4 8
184 ' database: ' . ($s_val eq '' ? '(none)' : $s_val),
185 ' target: ' . ($t_val eq '' ? '(none)' : $t_val),
186 'PostgreSQL has no ALTER TYPE for what a range type is built with. Move what',
187 'uses the type to a new one, drop this one and create it again.',
188 );
189 }
190 }
191 6 return ();
192 }
193  
194 2 ▶sub _article {
Subroutine _article: called CC=2 SCAR=6.9
195 2 my ($info) = @_;
196 2 my $name = _kind_name($info);
197 2 ▶ return ($name =~ /\A[aeiou]/ ? 'an ' : 'a ') . $name;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[aeiou]/ ? : 1 1 2
198 }
199  
200 # The types a type is built on: the types of the attributes of a composite
201 # type, the subtype of a range.
202 11 ▶sub _built_on {
Subroutine _built_on: called CC=5 SCAR=16.2
203 11 my ($info) = @_;
204 11 ▶ my $kind = $info->{type_kind} // '';
Condition50%1/2
$info->{"type_kind"} // ""
l!l
110
MC/DC0%0/1
$info->{"type_kind"} // ""
$info->{"type_kind"}
Truth table50%1/2
A
A= $info->{"type_kind"}
Aresult
11
00
205 1 ▶ my @used = $kind eq 'composite' ? map { $_->{type} } @{ $info->{attributes} // [] }
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'range' ? : 1 9 10
Condition50%1/2
$info->{"attributes"} // []
l!l
10
MC/DC0%0/1
$info->{"attributes"} // []
$info->{"attributes"}
Truth table50%1/2
A
A= $info->{"attributes"}
Aresult
11
00
206 : $kind eq 'range' ? ($info->{subtype})
207 11 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'composite' ? : 1 10 11
208 11 return grep { defined } @used;
209 }
210  
211 # The order types are dropped in: a type goes ahead of the types it is built
212 # on. A type is told by its text as format_type gives it -- with its schema
213 # where the search_path does not find it, an array with [] behind it, quoted
214 # where the name needs it -- so both sides are compared unquoted, the
215 # qualified key first and the bare name where the text has no schema.
216 35 ▶sub _drop_order {
Subroutine _drop_order: called CC=9 SCAR=22.3
217 35 my ($source, @keys) = @_;
218 35 my %left = map { $_ => 1 } @keys;
219 35 my %of;
220 35 my %by_key = map { my $k = $_; (my $u = $k) =~ tr/"//d; ($u => $k) } @keys;
221 35 for my $key (@keys) {
222 11 for my $used (_built_on($source->{$key})) {
223 2 (my $name = $used) =~ s/(?:\[\d*\])+\z//;
224 2 $name =~ tr/"//d;
225 my $base = $by_key{$name}
226 2 ▶ // (grep { ($source->{$_}{type_name} // '') eq $name } @keys)[0];
Condition60%3/5
$by_key{$name} // (grep {($source->{$_}{"type_name"} // "") eq $name;} @keys)[0]
l!l&&r!l&&!r
110
$source->{$_}{"type_name"} // ""
l!l
40
MC/DC0%0/2
$by_key{$name} // (grep {($source->{$_}{"type_name"} // "") eq $name;} @keys)[0]
$by_key{$name} (grep {($source->{$_}{"type_name"} // "") eq $name;} @keys)[0]
MC/DC0%0/1
$source->{$_}{"type_name"} // ""
$source->{$_}{"type_name"}
Truth table66%2/3
A // B
A= $by_key{$name}B= (grep {($source->{$_}{"type_name"} // "") eq $name;} @keys)[0]
ABresult
1X1
011
000
Truth table50%1/2
A
A= $source->{$_}{"type_name"}
Aresult
11
00
227 2 ▶ $of{$key}{$base} = 1 if defined $base && $base ne $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $base and $base ne $key 2 0 2
Condition33%1/3
defined $base and $base ne $key
!ll&&!rl&&r
002
MC/DC0%0/2
defined $base and $base ne $key
defined $base $base ne $key
Truth table33%1/3
A and B
A= defined $baseB= $base ne $key
ABresult
0X0
100
111
228 }
229 }
230 35 my @order;
231 35 ▶ while (%left) {
Branch: 1 branch
BranchTrueFalseTotal
if (%left) 11 35 46
232 # the first by key that no type left is built on
233 my ($next) = sort grep {
234 11 my $candidate = $_;
235 17 !grep { $of{$_}{$candidate} } keys %left
236 } keys %left;
237 11 ▶ ($next) = sort keys %left unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 0 11 11
238 11 push @order, $next;
239 11 delete $left{$next};
240 }
241 35 return @order;
242 }
243  
244 58 ▶sub diff {
Subroutine diff: called CC=19 SCAR=29.4
Pod documented
245 58 my ($class, $source, $target, $source_dependents) = @_;
246 58 my @ops;
247  
248 58 _refuse_case_only($source, $target);
249  
250 # A type both sides have that differs in more than new enum values at the
251 # end has no statement here: refused, all of them in one message.
252 55 my @refused;
253 55 for my $key (sort keys %$target) {
254 54 ▶ next unless exists $source->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source->{$key} 10 44 54
255 44 my @lines = _type_change($key, $source->{$key}, $target->{$key});
256 44 ▶ push @refused, \@lines if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 20 24 44
257 }
258 55 ▶ if (@refused) {
Branch: 1 branch
BranchTrueFalseTotal
if (@refused) 19 36 55
259 die join "\n",
260 19 (map { ('refusing to diff: ' . join("\n", @$_), '') } @refused),
261 'No statement was generated.',
262 '';
263 }
264  
265 36 for my $key (sort keys %$target) {
266 34 ▶ if (!exists $source->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $source->{$key}) 10 24 34
267 push @ops, $class->new(
268 action => 'create',
269 type_key => $key,
270 10 type_info => $target->{$key},
271 );
272 10 next;
273 }
274  
275 # Existing type — check for enum value additions
276 24 my $src = $source->{$key};
277 24 my $tgt = $target->{$key};
278  
279 24 ▶ if ($tgt->{type_kind} eq 'enum' && $src->{type_kind} eq 'enum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tgt->{"type_kind"} eq "enum" and $src->{"type_kind"} eq "enum") 16 8 24
Condition66%2/3
$tgt->{"type_kind"} eq "enum" and $src->{"type_kind"} eq "enum"
!ll&&!rl&&r
8016
MC/DC50%1/2
$tgt->{"type_kind"} eq "enum" and $src->{"type_kind"} eq "enum"
$tgt->{"type_kind"} eq "enum" $src->{"type_kind"} eq "enum"
Truth table66%2/3
A and B
A= $tgt->{"type_kind"} eq "enum"B= $src->{"type_kind"} eq "enum"
ABresult
0X0
100
111
280 16 my %src_vals = map { $_ => 1 } @{ $src->{values} };
281 16 my @new_vals = grep { !$src_vals{$_} } @{ $tgt->{values} };
282 16 ▶ if (@new_vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new_vals) 9 7 16
283 # Each new value is placed relative to a value that exists when its
284 # statement runs: after the value before it in the target (an old one
285 # or a new one written ahead of it), or, when it comes first, before
286 # the first value the type has. A value with no old value behind it
287 # needs no position.
288 9 my (@positions, @after_old);
289 9 my $seen_old_after = 0;
290 9 for my $i (reverse 0 .. $#{ $tgt->{values} }) {
291 29 $after_old[$i] = $seen_old_after;
292 29 ▶ $seen_old_after = 1 if $src_vals{ $tgt->{values}[$i] };
Branch: 1 branch
BranchTrueFalseTotal
if $src_vals{$tgt->{"values"}[$i]} 17 12 29
293 }
294 9 my $prev;
295 9 for my $i (0 .. $#{ $tgt->{values} }) {
296 29 my $v = $tgt->{values}[$i];
297 29 ▶ if (!$src_vals{$v} && $after_old[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $src_vals{$v} and $after_old[$i]) { } 7 22 29
elsif (not $src_vals{$v}) { } 5 17 22
Condition100%3/3
not $src_vals{$v} and $after_old[$i]
!ll&&!rl&&r
1757
MC/DC100%2/2
not $src_vals{$v} and $after_old[$i]
not $src_vals{$v} $after_old[$i]
Truth table100%3/3
not(A) and B
A= not $src_vals{$v}B= $after_old[$i]
ABresult
0X0
100
111
298 7 ▶ if (defined $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev) { } 5 2 7
299 5 push @positions, { value => $v, after => $prev };
300 }
301 else {
302 2 my ($first_old) = grep { $src_vals{$_} } @{ $tgt->{values} };
303 2 push @positions, { value => $v, before => $first_old };
304 }
305 }
306 elsif (!$src_vals{$v}) {
307 5 push @positions, { value => $v };
308 }
309 29 $prev = $v;
310 }
311 9 push @ops, $class->new(
312 action => 'add_value',
313 type_key => $key,
314 type_info => $tgt,
315 added_values => \@new_vals,
316 value_positions => \@positions,
317 );
318 }
319 }
320 }
321  
322 36 my @dropped = grep { !exists $target->{$_} } sort keys %$source;
323  
324 # What stands on a type outside the model is named, not left to the server.
325 36 DBIO::PostgreSQL::Diff::Dependents->refuse(map { {
326 object => _kind_name($source->{$_}) . " $_",
327 12 ▶ dependents => ($source_dependents // {})->{types}{$_},
Condition100%2/2
$source_dependents // {}
l!l
102
MC/DC100%1/1
$source_dependents // {}
$source_dependents
Truth table100%2/2
A
A= $source_dependents
Aresult
11
00
328 } } @dropped);
329  
330 35 for my $key (_drop_order($source, @dropped)) {
331 push @ops, $class->new(
332 action => 'drop',
333 type_key => $key,
334 11 type_info => $source->{$key},
335 );
336 }
337  
338 35 return @ops;
339 }
340  
341  
342 82 ▶sub as_sql {
Subroutine as_sql: called CC=20 SCAR=30.0
Pod documented
343 82 my ($self) = @_;
344 82 my $info = $self->type_info;
345 82 my $type = quote_qualified(
346 DBIO::PostgreSQL::Introspect->key_names($self->type_key, type_name => $info));
347 82 ▶ my $object = 'type ' . ($self->type_key // '');
Condition50%1/2
$self->type_key // ""
l!l
820
MC/DC0%0/1
$self->type_key // ""
$self->type_key
Truth table50%1/2
A
A= $self->type_key
Aresult
11
00
348  
349 82 ▶ if ($self->action eq 'create') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->action eq 'create') { } 40 42 82
elsif ($self->action eq 'drop') { } 32 10 42
elsif ($self->action eq 'add_value') { } 9 1 10
350 40 ▶ my $kind = $info->{type_kind} // '';
Condition50%1/2
$info->{"type_kind"} // ""
l!l
400
MC/DC0%0/1
$info->{"type_kind"} // ""
$info->{"type_kind"}
Truth table50%1/2
A
A= $info->{"type_kind"}
Aresult
11
00
351 40 ▶ if ($kind eq 'enum') {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind eq 'enum') { } 28 12 40
elsif ($kind eq 'composite') { } 3 9 12
elsif ($kind eq 'range') { } 8 1 9
352 28 my $values = join ', ', map { quote_literal($_) } @{ $info->{values} };
353 28 return sprintf "CREATE TYPE %s AS ENUM (%s);", $type, $values;
354 }
355 elsif ($kind eq 'composite') {
356 my $attrs = join ",\n ", map {
357 quote_ident($_->{name}) . " $_->{type}"
358 6 ▶ . ( ($_->{collation} // '') ne '' ? " COLLATE $_->{collation}" : '' )
Branch: 1 branch
BranchTrueFalseTotal
($_->{'collation'} // '') ne '' ? : 1 5 6
Condition100%2/2
$_->{"collation"} // ""
l!l
15
MC/DC100%1/1
$_->{"collation"} // ""
$_->{"collation"}
Truth table100%2/2
A
A= $_->{"collation"}
Aresult
11
00
359 3 } @{ $info->{attributes} };
360 3 return sprintf "CREATE TYPE %s AS (\n %s\n);", $type, $attrs;
361 }
362 elsif ($kind eq 'range') {
363 DBIO::PostgreSQL::Diff::NoStatement->throw(
364 $self, $object, 'its type_info has no subtype, and a range type is created over one',
365 8 ▶ ) unless ($info->{subtype} // '') =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"subtype"} // "") =~ /\S/ 3 5 8
Condition100%2/2
$info->{"subtype"} // ""
l!l
71
MC/DC100%1/1
$info->{"subtype"} // ""
$info->{"subtype"}
Truth table100%2/2
A
A= $info->{"subtype"}
Aresult
11
00
366 my %option = (
367 SUBTYPE => $info->{subtype},
368 SUBTYPE_OPCLASS => $info->{subtype_opclass},
369 COLLATION => $info->{collation},
370 CANONICAL => $info->{canonical},
371 SUBTYPE_DIFF => $info->{subtype_diff},
372 5 );
373 return sprintf "CREATE TYPE %s AS RANGE (%s);", $type, join ', ',
374 9 map { "$_ = $option{$_}" }
375 5 ▶ grep { ($option{$_} // '') ne '' } qw( SUBTYPE SUBTYPE_OPCLASS COLLATION CANONICAL SUBTYPE_DIFF );
Condition100%2/2
$option{$_} // ""
l!l
916
MC/DC100%1/1
$option{$_} // ""
$option{$_}
Truth table100%2/2
A
A= $option{$_}
Aresult
11
00
376 }
377 DBIO::PostgreSQL::Diff::NoStatement->throw(
378 1 $self, $object,
379 "its type_info is of kind '$kind', and the diff creates an enum, a composite and a range type",
380 );
381 }
382 elsif ($self->action eq 'drop') {
383 32 return sprintf "DROP TYPE %s;", $type;
384 }
385 elsif ($self->action eq 'add_value') {
386 9 ▶ my @values = @{ $self->added_values // [] };
Condition50%1/2
$self->added_values // []
l!l
90
MC/DC0%0/1
$self->added_values // []
$self->added_values
Truth table50%1/2
A
A= $self->added_values
Aresult
11
00
387 9 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object, 'it has no added_values')
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 9 9
388 unless @values;
389 9 ▶ my %where = map { $_->{value} => $_ } @{ $self->value_positions // [] };
Condition100%2/2
$self->value_positions // []
l!l
72
MC/DC100%1/1
$self->value_positions // []
$self->value_positions
Truth table100%2/2
A
A= $self->value_positions
Aresult
11
00
390 return join "\n", map {
391 9 ▶ my $pos = $where{$_} // {};
Condition100%2/2
$where{$_} // {}
l!l
104
MC/DC100%1/1
$where{$_} // {}
$where{$_}
Truth table100%2/2
A
A= $where{$_}
Aresult
11
00
392 sprintf "ALTER TYPE %s ADD VALUE %s%s;", $type, quote_literal($_),
393 exists $pos->{after} ? ' AFTER ' . quote_literal($pos->{after})
394 : exists $pos->{before} ? ' BEFORE ' . quote_literal($pos->{before})
395 14 ▶ : '';
Branch: 2 branches
BranchTrueFalseTotal
exists $pos->{'after'} ? : 4 10 14
exists $pos->{'before'} ? : 2 8 10
396 } @values;
397 }
398 1 DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object);
399 }
400  
401  
402 2 ▶sub summary {
Subroutine summary: called CC=3 SCAR=13.6
Pod documented
403 2 my ($self) = @_;
404 2 ▶ if ($self->action eq 'add_value') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add_value") 0 2 2
405 0 my $count = scalar @{ $self->added_values };
406 0 my $vals = join ', ', @{ $self->added_values };
407 0 return sprintf ' ~type: %s +%d value(s) (%s)', $self->type_key, $count, $vals;
408 }
409 2 ▶ my $prefix = $self->action eq 'create' ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 1 1 2
410 2 return sprintf ' %stype: %s (%s)', $prefix, $self->type_key, $self->type_info->{type_kind};
411 }
412  
413 106 1;
414  
415 __END__
« blib/lib/DBIO/PostgreSQL/Introspect/OutsideReaders.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/Triggers.pm »